Easily manage routes and handlers on top of *http.ServeMux.
As you would do with almost any other Go package.
go get -u github.com/mariomenjr/handlr
You can register paths and handlers directly:
// main.go
func main() {
h := handlr.New()
h.HandleFunc("/feed", feedHandler)
r.Start(1993)
}
func feedHandler(w http.ResponseWriter, r *http.Request) {
// ...
}
You can also register Route
s which allow you to organize your handlers (or even sub-Routes) into multiple files in an elegant way.
├── main.go
├── feed.route.go
├── acccount.route.go
// main.go
func main() {
h := handlr.New()
h.RouteFunc("/feed", feedRoute)
h.RouteFunc("/account", accountRoute)
r.Start(1993)
}
// feed.route.go
func feedRoute(r *handlr.Router) {
r.HandleFunc("/latest", latestHandler)
r.RouteFunc("/custom", feedCustomRoute)
}
func latestHandler(w http.ResponseWriter, r *http.Request) {
// ...
}
func feedCustomRoute(r *handlr.Router) {
r.HandleFunc("/monthly", feedCustomMonthlyHandler)
}
func feedCustomMonthlyHandler(w http.ResponseWriter, r *http.Request) {
// ...
}
// account.route.go
func accountRoute(r *handlr.Router) {
r.HandleFunc("/profile", accountProfileHandlr)
r.HandleFunc("/settings", accountSettingsHandlr)
}
func accountProfileHandlr(w http.ResponseWriter, r *http.Request) {
// ...
}
func accountSettingsHandlr(w http.ResponseWriter, r *http.Request) {
// ...
}
The source code of this project is under MIT License.