diff options
Diffstat (limited to 'internal/server/server.go')
-rw-r--r-- | internal/server/server.go | 32 |
1 files changed, 23 insertions, 9 deletions
diff --git a/internal/server/server.go b/internal/server/server.go index b174c0c..269ed9e 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -8,6 +8,7 @@ import ( "net/url" "os" "path/filepath" + "regexp" "slices" "strconv" "strings" @@ -74,7 +75,6 @@ func applyDevModeOverrides(config *cfg.Config, runtimeConfig *Config) { } func updateCSPHashes(config *cfg.Config, r *builder.Result) { - clear(config.CSP.StyleSrc) for i, h := range r.Hashes { config.CSP.StyleSrc[i] = fmt.Sprintf("'%s'", h) } @@ -184,21 +184,35 @@ func New(runtimeConfig *Config, log *log.Logger) (*Server, error) { } if runtimeConfig.Redirect { + re := regexp.MustCompile( + "^(.*)\\." + strings.ReplaceAll(config.WildcardDomain, ".", `\.`) + "$", + ) + replace := "${1}." + config.Domains[0] loggingMux.Handle(config.BaseURL.Hostname()+"/", mux) loggingMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - path, _ := website.CanonicalisePath(r.URL.Path) - newURL := config.BaseURL.JoinPath(path) - http.Redirect(w, r, newURL.String(), http.StatusMovedPermanently) + if slices.Contains(config.Domains, r.Host) { + path, _ := website.CanonicalisePath(r.URL.Path) + newURL := config.BaseURL.JoinPath(path) + http.Redirect(w, r, newURL.String(), http.StatusMovedPermanently) + } else { + url := config.BaseURL + url.Host = re.ReplaceAllString(r.Host, replace) + http.Redirect(w, r, url.String(), http.StatusTemporaryRedirect) + } }) } else { loggingMux.Handle("/", mux) } - top.Handle("/", - serverHeaderHandler( - wrapHandlerWithLogging(loggingMux, log), - ), - ) + if runtimeConfig.Development { + top.Handle("/", + serverHeaderHandler( + wrapHandlerWithLogging(loggingMux, log), + ), + ) + } else { + top.Handle("/", serverHeaderHandler(loggingMux)) + } top.HandleFunc("/health", func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusNoContent) |