diff options
-rw-r--r-- | internal/config/config.go | 17 |
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 { |