https://github.com/mariomenjr/handlr
Easily manage routes and handlers on top of *http.ServeMux.
https://github.com/mariomenjr/handlr
go handler http router
Last synced: 10 months ago
JSON representation
Easily manage routes and handlers on top of *http.ServeMux.
- Host: GitHub
- URL: https://github.com/mariomenjr/handlr
- Owner: mariomenjr
- License: mit
- Created: 2022-04-23T14:46:54.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-05-03T17:14:25.000Z (about 4 years ago)
- Last Synced: 2025-08-04T11:53:37.565Z (10 months ago)
- Topics: go, handler, http, router
- Language: Go
- Homepage:
- Size: 25.4 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mariomenjr/handlr
[](https://godoc.org/github.com/mariomenjr/handlr)
[](https://circleci.com/gh/mariomenjr/handlr/tree/main)
Easily manage routes and handlers on top of *http.ServeMux.
---
## Install
As you would do with almost any other Go package.
```bash
go get -u github.com/mariomenjr/handlr
```
---
## Examples
### Handlers
You can register paths and handlers directly:
```go
// 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 `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
```
```go
// main.go
func main() {
h := handlr.New()
h.RouteFunc("/feed", feedRoute)
h.RouteFunc("/account", accountRoute)
r.Start(1993)
}
```
```go
// 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) {
// ...
}
```
```go
// 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](https://opensource.org/licenses/MIT).