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
This commit is contained in:
Nicola Murino 2020-12-20 10:22:16 +01:00
parent 1ac610da1a
commit 743b350fdd
No known key found for this signature in database
GPG key ID: 2F1FB59433D5A8CB
2 changed files with 3 additions and 4 deletions

View file

@ -22,6 +22,8 @@ func GetHTTPRouter() http.Handler {
func initializeRouter(staticFilesPath string, enableWebAdmin bool) { func initializeRouter(staticFilesPath string, enableWebAdmin bool) {
router = chi.NewRouter() router = chi.NewRouter()
router.Use(middleware.GetHead)
router.Group(func(r chi.Router) { router.Group(func(r chi.Router) {
r.Get("/healthz", func(w http.ResponseWriter, r *http.Request) { r.Get("/healthz", func(w http.ResponseWriter, r *http.Request) {
render.PlainText(w, r, "ok") render.PlainText(w, r, "ok")
@ -38,10 +40,6 @@ func initializeRouter(staticFilesPath string, enableWebAdmin bool) {
sendAPIResponse(w, r, nil, "Not Found", http.StatusNotFound) 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) { router.Get("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, webUsersPath, http.StatusMovedPermanently) http.Redirect(w, r, webUsersPath, http.StatusMovedPermanently)
}) })

View file

@ -15,6 +15,7 @@ import (
func initializeRouter(enableProfiler bool) { func initializeRouter(enableProfiler bool) {
router = chi.NewRouter() router = chi.NewRouter()
router.Use(middleware.GetHead)
router.Use(middleware.Recoverer) router.Use(middleware.Recoverer)
router.Group(func(r chi.Router) { router.Group(func(r chi.Router) {