Skip to content

Instantly share code, notes, and snippets.

@alehano
Created July 31, 2014 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alehano/db216ee27b421e4a11b4 to your computer and use it in GitHub Desktop.
Save alehano/db216ee27b421e4a11b4 to your computer and use it in GitHub Desktop.
Http Handler auth decorator
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