From 1bcdf0e813262cc64a78996a7d67de22ba47271d Mon Sep 17 00:00:00 2001 From: Haelnorr Date: Fri, 2 Jan 2026 19:12:42 +1100 Subject: [PATCH] migrated out env module and moved config to internal --- cmd/projectreshoot/run.go | 2 +- go.mod | 1 + go.sum | 2 + {pkg => internal}/config/config.go | 35 +++++----- internal/handler/login.go | 2 +- internal/handler/movie.go | 2 +- internal/handler/movie_search.go | 2 +- internal/handler/reauthenticatate.go | 2 +- internal/handler/register.go | 2 +- internal/httpserver/routes.go | 2 +- internal/httpserver/server.go | 2 +- internal/middleware/authentication.go | 2 +- pkg/config/environment.go | 94 --------------------------- 13 files changed, 30 insertions(+), 120 deletions(-) rename {pkg => internal}/config/config.go (75%) delete mode 100644 pkg/config/environment.go diff --git a/cmd/projectreshoot/run.go b/cmd/projectreshoot/run.go index 9b9841c..7ae00f4 100644 --- a/cmd/projectreshoot/run.go +++ b/cmd/projectreshoot/run.go @@ -7,8 +7,8 @@ import ( "net/http" "os" "os/signal" + "projectreshoot/internal/config" "projectreshoot/internal/httpserver" - "projectreshoot/pkg/config" "projectreshoot/pkg/embedfs" "sync" "time" diff --git a/go.mod b/go.mod index e7137b3..43b3d8b 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.25.5 require ( git.haelnorr.com/h/golib/cookies v0.9.0 + git.haelnorr.com/h/golib/env v0.9.0 git.haelnorr.com/h/golib/hlog v0.9.0 git.haelnorr.com/h/golib/jwt v0.9.2 git.haelnorr.com/h/golib/tmdb v0.8.0 diff --git a/go.sum b/go.sum index 4f3e3f3..62949aa 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ git.haelnorr.com/h/golib/cookies v0.9.0 h1:Vf+eX1prHkKuGrQon1BHY87yaPc1H+HJFRXDOV/AuWs= git.haelnorr.com/h/golib/cookies v0.9.0/go.mod h1:y1385YExI9gLwckCVDCYVcsFXr6N7T3brJjnJD2QIuo= +git.haelnorr.com/h/golib/env v0.9.0 h1:Ahqr3PbHy7HdWEHUhylzIZy6Gg8mST5UdgKlU2RAhls= +git.haelnorr.com/h/golib/env v0.9.0/go.mod h1:glUQVdA1HMKX1avTDyTyuhcr36SSxZtlJxKDT5KTztg= git.haelnorr.com/h/golib/hlog v0.9.0 h1:ib8n2MdmiRK2TF067p220kXmhDe9aAnlcsgpuv+QpvE= git.haelnorr.com/h/golib/hlog v0.9.0/go.mod h1:oOlzb8UVHUYP1k7dN5PSJXVskAB2z8EYgRN85jAi0Zk= git.haelnorr.com/h/golib/jwt v0.9.2 h1:l1Ow7DPGACAU54CnMP/NlZjdc4nRD1wr3xZ8a7taRvU= diff --git a/pkg/config/config.go b/internal/config/config.go similarity index 75% rename from pkg/config/config.go rename to internal/config/config.go index 534a05e..2980bdc 100644 --- a/pkg/config/config.go +++ b/internal/config/config.go @@ -5,6 +5,7 @@ import ( "os" "time" + "git.haelnorr.com/h/golib/env" "git.haelnorr.com/h/golib/hlog" "git.haelnorr.com/h/golib/tmdb" "github.com/joho/godotenv" @@ -47,17 +48,17 @@ func GetConfig(args map[string]string) (*Config, error) { if args["host"] != "" { host = args["host"] } else { - host = GetEnvDefault("HOST", "127.0.0.1") + host = env.String("HOST", "127.0.0.1") } if args["port"] != "" { port = args["port"] } else { - port = GetEnvDefault("PORT", "3010") + port = env.String("PORT", "3010") } if args["loglevel"] != "" { logLevel = hlog.LogLevel(args["loglevel"]) } else { - logLevel = hlog.LogLevel(GetEnvDefault("LOG_LEVEL", "info")) + logLevel = hlog.LogLevel(env.String("LOG_LEVEL", "info")) } if args["logoutput"] != "" { opts := map[string]string{ @@ -73,7 +74,7 @@ func GetConfig(args map[string]string) (*Config, error) { ) } } else { - logOutput = GetEnvDefault("LOG_OUTPUT", "console") + logOutput = env.String("LOG_OUTPUT", "console") } if logOutput != "both" && logOutput != "console" && logOutput != "file" { logOutput = "console" @@ -86,22 +87,22 @@ func GetConfig(args map[string]string) (*Config, error) { config := &Config{ Host: host, Port: port, - TrustedHost: GetEnvDefault("TRUSTED_HOST", "127.0.0.1"), - SSL: GetEnvBool("SSL_MODE", false), - GZIP: GetEnvBool("GZIP", false), - ReadHeaderTimeout: GetEnvDur("READ_HEADER_TIMEOUT", 2), - WriteTimeout: GetEnvDur("WRITE_TIMEOUT", 10), - IdleTimeout: GetEnvDur("IDLE_TIMEOUT", 120), + TrustedHost: env.String("TRUSTED_HOST", "127.0.0.1"), + SSL: env.Bool("SSL_MODE", false), + GZIP: env.Bool("GZIP", false), + ReadHeaderTimeout: env.Duration("READ_HEADER_TIMEOUT", 2), + WriteTimeout: env.Duration("WRITE_TIMEOUT", 10), + IdleTimeout: env.Duration("IDLE_TIMEOUT", 120), DBName: "00001", - DBLockTimeout: GetEnvDur("DB_LOCK_TIMEOUT", 60), - SecretKey: os.Getenv("SECRET_KEY"), - AccessTokenExpiry: GetEnvInt64("ACCESS_TOKEN_EXPIRY", 5), - RefreshTokenExpiry: GetEnvInt64("REFRESH_TOKEN_EXPIRY", 1440), // defaults to 1 day - TokenFreshTime: GetEnvInt64("TOKEN_FRESH_TIME", 5), + DBLockTimeout: env.Duration("DB_LOCK_TIMEOUT", 60), + SecretKey: env.String("SECRET_KEY", ""), + AccessTokenExpiry: env.Int64("ACCESS_TOKEN_EXPIRY", 5), + RefreshTokenExpiry: env.Int64("REFRESH_TOKEN_EXPIRY", 1440), // defaults to 1 day + TokenFreshTime: env.Int64("TOKEN_FRESH_TIME", 5), LogLevel: logLevel, LogOutput: logOutput, - LogDir: GetEnvDefault("LOG_DIR", ""), - TMDBToken: os.Getenv("TMDB_API_TOKEN"), + LogDir: env.String("LOG_DIR", ""), + TMDBToken: env.String("TMDB_API_TOKEN", ""), TMDBConfig: tmdbcfg, } diff --git a/internal/handler/login.go b/internal/handler/login.go index b9a92f6..0f369a8 100644 --- a/internal/handler/login.go +++ b/internal/handler/login.go @@ -6,10 +6,10 @@ import ( "net/http" "time" + "projectreshoot/internal/config" "projectreshoot/internal/models" "projectreshoot/internal/view/component/form" "projectreshoot/internal/view/page" - "projectreshoot/pkg/config" "git.haelnorr.com/h/golib/cookies" "git.haelnorr.com/h/golib/hlog" diff --git a/internal/handler/movie.go b/internal/handler/movie.go index ca352fa..e33aeb5 100644 --- a/internal/handler/movie.go +++ b/internal/handler/movie.go @@ -2,8 +2,8 @@ package handler import ( "net/http" + "projectreshoot/internal/config" "projectreshoot/internal/view/page" - "projectreshoot/pkg/config" "strconv" "git.haelnorr.com/h/golib/hlog" diff --git a/internal/handler/movie_search.go b/internal/handler/movie_search.go index dfa7b98..aad319d 100644 --- a/internal/handler/movie_search.go +++ b/internal/handler/movie_search.go @@ -2,9 +2,9 @@ package handler import ( "net/http" + "projectreshoot/internal/config" "projectreshoot/internal/view/component/search" "projectreshoot/internal/view/page" - "projectreshoot/pkg/config" "git.haelnorr.com/h/golib/hlog" "git.haelnorr.com/h/golib/tmdb" diff --git a/internal/handler/reauthenticatate.go b/internal/handler/reauthenticatate.go index 3757499..46eb5dc 100644 --- a/internal/handler/reauthenticatate.go +++ b/internal/handler/reauthenticatate.go @@ -6,8 +6,8 @@ import ( "net/http" "time" + "projectreshoot/internal/config" "projectreshoot/internal/view/component/form" - "projectreshoot/pkg/config" "projectreshoot/pkg/contexts" "git.haelnorr.com/h/golib/hlog" diff --git a/internal/handler/register.go b/internal/handler/register.go index 25e7587..6eb493d 100644 --- a/internal/handler/register.go +++ b/internal/handler/register.go @@ -6,10 +6,10 @@ import ( "net/http" "time" + "projectreshoot/internal/config" "projectreshoot/internal/models" "projectreshoot/internal/view/component/form" "projectreshoot/internal/view/page" - "projectreshoot/pkg/config" "git.haelnorr.com/h/golib/cookies" "git.haelnorr.com/h/golib/hlog" diff --git a/internal/httpserver/routes.go b/internal/httpserver/routes.go index e96c04b..12f4539 100644 --- a/internal/httpserver/routes.go +++ b/internal/httpserver/routes.go @@ -4,10 +4,10 @@ import ( "database/sql" "net/http" + "projectreshoot/internal/config" "projectreshoot/internal/handler" "projectreshoot/internal/middleware" "projectreshoot/internal/view/page" - "projectreshoot/pkg/config" "git.haelnorr.com/h/golib/hlog" "git.haelnorr.com/h/golib/jwt" diff --git a/internal/httpserver/server.go b/internal/httpserver/server.go index 5d73bc4..c407027 100644 --- a/internal/httpserver/server.go +++ b/internal/httpserver/server.go @@ -7,8 +7,8 @@ import ( "net/http" "time" + "projectreshoot/internal/config" "projectreshoot/internal/middleware" - "projectreshoot/pkg/config" "git.haelnorr.com/h/golib/hlog" "git.haelnorr.com/h/golib/jwt" diff --git a/internal/middleware/authentication.go b/internal/middleware/authentication.go index efa0b25..71a9a42 100644 --- a/internal/middleware/authentication.go +++ b/internal/middleware/authentication.go @@ -7,9 +7,9 @@ import ( "sync/atomic" "time" + "projectreshoot/internal/config" "projectreshoot/internal/handler" "projectreshoot/internal/models" - "projectreshoot/pkg/config" "projectreshoot/pkg/contexts" "git.haelnorr.com/h/golib/cookies" diff --git a/pkg/config/environment.go b/pkg/config/environment.go deleted file mode 100644 index 2c4c815..0000000 --- a/pkg/config/environment.go +++ /dev/null @@ -1,94 +0,0 @@ -package config - -import ( - "os" - "strconv" - "strings" - "time" -) - -// Get an environment variable, specifying a default value if its not set -func GetEnvDefault(key string, defaultValue string) string { - val, exists := os.LookupEnv(key) - if !exists { - return defaultValue - } - return val -} - -// Get an environment variable as a time.Duration, specifying a default value if its -// not set or can't be parsed properly -func GetEnvDur(key string, defaultValue time.Duration) time.Duration { - val, exists := os.LookupEnv(key) - if !exists { - return time.Duration(defaultValue) - } - - intVal, err := strconv.Atoi(val) - if err != nil { - return time.Duration(defaultValue) - } - return time.Duration(intVal) - -} - -// Get an environment variable as an int, specifying a default value if its -// not set or can't be parsed properly into an int -func GetEnvInt(key string, defaultValue int) int { - val, exists := os.LookupEnv(key) - if !exists { - return defaultValue - } - - intVal, err := strconv.Atoi(val) - if err != nil { - return defaultValue - } - return intVal - -} - -// Get an environment variable as an int64, specifying a default value if its -// not set or can't be parsed properly into an int64 -func GetEnvInt64(key string, defaultValue int64) int64 { - val, exists := os.LookupEnv(key) - if !exists { - return defaultValue - } - - intVal, err := strconv.ParseInt(val, 10, 64) - if err != nil { - return defaultValue - } - return intVal - -} - -// Get an environment variable as a boolean, specifying a default value if its -// not set or can't be parsed properly into a bool -func GetEnvBool(key string, defaultValue bool) bool { - val, exists := os.LookupEnv(key) - if !exists { - return defaultValue - } - truthy := map[string]bool{ - "true": true, "t": true, "yes": true, "y": true, "on": true, "1": true, - "enable": true, "enabled": true, "active": true, "affirmative": true, - } - - falsy := map[string]bool{ - "false": false, "f": false, "no": false, "n": false, "off": false, "0": false, - "disable": false, "disabled": false, "inactive": false, "negative": false, - } - - normalized := strings.TrimSpace(strings.ToLower(val)) - - if val, ok := truthy[normalized]; ok { - return val - } - if val, ok := falsy[normalized]; ok { - return val - } - - return defaultValue -}