diff --git a/README.md b/README.md index 74af061..3f74fef 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ Configuration is done using environment variables. - `PASSED_MAX_LENGTH`: Maximum password length in KiB, defaults to `12288`. - `PASSED_STORE_TYPE`: Store type to pick, defaults to `ram`. - `ram`: Passwords are stored in RAM. - - `dir`: Passwords are stored in a directory. The directory is specified using `PASSED_STORE_DIR_PATH`, which defaults to `passwords`. PassED will **not** create the directory for you. + - `dir`: Passwords are stored in a directory. The directory is specified using `PASSED_STORE_DIR_PATH`, which defaults to `passwords`. PassED will create the directory for you. - `PASSED_STORE_CLEAR_INTERVAL`: Time that should pass between clearing expired passwords in seconds, defaults to `30`. ## Translators diff --git a/config/config.go b/config/config.go deleted file mode 100644 index c0a8aad..0000000 --- a/config/config.go +++ /dev/null @@ -1,37 +0,0 @@ -package config - -import ( - "log" - "os" - "strconv" -) - -func Env(name string, out any, def string) { - raw := os.Getenv(name) - if raw == "" { - raw = def - log.Printf("No \"%s\" provided, defaulting to \"%s\".", name, def) - } - - switch value := out.(type) { - case *int: - i, err := strconv.ParseInt(raw, 10, 64) - if err != nil { - log.Printf("\"%s\" is not a number (\"%s\").", name, raw) - return - } - - *value = int(i) - - case *bool: - switch raw { - case "true", "TRUE", "1": - *value = true - case "false", "FALSE", "0": - *value = false - } - - case *string: - *value = raw - } -} diff --git a/main.go b/main.go index ae9844b..e2a9446 100644 --- a/main.go +++ b/main.go @@ -5,9 +5,10 @@ import ( "errors" "log" "net/http" + "os" + "strconv" "time" - "git.1e99.eu/1e99/passed/config" "git.1e99.eu/1e99/passed/routes" "git.1e99.eu/1e99/passed/storage" ) @@ -24,9 +25,9 @@ func run() error { var address string var logRequests bool var maxPasswordLength int - config.Env("PASSED_ADDRESS", &address, ":3000") - config.Env("PASSED_LOG_REQUESTS", &logRequests, "true") - config.Env("PASSED_MAX_LENGTH", &maxPasswordLength, "12288") + env("PASSED_ADDRESS", &address, ":3000") + env("PASSED_LOG_REQUESTS", &logRequests, "true") + env("PASSED_MAX_LENGTH", &maxPasswordLength, "12288") mux := http.NewServeMux() handler := http.Handler(mux) @@ -52,15 +53,20 @@ func run() error { func newStore() (store storage.Store, err error) { var storeType string var clearInterval int - config.Env("PASSED_STORE_TYPE", &storeType, "ram") - config.Env("PASSED_STORE_CLEAR_INTERVAL", &clearInterval, "30") + env("PASSED_STORE_TYPE", &storeType, "ram") + env("PASSED_STORE_CLEAR_INTERVAL", &clearInterval, "30") switch storeType { case "ram": store = storage.NewRamStore() case "dir", "directory": var path string - config.Env("PASSED_STORE_DIR_PATH", &path, "passwords") + env("PASSED_STORE_DIR_PATH", &path, "passwords") + + err = os.MkdirAll(path, os.ModePerm) + if err != nil { + return nil, err + } store = storage.NewDirStore(path) default: @@ -92,3 +98,33 @@ func main() { log.Fatalf("%s", err) } } + +func env(name string, out any, def string) { + raw := os.Getenv(name) + if raw == "" { + raw = def + log.Printf("No \"%s\" provided, defaulting to \"%s\".", name, def) + } + + switch value := out.(type) { + case *int: + i, err := strconv.ParseInt(raw, 10, 64) + if err != nil { + log.Printf("\"%s\" is not a number (\"%s\").", name, raw) + return + } + + *value = int(i) + + case *bool: + switch raw { + case "true", "TRUE", "1": + *value = true + case "false", "FALSE", "0": + *value = false + } + + case *string: + *value = raw + } +}