2024-08-22 21:08:25 -05:00
|
|
|
# Router
|
|
|
|
|
2024-08-22 22:02:27 -05:00
|
|
|
A radix-tree based no-allocation router in Go. All credit to Eduard Urbach for his incredible work.
|
|
|
|
|
|
|
|
## Features
|
|
|
|
|
|
|
|
- Efficient lookup
|
|
|
|
- Generic data structure
|
|
|
|
- Zero dependencies
|
|
|
|
|
|
|
|
## Installation
|
|
|
|
|
|
|
|
```shell
|
|
|
|
go get git.sharkk.net/Go/Router
|
|
|
|
```
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
```go
|
|
|
|
router := router.New[string]()
|
|
|
|
|
|
|
|
// Static routes
|
|
|
|
router.Add("GET", "/hello", "...")
|
|
|
|
router.Add("GET", "/world", "...")
|
|
|
|
|
|
|
|
// Parameter routes
|
|
|
|
router.Add("GET", "/users/:id", "...")
|
|
|
|
router.Add("GET", "/users/:id/comments", "...")
|
|
|
|
|
|
|
|
// Wildcard routes
|
|
|
|
router.Add("GET", "/images/*path", "...")
|
|
|
|
|
|
|
|
// Simple lookup
|
|
|
|
data, params := router.Lookup("GET", "/users/42")
|
|
|
|
fmt.Println(data, params)
|
|
|
|
|
|
|
|
// Efficient lookup
|
|
|
|
data := router.LookupNoAlloc("GET", "/users/42", func(key string, value string) {
|
|
|
|
fmt.Println(key, value)
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
|
|
|
## Benchmarks
|
|
|
|
```
|
|
|
|
goos: linux
|
|
|
|
goarch: amd64
|
|
|
|
pkg: git.sharkk.net/Go/Router/tests
|
|
|
|
cpu: AMD Ryzen 9 7950X 16-Core Processor
|
|
|
|
BenchmarkBlog/Len1-Param0-32 384992013 3.337 ns/op 0 B/op 0 allocs/op
|
|
|
|
BenchmarkBlog/Len1-Param1-32 199599014 6.021 ns/op 0 B/op 0 allocs/op
|
|
|
|
BenchmarkGithub/Len7-Param0-32 256332994 4.678 ns/op 0 B/op 0 allocs/op
|
|
|
|
BenchmarkGithub/Len7-Param1-32 269038417 4.455 ns/op 0 B/op 0 allocs/op
|
|
|
|
BenchmarkGithub/Len7-Param2-32 256228226 4.673 ns/op 0 B/op 0 allocs/op
|
|
|
|
PASS
|
|
|
|
ok git.sharkk.net/Go/Router/tests 8.410s
|
|
|
|
```
|