Skip to content

Instantly share code, notes, and snippets.

@Ziaunys
Last active December 31, 2015 23:29
Show Gist options
  • Save Ziaunys/8060432 to your computer and use it in GitHub Desktop.
Save Ziaunys/8060432 to your computer and use it in GitHub Desktop.
Mux laziness..
type Handler struct {
Function func(http.ResponseWriter, *http.Request)
Methods []string
}
func AddHandlers(r *mux.Router, handles map[string]Handler) {
for k, v := range handles {
r.HandleFunc(k, v.Function).Methods(v.Methods...)
}
}
func main() {
r := mux.NewRouter()
r.Headers("Content-Type", "text/json")
AddHandlers(r, map[string]Handler{
"/": Handler{ Function: homeHandler, Methods: []string{ "GET",}},
"/blogs/{name}": Handler{ Function: blogHandler, Methods: []string{ "GET", "POST", "PUT", "DELETE",}},
"/blogs/{name}/{post_title}": Handler{ Function: postHandler, Methods: []string{"GET", "POST", "PUT", "DELETE",}},
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment