https://github.com/valsov/router
Go HTTP router mux
https://github.com/valsov/router
http-server mux-router
Last synced: 5 months ago
JSON representation
Go HTTP router mux
- Host: GitHub
- URL: https://github.com/valsov/router
- Owner: valsov
- Created: 2024-05-16T17:02:02.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-04T17:21:32.000Z (over 1 year ago)
- Last Synced: 2025-10-19T18:52:19.955Z (5 months ago)
- Topics: http-server, mux-router
- Language: Go
- Homepage:
- Size: 23.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# HTTP router mux
Implementation of a request router/dispatcher based on a **radix tree** to enable fast route lookup.
## Usage
### Routes and handlers
```go
func main() {
// Configure router
mux := router.NewHttpRouter()
// Handle with http.HandlerFunc
mux.HandleFunc(router.GET, "/route/to/handle1", func(w http.ResponseWriter, r *http.Request) {
// [...]
})
// Handle with http.Handler
handler := CustomHandler{}
mux.Handle(router.GET, "/route/to/handle2", &handler)
// Support wildcards in path
mux.HandleFunc(router.GET, "/route/{param1}/sample/{param2}", func(w http.ResponseWriter, r *http.Request) {
// [...]
})
// Add middleware
mux.UseMiddleware(middleware.LoggerMiddleware(loggerInstance))
err := http.ListenAndServe("addr", mux)
// [...]
}
type CustomHandler struct{}
func (c *CustomHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// [...]
}
```
### Request parameters
```go
mux.HandleFunc(router.GET, "/route/{param1}", func(w http.ResponseWriter, r *http.Request) {
// Get route parameter "param1" value
routeParam, found := router.GetRouteParam(r, "param1")
// Get query parameter value
queryParam, found := router.GetQueryParam(r, "qparam1")
// Get multiple query parameter values
queryParams, found := router.GetQueryParamValues(r, "qparam2")
// Get request value from either router or query
param, found := router.GetParam(r, "param")
})
```
## Radix tree
Each registered route is split to form a tree, with the HTTP method as a route node. **Wildcards** are supported using the following syntax: `{wildcardName}`.
Example with routes registration (where "hx" is the handler's name):
- [GET] /users (h1)
- [GET] /users/{userId} (h2)
- [POST] /users (h3)
- [GET] /stats/sample (h4)
```mermaid
graph TD;
GET-->getRoot(/);
getRoot-->getUsers("users (h1)");
getUsers-->getUserWithId("{userId} (h2)");
getRoot-->stats;
stats-->getSample("sample (h4)");
POST-->postRoot(/);
postRoot-->postUsers("users (h3)");
```