about summary refs log tree commit diff stats
path: root/server.go
diff options
context:
space:
mode:
authorAlan Pearce2024-04-19 14:53:51 +0200
committerAlan Pearce2024-04-19 14:54:05 +0200
commit0fc79f324d54cc7a45c0ad0ffa9c75cd716e0126 (patch)
tree135793af7cdd7162454d8dd1a4a8e6ad553193d4 /server.go
parent8dd538daadc55a935e6dc205d9b2559830818c07 (diff)
downloadwebsite-0fc79f324d54cc7a45c0ad0ffa9c75cd716e0126.tar.lz
website-0fc79f324d54cc7a45c0ad0ffa9c75cd716e0126.tar.zst
website-0fc79f324d54cc7a45c0ad0ffa9c75cd716e0126.zip
server: configure with flags/envvars instead of toml
Diffstat (limited to 'server.go')
-rw-r--r--server.go25
1 files changed, 20 insertions, 5 deletions
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,