about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlan Pearce2024-06-24 23:29:02 +0200
committerAlan Pearce2024-06-24 23:29:02 +0200
commite4a9203bc0af96c29b40b53d32b20aefb6940e35 (patch)
tree8f4d57ea9dc0863fa1d2fd1be4984aa87ebd4605
parent6b5a99d1de4420a9334ed9b2119b56b138518c1c (diff)
downloadwebsite-e4a9203bc0af96c29b40b53d32b20aefb6940e35.tar.lz
website-e4a9203bc0af96c29b40b53d32b20aefb6940e35.tar.zst
website-e4a9203bc0af96c29b40b53d32b20aefb6940e35.zip
make hostname-based redirection configurable
-rw-r--r--internal/server/server.go17
1 files changed, 10 insertions, 7 deletions
diff --git a/internal/server/server.go b/internal/server/server.go
index 9edebe3..b9db417 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -29,6 +29,7 @@ var (
 type Config struct {
 	Development   bool   `conf:"default:false,flag:dev"`
 	Root          string `conf:"default:website"`
+	Redirect      bool   `conf:"default:false"`
 	ListenAddress string `conf:"default:localhost"`
 	Port          string `conf:"default:3000,short:p"`
 	TLS           bool   `conf:"default:false"`
@@ -132,14 +133,16 @@ func New(runtimeConfig *Config) (*Server, error) {
 		return nil, errors.Wrap(err, "could not create website mux")
 	}
 	log.Debug("binding main handler to", "host", listenAddress)
-	hostname := config.BaseURL.Hostname()
 
-	loggingMux.Handle(hostname+"/", mux)
-
-	loggingMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
-		newURL := config.BaseURL.JoinPath(r.URL.String())
-		http.Redirect(w, r, newURL.String(), 301)
-	})
+	if runtimeConfig.Development || runtimeConfig.Redirect {
+		loggingMux.Handle(config.BaseURL.Hostname()+"/", mux)
+		loggingMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
+			newURL := config.BaseURL.JoinPath(r.URL.String())
+			http.Redirect(w, r, newURL.String(), 301)
+		})
+	} else {
+		loggingMux.Handle("/", mux)
+	}
 
 	top.Handle("/",
 		serverHeaderHandler(