I'm playing around with gorilla mux and would like to set all of the application routes in a file so they don't fill up the main file with a bunch of routes. Ideally I would like to have the optional ability to even pull the routes from a database too.
Is gorilla mux the right package to use for this or is there something else to look at? Is this something that can be done?
gorilla mux doesn't do this, and it's not common for routing libraries in Go, since it's statically typed and compiled language.
If you have a simple 1:1 mapping of handlers, you can do this fairly easily:
// register the handlers or handler_funcs by name in a map:
handlerMap := make(map[string]*http.Handler)
// OR
handlerFuncMap := make(map[string]func(http.ResponseWriter, *http.Request))
handlerMap["myHandler"] = myHandler
// now you can iterate over you config values and assign them to a router
for path, handler := range routes {
myRouter.Handler(path, handlerMap[handler])
}