2024-08-22 21:08:25 -05:00
|
|
|
# Router
|
|
|
|
|
2024-09-05 14:14:14 -05:00
|
|
|
A radix-tree based no-allocation router in Go. All credit to Eduard Urbach for his incredible work. This router sports
|
|
|
|
a fancy PATRICIA tree structure for efficient string lookups. It also has ***zero dependencies***!
|
2024-08-22 22:02:27 -05:00
|
|
|
|
2024-09-06 08:16:32 -05:00
|
|
|
The router has a generic data structure, so any kind of handler can be used.
|
|
|
|
|
2024-08-22 22:02:27 -05:00
|
|
|
## Installation
|
|
|
|
|
|
|
|
```shell
|
|
|
|
go get git.sharkk.net/Go/Router
|
|
|
|
```
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
```go
|
2024-09-06 08:17:10 -05:00
|
|
|
router := router.New[string]()
|
2024-08-22 22:02:27 -05:00
|
|
|
|
|
|
|
// 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
|
|
|
|
```
|
2024-08-23 22:15:58 -05:00
|
|
|
cpu: AMD Ryzen 9 7950X 16-Core Processor
|
|
|
|
|
2024-08-23 22:16:28 -05:00
|
|
|
BenchmarkBlog/Len1-Params0-32 268391120 4.461 ns/op 0 B/op 0 allocs/op
|
|
|
|
BenchmarkBlog/Len1-Params0-NoAlloc-32 383737024 3.123 ns/op 0 B/op 0 allocs/op
|
|
|
|
BenchmarkBlog/Len1-Param1-32 23690427 47.85 ns/op 32 B/op 1 allocs/op
|
|
|
|
BenchmarkBlog/Len1-Param1-NoAlloc-32 248275540 4.913 ns/op 0 B/op 0 allocs/op
|
2024-08-23 22:15:58 -05:00
|
|
|
|
|
|
|
BenchmarkGithub/Len7-Params0-32 148926122 8.043 ns/op 0 B/op 0 allocs/op
|
|
|
|
BenchmarkGithub/Len7-Params0-NoAlloc-32 168011829 7.153 ns/op 0 B/op 0 allocs/op
|
|
|
|
BenchmarkGithub/Len7-Params1-32 22188592 47.25 ns/op 32 B/op 1 allocs/op
|
|
|
|
BenchmarkGithub/Len7-Params1-NoAlloc-32 100000000 11.29 ns/op 0 B/op 0 allocs/op
|
|
|
|
BenchmarkGithub/Len7-Params3-32 10181496 111.3 ns/op 96 B/op 2 allocs/op
|
|
|
|
BenchmarkGithub/Len7-Params3-NoAlloc-32 46369563 25.54 ns/op 0 B/op 0 allocs/op
|
2024-08-23 22:20:14 -05:00
|
|
|
```
|
|
|
|
|
2024-09-05 14:14:14 -05:00
|
|
|
## License
|
|
|
|
|
|
|
|
Licensed under MIT. [Take a look!](LICENSE)
|
|
|
|
|
2024-08-23 22:20:14 -05:00
|
|
|
## Credit
|
|
|
|
|
2024-09-05 14:14:14 -05:00
|
|
|
All of the code here is built by [Eduard Urbach](https://git.akyoto.dev/go/router).
|