Skip to content

Instantly share code, notes, and snippets.

@DirkWillem
Created January 30, 2016 17:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DirkWillem/a259de02da3d5861e237 to your computer and use it in GitHub Desktop.
Save DirkWillem/a259de02da3d5861e237 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
)
type httpHandlerFunc func(http.ResponseWriter, *http.Request)
func checkSecurityA(next httpHandlerFunc) httpHandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
header := req.Header.Get("Super-Duper-Safe-Security")
if header != "password" {
fmt.Fprint(res, "Invalid password")
res.WriteHeader(http.StatusUnauthorized)
return
}
next(res, req)
}
}
func checkSecurityB(password string, next httpHandlerFunc) httpHandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
header := req.Header.Get("Even-Safer-Security")
if header != password {
fmt.Fprint(res, "Invalid password")
res.WriteHeader(http.StatusUnauthorized)
return
}
next(res, req)
}
}
func indexHandler(res http.ResponseWriter, req *http.Request) {
fmt.Fprint(res, "Hello, World!")
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/", checkSecurityA(checkSecurityB("SaferPassword1234", (indexHandler))))
http.Handle("/", r)
http.ListenAndServe(":8000", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment