about summary refs log tree commit diff stats
path: root/server.go
diff options
context:
space:
mode:
authorAlan Pearce2024-04-20 19:31:21 +0200
committerAlan Pearce2024-04-20 19:31:21 +0200
commit9b3d6d55246912efe01f09d2f01cbe17a5ef1211 (patch)
tree266862333d506bc85186c9e4058302623eedbae1 /server.go
parentcc2245f991ca0474e3d4db9fa213be4aa8ccccb7 (diff)
downloadwebsite-9b3d6d55246912efe01f09d2f01cbe17a5ef1211.tar.lz
website-9b3d6d55246912efe01f09d2f01cbe17a5ef1211.tar.zst
website-9b3d6d55246912efe01f09d2f01cbe17a5ef1211.zip
send headers from config.toml
Diffstat (limited to 'server.go')
-rw-r--r--server.go35
1 files changed, 24 insertions, 11 deletions
diff --git a/server.go b/server.go
index 69dca43..d107028 100644
--- a/server.go
+++ b/server.go
@@ -49,16 +49,20 @@ var fs embed.FS
 var Commit string
 
 func main() {
-	config := Config{}
-	if help, err := conf.Parse("", &config); err != nil {
+	runtimeConfig := Config{}
+	if help, err := conf.Parse("", &runtimeConfig); err != nil {
 		if errors.Is(err, conf.ErrHelpWanted) {
 			fmt.Println(help)
 			os.Exit(1)
 		}
-		log.Panicf("parsing config: %v", err)
+		log.Panicf("parsing runtime configuration: %v", err)
+	}
+	config, err := cfg.GetConfig("config.toml")
+	if err != nil {
+		log.Panicf("parsing configuration file: %v", err)
 	}
 
-	err := sentry.Init(sentry.ClientOptions{
+	err = sentry.Init(sentry.ClientOptions{
 		Dsn:         os.Getenv("SENTRY_DSN"),
 		Release:     os.Getenv("FLY_MACHINE_VERSION"),
 		Environment: os.Getenv("ENV"),
@@ -100,6 +104,15 @@ func main() {
 
 	website.Use(prometheus.Middleware)
 	website.Use(fibersentry.New(fibersentry.Config{}))
+	website.Use(func(c *fiber.Ctx) error {
+		for k, v := range config.Extra.Headers {
+			c.Set(k, v)
+		}
+		if c.Secure() {
+			c.Set("Strict-Transport-Security", "max-age=31536000; includeSubdomains; preload")
+		}
+		return c.Next()
+	})
 
 	website.Use(compress.New())
 	website.Use(cache.New(cache.Config{
@@ -132,17 +145,17 @@ func main() {
 		MaxAge:             int((24 * time.Hour).Seconds()),
 	}))
 	website.Use(notFoundHandler)
-	hosts[config.BaseURL.Host] = &Host{website}
+	hosts[runtimeConfig.BaseURL.Host] = &Host{website}
 
 	toplevel := fiber.New(fiber.Config{
-		DisableStartupMessage: config.Production,
+		DisableStartupMessage: runtimeConfig.Production,
 		ServerHeader:          fmt.Sprintf("website (%s)", Commit),
 	})
 	toplevel.Get("/health", func(c *fiber.Ctx) error {
 		return c.SendStatus(fiber.StatusOK)
 	})
 	var logWriter io.Writer
-	if config.Production {
+	if runtimeConfig.Production {
 		logWriter = law.NewWriteAsyncer(os.Stdout, nil)
 	} else {
 		logWriter = os.Stdout
@@ -156,10 +169,10 @@ func main() {
 	toplevel.Use(func(c *fiber.Ctx) error {
 		host := hosts[c.Hostname()]
 		if host == nil {
-			if config.RedirectOtherHostnames {
-				return c.Redirect(config.BaseURL.JoinPath(c.OriginalURL()).String())
+			if runtimeConfig.RedirectOtherHostnames {
+				return c.Redirect(runtimeConfig.BaseURL.JoinPath(c.OriginalURL()).String())
 			} else {
-				hosts[config.BaseURL.Host].Fiber.Handler()(c.Context())
+				hosts[runtimeConfig.BaseURL.Host].Fiber.Handler()(c.Context())
 				return nil
 			}
 		} else {
@@ -172,5 +185,5 @@ func main() {
 		err := metricServer.Listen(":9091")
 		log.Printf("failed to start metrics server: %v", err)
 	}()
-	log.Fatal(toplevel.Listen(fmt.Sprintf("%s:%d", "", config.Port)))
+	log.Fatal(toplevel.Listen(fmt.Sprintf("%s:%d", "", runtimeConfig.Port)))
 }