From 0fc79f324d54cc7a45c0ad0ffa9c75cd716e0126 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Fri, 19 Apr 2024 14:53:51 +0200 Subject: server: configure with flags/envvars instead of toml --- flake.nix | 4 ++-- go.mod | 1 + go.sum | 3 +++ server.go | 25 ++++++++++++++++++++----- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/flake.nix b/flake.nix index cd1e86d..4d78ce1 100644 --- a/flake.nix +++ b/flake.nix @@ -34,10 +34,10 @@ gci flyctl (writeShellScriptBin "watch-builder" '' - ${pkgs.watchexec}/bin/watchexec -r -w flake.nix --shell fish 'direnv exec . watchexec -i server.go -i public -r go run ./cmd/build' + ${pkgs.watchexec}/bin/watchexec -r -w flake.nix --shell fish "direnv exec . watchexec -i server.go -i public -r go run ./cmd/build $@" '') (writeShellScriptBin "watch-server" '' - ${pkgs.watchexec}/bin/watchexec -r -w flake.nix --shell fish 'direnv exec . watchexec -r go run ./server.go' + ${pkgs.watchexec}/bin/watchexec -r -w flake.nix --shell fish "direnv exec . watchexec -r go run ./server.go $@" '') (writeShellScriptBin "check-licenses" '' ${pkgs.go-licenses}/bin/go-licenses check --include_tests ./... --disallowed_types=restricted,forbidden diff --git a/go.mod b/go.mod index a6e3e85..8f93f87 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/ansrivas/fiberprometheus/v2 v2.6.1 github.com/antchfx/xmlquery v1.4.0 github.com/antchfx/xpath v1.3.0 + github.com/ardanlabs/conf/v3 v3.1.7 github.com/deckarep/golang-set/v2 v2.6.0 github.com/getsentry/sentry-go v0.27.0 github.com/gofiber/contrib/fibersentry v1.0.4 diff --git a/go.sum b/go.sum index 6d1176c..4f728bc 100644 --- a/go.sum +++ b/go.sum @@ -17,6 +17,8 @@ github.com/antchfx/xmlquery v1.4.0 h1:xg2HkfcRK2TeTbdb0m1jxCYnvsPaGY/oeZWTGqX/0h github.com/antchfx/xmlquery v1.4.0/go.mod h1:Ax2aeaeDjfIw3CwXKDQ0GkwZ6QlxoChlIBP+mGnDFjI= github.com/antchfx/xpath v1.3.0 h1:nTMlzGAK3IJ0bPpME2urTuFL76o4A96iYvoKFHRXJgc= github.com/antchfx/xpath v1.3.0/go.mod h1:i54GszH55fYfBmoZXapTHN8T8tkcHfRgLyVwwqzXNcs= +github.com/ardanlabs/conf/v3 v3.1.7 h1:p232cF68TafoA5U9ZlbxUIhGJtGNdKHBXF80Fdqb5t0= +github.com/ardanlabs/conf/v3 v3.1.7/go.mod h1:zclexWKe0NVj6LHQ8NgDDZ7bQ1spE0KeKPFficdtAjU= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= @@ -41,6 +43,7 @@ github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= diff --git a/server.go b/server.go index 9238c73..b34c933 100644 --- a/server.go +++ b/server.go @@ -2,15 +2,18 @@ package main import ( "embed" + "errors" "fmt" "io" "log" "net/http" "os" "time" - "website/internal/config" + + cfg "website/internal/config" "github.com/ansrivas/fiberprometheus/v2" + "github.com/ardanlabs/conf/v3" "github.com/getsentry/sentry-go" "github.com/gofiber/contrib/fibersentry" "github.com/gofiber/fiber/v2" @@ -26,6 +29,13 @@ import ( "github.com/shengyanli1982/law" ) +type Config struct { + Production bool `conf:"default:false"` + Port uint16 `conf:"default:3000,short:p"` + BaseURL cfg.URL `conf:"default:http://localhost:3000,short:b"` + RedirectOtherHostnames bool `conf:"default:false"` +} + // TODO purge CSS // TODO HTTP2 https://github.com/dgrr/http2 @@ -37,6 +47,15 @@ type Host struct { var fs embed.FS func main() { + config := Config{} + if help, err := conf.Parse("", &config); err != nil { + if errors.Is(err, conf.ErrHelpWanted) { + fmt.Println(help) + os.Exit(1) + } + log.Panicf("parsing config: %v", err) + } + err := sentry.Init(sentry.ClientOptions{ Dsn: os.Getenv("SENTRY_DSN"), Release: os.Getenv("FLY_MACHINE_VERSION"), @@ -58,10 +77,6 @@ func main() { prometheus.RegisterAt(metricServer, "/metrics") hosts := map[string]*Host{} - config, err := config.GetConfig() - if err != nil { - log.Panic("config error", err) - } internal := fiber.New(fiber.Config{ GETOnly: true, -- cgit 1.4.1