feat: replace stdlig flag with pflag by spf13
This commit is contained in:
+2
-2
@@ -32,11 +32,11 @@ func main() {
|
||||
}
|
||||
|
||||
// kick off the watchdog depending on the environment variables
|
||||
if env.WatchdogEnabled {
|
||||
if env.Watchdog {
|
||||
slog.Info("kicking off database watchdog", "watchdog_interval", env.WatchdogInterval)
|
||||
database.KickoffDatabaseWatchdog(env, db)
|
||||
} else {
|
||||
slog.Info("skipping database watchdog", "watchdog_enabled", env.WatchdogEnabled)
|
||||
slog.Info("skipping database watchdog", "watchdog_enabled", env.Watchdog)
|
||||
}
|
||||
|
||||
// TO DO make gin log as json
|
||||
|
||||
@@ -5,6 +5,7 @@ go 1.26.1
|
||||
require (
|
||||
github.com/gin-gonic/gin v1.12.0
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/spf13/pflag v1.0.10
|
||||
gorm.io/driver/sqlite v1.6.0
|
||||
gorm.io/gorm v1.31.1
|
||||
)
|
||||
|
||||
@@ -59,6 +59,8 @@ github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8=
|
||||
github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII=
|
||||
github.com/quic-go/quic-go v0.59.0 h1:OLJkp1Mlm/aS7dpKgTc6cnpynnD2Xg7C1pwL6vy/SAw=
|
||||
github.com/quic-go/quic-go v0.59.0/go.mod h1:upnsH4Ju1YkqpLXC305eW3yDZ4NfnNbmQRCMWS58IKU=
|
||||
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
|
||||
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
|
||||
@@ -24,7 +24,7 @@ func KickoffApi(logger *slog.Logger, env bootstrap.Environment, db *gorm.DB) {
|
||||
r.Use(middleware.SlogMiddleware(logger))
|
||||
r.Use(gin.Recovery())
|
||||
|
||||
if env.AuthenticationEnabled {
|
||||
if env.Authentication {
|
||||
slog.Debug("activating authentication middleware") // only log when actually doign the thing it logs to do
|
||||
r.Use(middleware.AuthMiddleware())
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
||||
flag "github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
type Environment struct {
|
||||
@@ -12,13 +13,13 @@ type Environment struct {
|
||||
Codename string `env:"CODENAME" default:"Magical Anomaly" flag:"codename"`
|
||||
LogLevel string `env:"LOG_LEVEL" default:"debug" flag:"log-level"`
|
||||
|
||||
DataDirectory string `env:"DATA_DIR" default:"./data" flag:"data-dir" usage:"option to specify where the state data gets stored"`
|
||||
ContentDirectory string `env:"CONTENT_DIR" default:"./content" flag:"content-dir" usage:"option to specify where the content gets stored"`
|
||||
Hostname string `env:"HOSTNAME" default:"0.0.0.0" flag:"hostname" usage:"option specify the address/hostname to bind the api server to"`
|
||||
Port int `env:"PORT" default:"8080" flag:"port" usage:"option to specify the port to bind the api server to"`
|
||||
AuthenticationEnabled bool `env:"AUTH_ENABLED" default:"true" flag:"auth" usage:"option to disable authentication"`
|
||||
DataDirectory string `env:"DATA_DIR" default:"./data" flag:"data-dir" usage:"option to specify where the state data gets stored"`
|
||||
ContentDirectory string `env:"CONTENT_DIR" default:"./content" flag:"content-dir" usage:"option to specify where the content gets stored"`
|
||||
Hostname string `env:"HOSTNAME" default:"0.0.0.0" flag:"hostname" usage:"option to specify the address/hostname to bind the api server to"`
|
||||
Port int `env:"PORT" default:"8080" flag:"port" usage:"option to specify the port to bind the api server to"`
|
||||
Authentication bool `env:"AUTHENTICATION" default:"true" flag:"authentication" usage:"option to disable authentication"`
|
||||
|
||||
WatchdogEnabled bool `env:"WATCHDOG_ENABLED" default:"true" flag:"watchdog" usage:"option to disable watchdog"`
|
||||
Watchdog bool `env:"WATCHDOG" default:"true" flag:"watchdog" usage:"option to disable watchdog"`
|
||||
WatchdogInterval int `env:"WATCHDOG_INTERVAL" default:"60" flag:"watchdog-interval" usage:"option to specify the interval in second(s) on which watchdog runs"`
|
||||
WatchdogSyncMode string `env:"WATCHDOG_SYNC_MODE" default:"strict" flag:"watchdog-mode" usage:"option to specify the mode watchdog will run with: strict|sync|dry"`
|
||||
}
|
||||
@@ -37,7 +38,7 @@ func loadFromEnv(env *Environment) {
|
||||
|
||||
// If the variable is not found or is empty
|
||||
raw, ok := os.LookupEnv(key)
|
||||
if !ok || raw == "" {
|
||||
if !ok || len(raw) == 0 {
|
||||
raw = fallback
|
||||
}
|
||||
|
||||
@@ -68,17 +69,19 @@ func bindFlags(env *Environment) {
|
||||
|
||||
flagName := field.Tag.Get("flag")
|
||||
flagUsage := field.Tag.Get("usage")
|
||||
if flagName == "" {
|
||||
if len(flagName) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
switch valueField.Kind() {
|
||||
case reflect.String:
|
||||
flag.StringVar(valueField.Addr().Interface().(*string), flagName,
|
||||
flag.StringVar(
|
||||
valueField.Addr().Interface().(*string), flagName,
|
||||
valueField.String(), flagUsage,
|
||||
)
|
||||
case reflect.Int:
|
||||
flag.IntVar(valueField.Addr().Interface().(*int), flagName,
|
||||
flag.IntVar(
|
||||
valueField.Addr().Interface().(*int), flagName,
|
||||
int(valueField.Int()), flagUsage,
|
||||
)
|
||||
case reflect.Bool:
|
||||
|
||||
Reference in New Issue
Block a user