From 62d0aaac788301aabf3470c5f306211529e6378f Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Wed, 17 Apr 2024 19:53:46 +0200 Subject: config: add port (via environment variable) --- internal/config/config.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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 { -- cgit 1.4.1