add store directory creation

This commit is contained in:
rmanach
2025-01-03 14:48:49 +01:00
parent 18661f82f8
commit e0d7eeadc3
6 changed files with 78 additions and 39 deletions
+33 -10
View File
@@ -9,37 +9,45 @@ import (
"github.com/rs/zerolog/log"
)
const defaultAPISessionExpirationDuration = 30 * time.Second
const defaultPort = 8585
const (
defaultAPISessionExpirationDuration = 30 * time.Second
defaultPort = 8585
defaultMainDir = "./store"
)
var GetEnv = sync.OnceValue[env](newEnv)
var env = sync.OnceValue[environment](newEnv)()
type env struct {
type environment struct {
adminUsername string
adminPassword string
sessionExpirationDuration time.Duration
port int
isSecure bool
storeDir string
}
func (e env) GetCredentials() (username, password string) {
func (e environment) GetCredentials() (username, password string) {
return e.adminUsername, e.adminPassword
}
func (e env) GetSessionExpirationDuration() time.Duration {
func (e environment) GetSessionExpirationDuration() time.Duration {
return e.sessionExpirationDuration
}
func (e env) GetPort() int {
func (e environment) GetPort() int {
return e.port
}
func (e env) IsSecure() bool {
func (e environment) IsSecure() bool {
return e.isSecure
}
func newEnv() env {
env := env{
func (e environment) GetDir() string {
return e.storeDir
}
func newEnv() environment {
env := environment{
adminUsername: os.Getenv("API_ADMIN_USERNAME"),
adminPassword: os.Getenv("API_ADMIN_PASSWORD"),
isSecure: os.Getenv("API_SECURE") == "true",
@@ -59,5 +67,20 @@ func newEnv() env {
env.port = defaultPort
}
storeDir := os.Getenv("API_STORE_DIR")
if storeDir == "" {
storeDir = defaultMainDir
}
if err := os.MkdirAll(storeDir, 0755); err != nil { //nolint
log.Fatal().Err(err).Msg("unable to create store dir")
}
env.storeDir = storeDir
return env
}
func GetEnv() environment {
return env
}