lets-go:6.3 more middleware

This commit is contained in:
tamsin johnson 2024-01-25 15:52:18 -08:00
parent 8678a04350
commit bee34e7405
2 changed files with 16 additions and 1 deletions

View File

@ -4,6 +4,21 @@ import (
"net/http" "net/http"
) )
// logRequest ...
func (app *application) logRequest(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var (
ip = r.RemoteAddr
proto = r.Proto
method = r.Method
uri = r.URL.RequestURI()
)
app.logger.Info("received request", "ip", ip, "proto", proto, "method", method, "uri", uri)
next.ServeHTTP(w, r)
})
}
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) {
w.Header().Set("Content-Security-Policy", w.Header().Set("Content-Security-Policy",

View File

@ -16,5 +16,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 secureHeaders(mux) return app.logRequest(secureHeaders(mux))
} }