Compare commits

..

2 Commits

Author SHA1 Message Date
lbenedar
fbce0c006c ch6.3 2026-03-05 13:04:59 +03:00
lbenedar
fc1484aebe ch6.2 2026-03-05 12:54:29 +03:00
2 changed files with 26 additions and 2 deletions

24
cmd/web/middleware.go Normal file
View File

@@ -0,0 +1,24 @@
package main
import "net/http"
func secureHeaders(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Security-Policy",
"default-src 'self'; style-src 'self' fonts.googleapis.com; font-src fonts.gstatic.com")
w.Header().Set("Referrer-Policy", "origin-when-cross-origin")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("X-Frame-Options", "deny")
w.Header().Set("X-XSS-Protection", "0")
next.ServeHTTP(w, r)
})
}
func (app *application) logRequest(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
app.infoLog.Printf("%s - %s %s %s", r.RemoteAddr, r.Proto, r.Method, r.URL.RequestURI())
next.ServeHTTP(w, r)
})
}

View File

@@ -2,7 +2,7 @@ package main
import "net/http"
func (app *application) routes() *http.ServeMux {
func (app *application) routes() http.Handler {
mux := http.NewServeMux()
fileServer := http.FileServer(http.Dir("./ui/static/"))
@@ -12,5 +12,5 @@ func (app *application) routes() *http.ServeMux {
mux.HandleFunc("/snippet/view", app.snippetView)
mux.HandleFunc("/snippet/create", app.snippetCreate)
return mux
return app.logRequest(secureHeaders(mux))
}