From 743b350fddb3c70f6a681dc36c1fea8e0f5399fb Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Sun, 20 Dec 2020 10:22:16 +0100 Subject: [PATCH] httpd: add support for route undefined HEAD requests to GET handlers HEAD responses will not include a body but the Content-Length will be set as the equivalent GET request Fixes #255 --- httpd/router.go | 6 ++---- telemetry/router.go | 1 + 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/httpd/router.go b/httpd/router.go index e66c9a30..c62c2313 100644 --- a/httpd/router.go +++ b/httpd/router.go @@ -22,6 +22,8 @@ func GetHTTPRouter() http.Handler { func initializeRouter(staticFilesPath string, enableWebAdmin bool) { router = chi.NewRouter() + router.Use(middleware.GetHead) + router.Group(func(r chi.Router) { r.Get("/healthz", func(w http.ResponseWriter, r *http.Request) { render.PlainText(w, r, "ok") @@ -38,10 +40,6 @@ func initializeRouter(staticFilesPath string, enableWebAdmin bool) { sendAPIResponse(w, r, nil, "Not Found", http.StatusNotFound) })) - router.MethodNotAllowed(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - sendAPIResponse(w, r, nil, "Method not allowed", http.StatusMethodNotAllowed) - })) - router.Get("/", func(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, webUsersPath, http.StatusMovedPermanently) }) diff --git a/telemetry/router.go b/telemetry/router.go index 0781a6e2..9712f301 100644 --- a/telemetry/router.go +++ b/telemetry/router.go @@ -15,6 +15,7 @@ import ( func initializeRouter(enableProfiler bool) { router = chi.NewRouter() + router.Use(middleware.GetHead) router.Use(middleware.Recoverer) router.Group(func(r chi.Router) {