Instantly share code, notes, and snippets.

Embed
What would you like to do?
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