migrated out env module and moved config to internal

This commit is contained in:
2026-01-02 19:12:42 +11:00
parent 6dd80ee7b6
commit 1bcdf0e813
13 changed files with 30 additions and 120 deletions

View File

@@ -7,8 +7,8 @@ import (
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
"projectreshoot/internal/config"
"projectreshoot/internal/httpserver" "projectreshoot/internal/httpserver"
"projectreshoot/pkg/config"
"projectreshoot/pkg/embedfs" "projectreshoot/pkg/embedfs"
"sync" "sync"
"time" "time"

1
go.mod
View File

@@ -4,6 +4,7 @@ go 1.25.5
require ( require (
git.haelnorr.com/h/golib/cookies v0.9.0 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/hlog v0.9.0
git.haelnorr.com/h/golib/jwt v0.9.2 git.haelnorr.com/h/golib/jwt v0.9.2
git.haelnorr.com/h/golib/tmdb v0.8.0 git.haelnorr.com/h/golib/tmdb v0.8.0

2
go.sum
View File

@@ -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 h1:Vf+eX1prHkKuGrQon1BHY87yaPc1H+HJFRXDOV/AuWs=
git.haelnorr.com/h/golib/cookies v0.9.0/go.mod h1:y1385YExI9gLwckCVDCYVcsFXr6N7T3brJjnJD2QIuo= 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 h1:ib8n2MdmiRK2TF067p220kXmhDe9aAnlcsgpuv+QpvE=
git.haelnorr.com/h/golib/hlog v0.9.0/go.mod h1:oOlzb8UVHUYP1k7dN5PSJXVskAB2z8EYgRN85jAi0Zk= 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= git.haelnorr.com/h/golib/jwt v0.9.2 h1:l1Ow7DPGACAU54CnMP/NlZjdc4nRD1wr3xZ8a7taRvU=

View File

@@ -5,6 +5,7 @@ import (
"os" "os"
"time" "time"
"git.haelnorr.com/h/golib/env"
"git.haelnorr.com/h/golib/hlog" "git.haelnorr.com/h/golib/hlog"
"git.haelnorr.com/h/golib/tmdb" "git.haelnorr.com/h/golib/tmdb"
"github.com/joho/godotenv" "github.com/joho/godotenv"
@@ -47,17 +48,17 @@ func GetConfig(args map[string]string) (*Config, error) {
if args["host"] != "" { if args["host"] != "" {
host = args["host"] host = args["host"]
} else { } else {
host = GetEnvDefault("HOST", "127.0.0.1") host = env.String("HOST", "127.0.0.1")
} }
if args["port"] != "" { if args["port"] != "" {
port = args["port"] port = args["port"]
} else { } else {
port = GetEnvDefault("PORT", "3010") port = env.String("PORT", "3010")
} }
if args["loglevel"] != "" { if args["loglevel"] != "" {
logLevel = hlog.LogLevel(args["loglevel"]) logLevel = hlog.LogLevel(args["loglevel"])
} else { } else {
logLevel = hlog.LogLevel(GetEnvDefault("LOG_LEVEL", "info")) logLevel = hlog.LogLevel(env.String("LOG_LEVEL", "info"))
} }
if args["logoutput"] != "" { if args["logoutput"] != "" {
opts := map[string]string{ opts := map[string]string{
@@ -73,7 +74,7 @@ func GetConfig(args map[string]string) (*Config, error) {
) )
} }
} else { } else {
logOutput = GetEnvDefault("LOG_OUTPUT", "console") logOutput = env.String("LOG_OUTPUT", "console")
} }
if logOutput != "both" && logOutput != "console" && logOutput != "file" { if logOutput != "both" && logOutput != "console" && logOutput != "file" {
logOutput = "console" logOutput = "console"
@@ -86,22 +87,22 @@ func GetConfig(args map[string]string) (*Config, error) {
config := &Config{ config := &Config{
Host: host, Host: host,
Port: port, Port: port,
TrustedHost: GetEnvDefault("TRUSTED_HOST", "127.0.0.1"), TrustedHost: env.String("TRUSTED_HOST", "127.0.0.1"),
SSL: GetEnvBool("SSL_MODE", false), SSL: env.Bool("SSL_MODE", false),
GZIP: GetEnvBool("GZIP", false), GZIP: env.Bool("GZIP", false),
ReadHeaderTimeout: GetEnvDur("READ_HEADER_TIMEOUT", 2), ReadHeaderTimeout: env.Duration("READ_HEADER_TIMEOUT", 2),
WriteTimeout: GetEnvDur("WRITE_TIMEOUT", 10), WriteTimeout: env.Duration("WRITE_TIMEOUT", 10),
IdleTimeout: GetEnvDur("IDLE_TIMEOUT", 120), IdleTimeout: env.Duration("IDLE_TIMEOUT", 120),
DBName: "00001", DBName: "00001",
DBLockTimeout: GetEnvDur("DB_LOCK_TIMEOUT", 60), DBLockTimeout: env.Duration("DB_LOCK_TIMEOUT", 60),
SecretKey: os.Getenv("SECRET_KEY"), SecretKey: env.String("SECRET_KEY", ""),
AccessTokenExpiry: GetEnvInt64("ACCESS_TOKEN_EXPIRY", 5), AccessTokenExpiry: env.Int64("ACCESS_TOKEN_EXPIRY", 5),
RefreshTokenExpiry: GetEnvInt64("REFRESH_TOKEN_EXPIRY", 1440), // defaults to 1 day RefreshTokenExpiry: env.Int64("REFRESH_TOKEN_EXPIRY", 1440), // defaults to 1 day
TokenFreshTime: GetEnvInt64("TOKEN_FRESH_TIME", 5), TokenFreshTime: env.Int64("TOKEN_FRESH_TIME", 5),
LogLevel: logLevel, LogLevel: logLevel,
LogOutput: logOutput, LogOutput: logOutput,
LogDir: GetEnvDefault("LOG_DIR", ""), LogDir: env.String("LOG_DIR", ""),
TMDBToken: os.Getenv("TMDB_API_TOKEN"), TMDBToken: env.String("TMDB_API_TOKEN", ""),
TMDBConfig: tmdbcfg, TMDBConfig: tmdbcfg,
} }

View File

@@ -6,10 +6,10 @@ import (
"net/http" "net/http"
"time" "time"
"projectreshoot/internal/config"
"projectreshoot/internal/models" "projectreshoot/internal/models"
"projectreshoot/internal/view/component/form" "projectreshoot/internal/view/component/form"
"projectreshoot/internal/view/page" "projectreshoot/internal/view/page"
"projectreshoot/pkg/config"
"git.haelnorr.com/h/golib/cookies" "git.haelnorr.com/h/golib/cookies"
"git.haelnorr.com/h/golib/hlog" "git.haelnorr.com/h/golib/hlog"

View File

@@ -2,8 +2,8 @@ package handler
import ( import (
"net/http" "net/http"
"projectreshoot/internal/config"
"projectreshoot/internal/view/page" "projectreshoot/internal/view/page"
"projectreshoot/pkg/config"
"strconv" "strconv"
"git.haelnorr.com/h/golib/hlog" "git.haelnorr.com/h/golib/hlog"

View File

@@ -2,9 +2,9 @@ package handler
import ( import (
"net/http" "net/http"
"projectreshoot/internal/config"
"projectreshoot/internal/view/component/search" "projectreshoot/internal/view/component/search"
"projectreshoot/internal/view/page" "projectreshoot/internal/view/page"
"projectreshoot/pkg/config"
"git.haelnorr.com/h/golib/hlog" "git.haelnorr.com/h/golib/hlog"
"git.haelnorr.com/h/golib/tmdb" "git.haelnorr.com/h/golib/tmdb"

View File

@@ -6,8 +6,8 @@ import (
"net/http" "net/http"
"time" "time"
"projectreshoot/internal/config"
"projectreshoot/internal/view/component/form" "projectreshoot/internal/view/component/form"
"projectreshoot/pkg/config"
"projectreshoot/pkg/contexts" "projectreshoot/pkg/contexts"
"git.haelnorr.com/h/golib/hlog" "git.haelnorr.com/h/golib/hlog"

View File

@@ -6,10 +6,10 @@ import (
"net/http" "net/http"
"time" "time"
"projectreshoot/internal/config"
"projectreshoot/internal/models" "projectreshoot/internal/models"
"projectreshoot/internal/view/component/form" "projectreshoot/internal/view/component/form"
"projectreshoot/internal/view/page" "projectreshoot/internal/view/page"
"projectreshoot/pkg/config"
"git.haelnorr.com/h/golib/cookies" "git.haelnorr.com/h/golib/cookies"
"git.haelnorr.com/h/golib/hlog" "git.haelnorr.com/h/golib/hlog"

View File

@@ -4,10 +4,10 @@ import (
"database/sql" "database/sql"
"net/http" "net/http"
"projectreshoot/internal/config"
"projectreshoot/internal/handler" "projectreshoot/internal/handler"
"projectreshoot/internal/middleware" "projectreshoot/internal/middleware"
"projectreshoot/internal/view/page" "projectreshoot/internal/view/page"
"projectreshoot/pkg/config"
"git.haelnorr.com/h/golib/hlog" "git.haelnorr.com/h/golib/hlog"
"git.haelnorr.com/h/golib/jwt" "git.haelnorr.com/h/golib/jwt"

View File

@@ -7,8 +7,8 @@ import (
"net/http" "net/http"
"time" "time"
"projectreshoot/internal/config"
"projectreshoot/internal/middleware" "projectreshoot/internal/middleware"
"projectreshoot/pkg/config"
"git.haelnorr.com/h/golib/hlog" "git.haelnorr.com/h/golib/hlog"
"git.haelnorr.com/h/golib/jwt" "git.haelnorr.com/h/golib/jwt"

View File

@@ -7,9 +7,9 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"projectreshoot/internal/config"
"projectreshoot/internal/handler" "projectreshoot/internal/handler"
"projectreshoot/internal/models" "projectreshoot/internal/models"
"projectreshoot/pkg/config"
"projectreshoot/pkg/contexts" "projectreshoot/pkg/contexts"
"git.haelnorr.com/h/golib/cookies" "git.haelnorr.com/h/golib/cookies"

View File

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