about summary refs log tree commit diff stats
path: root/internal/server/server.go
diff options
context:
space:
mode:
authorAlan Pearce2024-06-29 19:33:16 +0200
committerAlan Pearce2024-06-29 19:33:16 +0200
commit0e50089ea4b4bbd89c58c379b252baa2d09853ed (patch)
treef07655896d75c33e31e2772b75725bf3980d258c /internal/server/server.go
parentf73b8d6770b0e3c3f6ccb50983083c2a4dc81a5f (diff)
downloadwebsite-0e50089ea4b4bbd89c58c379b252baa2d09853ed.tar.lz
website-0e50089ea4b4bbd89c58c379b252baa2d09853ed.tar.zst
website-0e50089ea4b4bbd89c58c379b252baa2d09853ed.zip
set timeouts for secondary (http) server
Diffstat (limited to 'internal/server/server.go')
-rw-r--r--internal/server/server.go26
1 files changed, 17 insertions, 9 deletions
diff --git a/internal/server/server.go b/internal/server/server.go
index 6f933ef..dfb0f8c 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -46,9 +46,9 @@ type Config struct {
 
 type Server struct {
 	*http.Server
-	redirectHandler func(http.ResponseWriter, *http.Request)
-	runtimeConfig   *Config
-	config          *cfg.Config
+	redirectServer *http.Server
+	runtimeConfig  *Config
+	config         *cfg.Config
 }
 
 func applyDevModeOverrides(config *cfg.Config, runtimeConfig *Config) {
@@ -178,14 +178,15 @@ func New(runtimeConfig *Config) (*Server, error) {
 		return nil, errors.Wrap(err, "could not create website mux")
 	}
 
-	redirectHandler := func(w http.ResponseWriter, r *http.Request) {
+	rMux := http.NewServeMux()
+	rMux.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(), 301)
-	}
+	})
 	if runtimeConfig.Redirect {
 		loggingMux.Handle(config.BaseURL.Hostname()+"/", mux)
-		loggingMux.HandleFunc("/", redirectHandler)
+		loggingMux.Handle("/", rMux)
 	} else {
 		loggingMux.Handle("/", mux)
 	}
@@ -209,9 +210,16 @@ func New(runtimeConfig *Config) (*Server, error) {
 			Addr:              listenAddress,
 			Handler:           top,
 		},
-		redirectHandler: redirectHandler,
-		config:          config,
-		runtimeConfig:   runtimeConfig,
+		redirectServer: &http.Server{
+			ReadHeaderTimeout: 10 * time.Second,
+			ReadTimeout:       1 * time.Minute,
+			WriteTimeout:      2 * time.Minute,
+			IdleTimeout:       10 * time.Minute,
+			Addr:              listenAddress,
+			Handler:           rMux,
+		},
+		config:        config,
+		runtimeConfig: runtimeConfig,
 	}, nil
 }