Created
April 20, 2017 22:04
-
-
Save hashbender/ed1b49e09b2827820e64aaa774758979 to your computer and use it in GitHub Desktop.
middlewares
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
//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