This commit is contained in:
lbenedar
2026-03-05 13:20:01 +03:00
parent fbce0c006c
commit be06d0a5ca
2 changed files with 18 additions and 2 deletions

View File

@@ -1,6 +1,9 @@
package main package main
import "net/http" import (
"fmt"
"net/http"
)
func secureHeaders(next http.Handler) http.Handler { func secureHeaders(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -22,3 +25,16 @@ func (app *application) logRequest(next http.Handler) http.Handler {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}) })
} }
func (app *application) recoverPanic(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
w.Header().Set("Connection", "close")
app.serverError(w, fmt.Errorf("%s", err))
}
}()
next.ServeHTTP(w, r)
})
}

View File

@@ -12,5 +12,5 @@ func (app *application) routes() http.Handler {
mux.HandleFunc("/snippet/view", app.snippetView) mux.HandleFunc("/snippet/view", app.snippetView)
mux.HandleFunc("/snippet/create", app.snippetCreate) mux.HandleFunc("/snippet/create", app.snippetCreate)
return app.logRequest(secureHeaders(mux)) return app.recoverPanic(app.logRequest(secureHeaders(mux)))
} }