From 3674f74cbf1e3289303487e086adfff8f1f15cfa Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Fri, 24 May 2024 23:34:36 +0200 Subject: fix redirection to wrong hostname --- internal/server/server.go | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'internal') diff --git a/internal/server/server.go b/internal/server/server.go index c72368c..11747ea 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -27,12 +27,11 @@ var ( ) type Config struct { - Production bool `conf:"default:false"` - InDevServer bool `conf:"default:false"` - Root string `conf:"default:website"` - ListenAddress string `conf:"default:localhost"` - Port string `conf:"default:3000,short:p"` - BaseURL cfg.URL `conf:"default:http://localhost:3000,short:b"` + Production bool `conf:"default:false"` + InDevServer bool `conf:"default:false"` + Root string `conf:"default:website"` + ListenAddress string `conf:"default:localhost"` + Port string `conf:"default:3000,short:p"` } type Server struct { @@ -61,6 +60,8 @@ func New(runtimeConfig *Config) (*Server, error) { applyDevModeOverrides(config) } + listenAddress := net.JoinHostPort(runtimeConfig.ListenAddress, runtimeConfig.Port) + env := "development" if runtimeConfig.Production { env = "production" @@ -85,13 +86,13 @@ func New(runtimeConfig *Config) (*Server, error) { if err != nil { return nil, errors.Wrap(err, "could not create website mux") } - log.Debug("binding main handler to", "host", runtimeConfig.BaseURL.Hostname()+"/") - hostname := runtimeConfig.BaseURL.Hostname() + log.Debug("binding main handler to", "host", listenAddress) + hostname := config.BaseURL.Hostname() top.Handle(hostname+"/", mux) top.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - newURL := runtimeConfig.BaseURL.JoinPath(r.URL.String()) + newURL := config.BaseURL.JoinPath(r.URL.String()) http.Redirect(w, r, newURL.String(), 301) }) @@ -99,8 +100,6 @@ func New(runtimeConfig *Config) (*Server, error) { w.WriteHeader(http.StatusNoContent) }) - listenAddress := net.JoinHostPort(runtimeConfig.ListenAddress, runtimeConfig.Port) - return &Server{ &http.Server{ Addr: listenAddress, -- cgit 1.4.1