Last active
December 31, 2015 23:29
-
-
Save Ziaunys/8060432 to your computer and use it in GitHub Desktop.
Mux laziness..
This file contains hidden or 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
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