dir store now creates directory

This commit is contained in:
1e99 2024-11-30 11:01:38 +01:00
parent 6855166afc
commit 972e941da3
3 changed files with 44 additions and 45 deletions

View file

@ -67,7 +67,7 @@ Configuration is done using environment variables.
- `PASSED_MAX_LENGTH`: Maximum password length in KiB, defaults to `12288`. - `PASSED_MAX_LENGTH`: Maximum password length in KiB, defaults to `12288`.
- `PASSED_STORE_TYPE`: Store type to pick, defaults to `ram`. - `PASSED_STORE_TYPE`: Store type to pick, defaults to `ram`.
- `ram`: Passwords are stored in 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`. - `PASSED_STORE_CLEAR_INTERVAL`: Time that should pass between clearing expired passwords in seconds, defaults to `30`.
## Translators ## Translators

View file

@ -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
}
}

50
main.go
View file

@ -5,9 +5,10 @@ import (
"errors" "errors"
"log" "log"
"net/http" "net/http"
"os"
"strconv"
"time" "time"
"git.1e99.eu/1e99/passed/config"
"git.1e99.eu/1e99/passed/routes" "git.1e99.eu/1e99/passed/routes"
"git.1e99.eu/1e99/passed/storage" "git.1e99.eu/1e99/passed/storage"
) )
@ -24,9 +25,9 @@ func run() error {
var address string var address string
var logRequests bool var logRequests bool
var maxPasswordLength int var maxPasswordLength int
config.Env("PASSED_ADDRESS", &address, ":3000") env("PASSED_ADDRESS", &address, ":3000")
config.Env("PASSED_LOG_REQUESTS", &logRequests, "true") env("PASSED_LOG_REQUESTS", &logRequests, "true")
config.Env("PASSED_MAX_LENGTH", &maxPasswordLength, "12288") env("PASSED_MAX_LENGTH", &maxPasswordLength, "12288")
mux := http.NewServeMux() mux := http.NewServeMux()
handler := http.Handler(mux) handler := http.Handler(mux)
@ -52,15 +53,20 @@ func run() error {
func newStore() (store storage.Store, err error) { func newStore() (store storage.Store, err error) {
var storeType string var storeType string
var clearInterval int var clearInterval int
config.Env("PASSED_STORE_TYPE", &storeType, "ram") env("PASSED_STORE_TYPE", &storeType, "ram")
config.Env("PASSED_STORE_CLEAR_INTERVAL", &clearInterval, "30") env("PASSED_STORE_CLEAR_INTERVAL", &clearInterval, "30")
switch storeType { switch storeType {
case "ram": case "ram":
store = storage.NewRamStore() store = storage.NewRamStore()
case "dir", "directory": case "dir", "directory":
var path string 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) store = storage.NewDirStore(path)
default: default:
@ -92,3 +98,33 @@ func main() {
log.Fatalf("%s", err) 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
}
}