From 1d01acca36b78eeba99da1adb10e72d186433b39 Mon Sep 17 00:00:00 2001 From: Mitchell Riedstra Date: Fri, 6 Jan 2023 00:04:28 -0500 Subject: Update site to server configuration via environment variables. Add a genhash command. Update docs. --- cmd/server/genhash.go | 38 +++++++++++++++++ cmd/server/main.go | 114 ++++++++++++++++++++++++++++++++------------------ 2 files changed, 112 insertions(+), 40 deletions(-) create mode 100644 cmd/server/genhash.go (limited to 'cmd') diff --git a/cmd/server/genhash.go b/cmd/server/genhash.go new file mode 100644 index 0000000..47f7bd4 --- /dev/null +++ b/cmd/server/genhash.go @@ -0,0 +1,38 @@ +package main + +import ( + "bytes" + "fmt" + + "golang.org/x/crypto/bcrypt" + "golang.org/x/term" +) + +func interactiveHashGen() { + fmt.Print("Enter password: ") + + passwd, err := term.ReadPassword(0) + if err != nil { + logger.Fatal("\nFailed: ", err) + } + + fmt.Printf("\nAgain: ") + + passwd2, err := term.ReadPassword(0) + if err != nil { + logger.Fatal("\nFailed: ", err) + } + + fmt.Println("") + + if !bytes.Equal(passwd, passwd2) { + logger.Fatal("Passwords do not match") + } + + passwd, err = bcrypt.GenerateFromPassword(passwd, bcrypt.DefaultCost) + if err != nil { + logger.Fatal("Failed: ", err) + } + + fmt.Printf("hash: %s\n", string(passwd)) +} diff --git a/cmd/server/main.go b/cmd/server/main.go index 29cffbf..f4f8dc3 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -11,83 +11,117 @@ import ( "github.com/gomodule/redigo/redis" "gopkg.in/yaml.v3" + "riedstra.dev/mitch/go-website/envflag" "riedstra.dev/mitch/go-website/page" ) var VersionString = "" +var logger = log.New(os.Stderr, "", 0) + func VersionPrint() { fmt.Println(VersionString) os.Exit(0) } +func logIfErr(err error) { + if err != nil { + logger.Println(err) + } +} + func main() { //nolint:funlen - fl := flag.NewFlagSet("Website", flag.ExitOnError) - listen := fl.String("l", "0.0.0.0:8001", "Listening address") - directory := fl.String("d", ".", "Directory to serve.") - version := fl.Bool("v", false, "Print the version then exit") - confFn := fl.String("c", "conf.yml", "Location for the config file") - authConfFn := fl.String("ac", "auth.json", - "Location for authorization configuration file") - verbose := fl.Bool("V", false, "Be more verbose ( dump config, etc ) ") - fl.StringVar(&page.TimeFormat, "T", page.TimeFormat, - "Set the page time format, be careful with this") - - defaultIndexPath := "/reIndex" - - indexPath := fl.String("i", defaultIndexPath, - "Path in which, when called will rebuild the index and clear the cache") - redisAddr := fl.String("r", "127.0.0.1:6379", "Redis server set to \"\" to disable") - redisKey := fl.String("rk", "go-website", "Redis key to use for storing cached pages") + var ( + listen = ":8001" + directory = "." + version = false + confFn = "conf.yml" + authConfFn = "auth.json" + verbose = false + defaultIndexPath = "/reIndex" + indexPath = "/reIndex" + redisAddr = "127.0.0.1:6379" + redisKey = "go-website" + pageTimeout = 15 + genhash = false + ) - pageTimeout := fl.Int("timeout", 15, "Seconds until page timeout for read and write") + fl := flag.NewFlagSet("Website", flag.ExitOnError) - fl.BoolVar(&page.CacheIndex, "cache-index", true, - "If set to false do not cache index") + envflag.String(fl, &directory, "d", "SITE_DIR", + "Website directory to serve") + envflag.String(fl, &listen, "l", "LISTEN_ADDR", "listening address") + logIfErr(envflag.Bool(fl, &version, "v", "PRINT_VERSION_AND_EXIT", + "print version and exit")) + envflag.String(fl, &confFn, "c", "CONFIG_FILE", + "Location for configuration file") + envflag.String(fl, &authConfFn, "ac", "AUTH_CONFIG", + "location for the authorization config") + logIfErr(envflag.Bool(fl, &verbose, "V", "VERBOSE", + "Be more verbose, dump config and such")) + envflag.String(fl, &page.TimeFormat, "T", "TIME_FORMAT", + "Override the default format used to parse page time. Be careful.") + envflag.String(fl, &indexPath, "i", "INDEX_PATH", + "Path in which, when called will rebuild the index and clear the cache") + envflag.String(fl, &redisAddr, "r", "REDIS_ADDR", + "Redis server set to \"\" to disable") + envflag.String(fl, &redisKey, "rk", "REDIS_KEY", + "Redis key to use for storing cached pages") + logIfErr(envflag.Int(fl, &pageTimeout, "timeout", "HTTP_TIMEOUT", + "Seconds until page timeout for read and write")) + logIfErr(envflag.Bool(fl, &page.CacheIndex, "cache-index", "CACHE_INDEX", + "If set to false, do not cache the page index")) + logIfErr(envflag.Bool(fl, &genhash, "genhash", "INTERACTIVE_HASH_GEN", + "If set to true, interactively generate a password hash")) _ = fl.Parse(os.Args[1:]) - if *version { + if version { VersionPrint() } - if err := os.Chdir(*directory); err != nil { - log.Fatal(err) + if genhash { + interactiveHashGen() + os.Exit(0) + } + + if err := os.Chdir(directory); err != nil { + logger.Fatal(err) } - app, err := loadConf(*confFn) + app, err := loadConf(confFn) if err != nil { - log.Println(err) + logger.Println(err) app = &App{} } - err = app.ReadAuth(*authConfFn) + err = app.ReadAuth(authConfFn) if err != nil { - log.Println(err) + logger.Println(err) } - if app.ReIndexPath == "" || *indexPath != defaultIndexPath { - app.ReIndexPath = *indexPath + if app.ReIndexPath == "" || indexPath != defaultIndexPath { + app.ReIndexPath = indexPath } if app.RedisKey == "" { - app.RedisKey = *redisKey + app.RedisKey = redisKey } - if *redisAddr != "" { + if redisAddr != "" { app.redisPool = &redis.Pool{ MaxIdle: 80, //nolint:gomnd MaxActive: 12000, //nolint:gomnd Dial: func() (redis.Conn, error) { - c, err := redis.Dial("tcp", *redisAddr) + c, err := redis.Dial("tcp", redisAddr) - if strings.HasPrefix(*redisAddr, "unix:") { - c, err = redis.Dial("unix", strings.TrimPrefix(*redisAddr, "unix:")) + if strings.HasPrefix(redisAddr, "unix:") { + c, err = redis.Dial("unix", strings.TrimPrefix(redisAddr, "unix:")) } if err != nil { - log.Println("Redis dial error: ", err) + logger.Println("Redis dial error: ", err) } return c, err //nolint @@ -95,7 +129,7 @@ func main() { //nolint:funlen } } - if *verbose { + if verbose { b, _ := yaml.Marshal(app) os.Stderr.Write(b) } @@ -104,9 +138,9 @@ func main() { //nolint:funlen srv := &http.Server{ Handler: app, - Addr: *listen, - WriteTimeout: time.Duration(*pageTimeout) * time.Second, - ReadTimeout: time.Duration(*pageTimeout) * time.Second, + Addr: listen, + WriteTimeout: time.Duration(pageTimeout) * time.Second, + ReadTimeout: time.Duration(pageTimeout) * time.Second, } - log.Fatal(srv.ListenAndServe()) + logger.Fatal(srv.ListenAndServe()) } -- cgit v1.2.3