I am writing a small website and for every page, I am putting a server name to its header:
func httpSignUp(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("Server", SERVER_NAME)
}
I am wondering if there's a way that I can set http.ResponseWriter's default server name, so I don't have to use the same line over and over?
Create a wrapper to set the header:
func wrap(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (
w.Header().Set("Server", SERVER_NAME)
h.ServeHTTP(w, r)
})
}
Wrap individual handlers
http.Handle("/path", wrap(aHandler)(
http.Handle("/another/path", wrap(anotherHandler))
or the root handler passed to ListenAndServe:
log.Fatal(http.ListenAndServe(addr, wrap(rootHandler))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With