Created
January 30, 2016 17:45
-
-
Save DirkWillem/a259de02da3d5861e237 to your computer and use it in GitHub Desktop.
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" | |
"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