Skip to content

Instantly share code, notes, and snippets.

@mattmcla
Last active April 18, 2016 18:27
Show Gist options
  • Save mattmcla/d2c5d19fea046126e0ca2dccfc185cd4 to your computer and use it in GitHub Desktop.
Save mattmcla/d2c5d19fea046126e0ca2dccfc185cd4 to your computer and use it in GitHub Desktop.
Custom Headers on Every Response With golangs httprouter
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