Created
July 31, 2014 06:45
-
-
Save alehano/db216ee27b421e4a11b4 to your computer and use it in GitHub Desktop.
Http Handler auth decorator
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
package main | |
import ( | |
"fmt" | |
"net/http" | |
) | |
const ( | |
//user_is_authenticated = false | |
user_is_authenticated = true | |
) | |
func authCheck(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) { | |
return func(w http.ResponseWriter, r *http.Request) { | |
// check for auth | |
is_auth := user_is_authenticated | |
if is_auth { | |
f(w, r) | |
} else { | |
http.Redirect(w, r, "/access_denied", http.StatusTemporaryRedirect) | |
} | |
} | |
} | |
func AdminPageHandler(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "Here is admin page\n") | |
} | |
func main() { | |
http.HandleFunc("/", authCheck(AdminPageHandler)) | |
//http.HandleFunc("/", authCheck(SomeOtherHandler)) | |
http.ListenAndServe(":8080", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment