all repos — website @ 0fc79f324d54cc7a45c0ad0ffa9c75cd716e0126

My website

server: configure with flags/envvars instead of toml

Alan Pearce
commit

0fc79f324d54cc7a45c0ad0ffa9c75cd716e0126

parent

8dd538daadc55a935e6dc205d9b2559830818c07

1 file changed, 20 insertions(+), 5 deletions(-)

changed files
M server.goserver.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 @@
"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 @@ //go:embed all:public/*
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 @@ prometheus := fiberprometheus.New("homestead")
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,