mariomenjr / handlr

Easily manage routes and handlers on top of *http.ServeMux.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

mariomenjr/handlr

GoDoc CircleCI

Easily manage routes and handlers on top of *http.ServeMux.


Install

As you would do with almost any other Go package.

go get -u github.com/mariomenjr/handlr

Examples

Handlers

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) {
	// ...
}

Routes

You can also register Routes 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) {
	// ...
}

License

The source code of this project is under MIT License.

About

Easily manage routes and handlers on top of *http.ServeMux.

License:MIT License


Languages

Language:Go 100.0%