-
-
Save efrenfuentes/917a8182e80a0eeceac7 to your computer and use it in GitHub Desktop.
go/use: Little middleware chains that could. Inspired by comments here: https://github.com/gorilla/mux/pull/36
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
r := mux.NewRouter() | |
// Single handler | |
r.HandleFunc("/form", use(http.HandlerFunc(formHandler), csrf, logging) | |
// All handlers | |
http.Handle("/", recovery(r)) | |
// Sub-routers | |
apiMiddleware := []func(http.Handler) http.Handler{logging, apiAuth, json} | |
api := router.PathPrefix("/api").SubRouter() | |
api.Handle("/users", use(usersHandler, apiMiddleware...)) | |
// Middleware chainer | |
func use(h http.Handler, middleware ...func(http.Handler) http.Handler) http.Handler { | |
for _, m := range middleware { | |
h = m(h) | |
} | |
return h | |
} | |
// Middleware (just a http.Handler) | |
func csrf(h http.Handler) http.Handler { | |
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
// do stuff | |
h.ServeHTTP(w, r) | |
// do stuff after | |
}) | |
} | |
// Basic handler at the end of it all. | |
func formHandler(w http.ResponseWriter, r *http.Request) { | |
// do stuff | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment