Last active
April 18, 2016 18:27
-
-
Save mattmcla/d2c5d19fea046126e0ca2dccfc185cd4 to your computer and use it in GitHub Desktop.
Custom Headers on Every Response With golangs httprouter
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
func main() { | |
r := httprouter.New() | |
// Health check | |
r.GET(“/health”, HealthCheckHandler) | |
// Authentication | |
r.POST(“/token-auth”, HeaderMiddleware(TokenAuthHandler)) | |
r.POST(“/p”, HeaderMiddleware(SomeFormHandler)) | |
fmt.Println(“Starting server on :3030”) | |
http.ListenAndServe(“:3030”, r) | |
} | |
func HeaderMiddleware(h httprouter.Handle) httprouter.Handle { | |
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | |
w.Header().Set(“Access-Control-Allow-Origin”, “*”) | |
h(w, r, ps) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment