all repos — homestead @ 08a32da972830cdd745e970b2a47f8365ddf15f7

Code for my website

config: add port (via environment variable)

Alan Pearce
commit

08a32da972830cdd745e970b2a47f8365ddf15f7

parent

a49b53146873f980e1e9dd17888bca159605a827

1 file changed, 17 insertions(+), 0 deletions(-)

jump to
M internal/config/config.gointernal/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 @@ }
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 @@ } else {
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 {