gorilla/mux itself is just a router and dispatcher, which, although technically could do what you're asking, is not really what it's designed for.
The "idiomatic" approach is to wrap your handler with middleware that decorates your handler with any additional functions, like one to check headers for some information on every call.
Expanding on the gorilla/mux full example, you could do something like:
package main
import (
"log"
"net/http"
"github.com/gorilla/mux"
)
func HeaderCheckWrapper(requiredHeader string, original func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if foo, ok := r.Header[requiredHeader]; ok {
// do whatever with the header value, and then call the original:
log.Printf("Found header %q, with value %q", requiredHeader, foo)
original(w, r)
return
}
// otherwise reject the request
w.WriteHeader(http.StatusPreconditionFailed)
w.Write([]byte("missing expected header " + requiredHeader))
}
}
func YourHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Gorilla!
"))
}
func main() {
r := mux.NewRouter()
// Routes consist of a path and a handler function.
r.HandleFunc("/", HeaderCheckWrapper("X-Foo-Header", YourHandler))
// Bind to a port and pass our router in
log.Fatal(http.ListenAndServe(":8000", r))
}