about summary refs log tree commit diff stats
path: root/internal/config/config.go
diff options
context:
space:
mode:
authorAlan Pearce2024-04-17 19:53:46 +0200
committerAlan Pearce2024-04-17 20:53:30 +0200
commit62d0aaac788301aabf3470c5f306211529e6378f (patch)
treecee2f70a9925c18b8792b0ee4d9476db14582860 /internal/config/config.go
parent6f6057fd56aa6d065ec59341b08cffc03619b21d (diff)
downloadwebsite-62d0aaac788301aabf3470c5f306211529e6378f.tar.lz
website-62d0aaac788301aabf3470c5f306211529e6378f.tar.zst
website-62d0aaac788301aabf3470c5f306211529e6378f.zip
config: add port (via environment variable)
Diffstat (limited to 'internal/config/config.go')
-rw-r--r--internal/config/config.go17
1 files changed, 17 insertions, 0 deletions
diff --git a/internal/config/config.go b/internal/config/config.go
index 66e3d82..96f721e 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -1,9 +1,12 @@
 package config
 
 import (
+	"fmt"
 	"io/fs"
 	"log/slog"
 	"net/url"
+	"os"
+	"strconv"
 
 	"github.com/BurntSushi/toml"
 	"github.com/pkg/errors"
@@ -32,6 +35,7 @@ type Config struct {
 	DefaultLanguage        string `toml:"default_language"`
 	BaseURL                URL    `toml:"base_url"`
 	RedirectOtherHostnames bool   `toml:"redirect_other_hostnames"`
+	Port                   uint64
 	Title                  string
 	Email                  string
 	Description            string
@@ -44,6 +48,14 @@ type Config struct {
 	Menus map[string][]MenuItem
 }
 
+func getEnvFallback(key, fallback string) string {
+	if value, ok := os.LookupEnv(key); ok {
+		return value
+	} else {
+		return fallback
+	}
+}
+
 func setDevelopmentOverrides(config *Config) error {
 	overrideURL, err := URL.Parse(config.BaseURL, "http://localhost:"+fmt.Sprint(config.Port))
 	if err != nil {
@@ -68,6 +80,11 @@ func GetConfig() (*Config, error) {
 			return nil, errors.Wrap(err, "config error")
 		}
 	}
+	port, err := strconv.ParseUint(getEnvFallback("PORT", "3000"), 10, 16)
+	if err != nil {
+		return nil, err
+	}
+	config.Port = port
 	if os.Getenv("ENV") != "production" {
 		err = setDevelopmentOverrides(&config)
 		if err != nil {