all repos — website @ 6f6057fd56aa6d065ec59341b08cffc03619b21d

My website

config: override base_url in development
Alan Pearce alan@alanpearce.eu
Wed, 17 Apr 2024 19:53:22 +0200
commit

6f6057fd56aa6d065ec59341b08cffc03619b21d

parent

37dabe7476a9ed35a95d42ae32e3064fccbbea2d

1 files changed, 15 insertions(+), 0 deletions(-)

jump to
M internal/config/config.gointernal/config/config.go
@@ -44,6 +44,15 @@ } 	Menus map[string][]MenuItem
 }
 
+func setDevelopmentOverrides(config *Config) error {
+	overrideURL, err := URL.Parse(config.BaseURL, "http://localhost:"+fmt.Sprint(config.Port))
+	if err != nil {
+		return err
+	}
+	config.BaseURL.URL = overrideURL
+	return nil
+}
+
 func GetConfig() (*Config, error) {
 	config := Config{}
 	slog.Debug("reading config.toml")
@@ -57,6 +66,12 @@ } else if errors.As(err, &tomlError) { 			return nil, errors.WithMessage(err, tomlError.ErrorWithUsage())
 		} else {
 			return nil, errors.Wrap(err, "config error")
+		}
+	}
+	if os.Getenv("ENV") != "production" {
+		err = setDevelopmentOverrides(&config)
+		if err != nil {
+			return nil, errors.WithMessage(err, "could not override configuration")
 		}
 	}
 	return &config, nil