Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shyamz-22/router
A lightweight httprouter
https://github.com/shyamz-22/router
go golang microservices rest-api router web
Last synced: 3 days ago
JSON representation
A lightweight httprouter
- Host: GitHub
- URL: https://github.com/shyamz-22/router
- Owner: shyamz-22
- License: mit
- Created: 2018-09-02T15:55:39.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-06-01T13:33:19.000Z (over 5 years ago)
- Last Synced: 2024-06-20T14:20:49.165Z (7 months ago)
- Topics: go, golang, microservices, rest-api, router, web
- Language: Go
- Size: 19.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# HTTP ROUTER
A very lean implementation of http router that supports path param parsing, suitable
for most REST implementations. Inspired by [httprouter](https://github.com/julienschmidt/httprouter/blob/master/params_go17.go)## Usage
```go
package mainimport (
"fmt"
"github.com/shyamz-22/router"
"log"
"net/http"
)func main() {
rtr := router.New()
rtr.Add("/ping", http.MethodGet, func(w http.ResponseWriter, r *http.Request, params router.PathParams) {
w.Write([]byte("Pong!"))
})
rtr.Add("/pings/:id", http.MethodGet, func(w http.ResponseWriter, r *http.Request, params router.PathParams) {
id := params.ByName("id")
response := fmt.Sprintf("Pong: %s", id)
w.Write([]byte(response))
})
log.Fatal(http.ListenAndServe(":8080", rtr))
}
```## Running tests
```bash
> go test -v ./...```
## Running parallel tests
```bash
> go test -parallel 8 -v ./...```
## Running Benchmarks
```bash
> go test -run none -bench Benchmark -benchmem -benchtime 3s -memprofile mem.out
```## Memory profiling
```bash
> go test -run none -bench BenchmarkGithub -benchmem -benchtime 20s -memprofile mem.out
> go tool pprof -alloc_space router.test mem.out
```
### Output```
File: router.test
Type: alloc_space
Time: Sep 2, 2018 at 9:32pm (CEST)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top
(pprof) list findRoute```
## Setup
```bash
> go get github.com/shyamz-22/router
> cd $GOPATH/github.com/shyamz-22/router
> dep ensure -update
> go test ./...
```## What is not supported yet
- Support for http.Handler
- Behavior for trailing slashes
- Configurable NotFound and MethodNotAllowed Handlers
- Panic Handling
- Regexp validation for path parameters