Skip to content

Instantly share code, notes, and snippets.

@hashbender
Created April 20, 2017 22:04
Show Gist options
  • Save hashbender/ed1b49e09b2827820e64aaa774758979 to your computer and use it in GitHub Desktop.
Save hashbender/ed1b49e09b2827820e64aaa774758979 to your computer and use it in GitHub Desktop.
middlewares
//CheckAuth is an example middleware which demonstrates how we *might* check auth.
func CheckAuth(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
//TODO this is just an example.
//Get the cookie or something and check it
cookie, err := r.Cookie("session")
if err != nil || cookie == nil {
// TODO if an err, then redirect
// http.Redirect(w, r, "/", 401)
}
//If the auth check passes, then handle continue down the chain
h.ServeHTTP(w, r)
})
}
//SetContentTypeText this only exists to demonstrate how we can chain middlewares
func SetContentTypeText(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
h.ServeHTTP(w, r)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment