2023-07-09 10:21:32 +00:00
|
|
|
# router
|
|
|
|
|
2023-07-09 17:46:17 +02:00
|
|
|
HTTP router based on radix trees.
|
|
|
|
|
2023-09-01 12:43:36 +02:00
|
|
|
## Features
|
|
|
|
|
|
|
|
- Efficient lookup
|
|
|
|
- Generic data structure
|
|
|
|
- Zero dependencies (excluding tests)
|
|
|
|
|
2023-07-18 11:13:58 +02:00
|
|
|
## Installation
|
|
|
|
|
|
|
|
```shell
|
|
|
|
go get git.akyoto.dev/go/router
|
|
|
|
```
|
|
|
|
|
2023-07-10 14:48:28 +02:00
|
|
|
## Usage
|
2023-07-09 21:24:24 +02:00
|
|
|
|
|
|
|
```go
|
|
|
|
router := router.New[string]()
|
|
|
|
|
2023-09-01 12:43:36 +02:00
|
|
|
// Static routes
|
|
|
|
router.Add("GET", "/hello", "...")
|
|
|
|
router.Add("GET", "/world", "...")
|
2023-07-09 21:24:24 +02:00
|
|
|
|
2023-09-01 12:43:36 +02:00
|
|
|
// Parameter routes
|
2023-07-10 14:48:28 +02:00
|
|
|
router.Add("GET", "/users/:id", "...")
|
|
|
|
router.Add("GET", "/users/:id/comments", "...")
|
|
|
|
|
2023-09-01 12:43:36 +02:00
|
|
|
// Wildcard routes
|
|
|
|
router.Add("GET", "/images/*path", "...")
|
2023-07-10 14:48:28 +02:00
|
|
|
|
2023-09-01 12:43:36 +02:00
|
|
|
// Simple lookup
|
|
|
|
data, params := router.Lookup("GET", "/users/42")
|
|
|
|
fmt.Println(data, params)
|
2023-07-10 14:48:28 +02:00
|
|
|
|
2023-09-01 12:43:36 +02:00
|
|
|
// Efficient lookup
|
|
|
|
data := router.LookupNoAlloc("GET", "/users/42", func(key string, value string) {
|
|
|
|
fmt.Println(key, value)
|
|
|
|
})
|
2023-07-10 14:48:28 +02:00
|
|
|
```
|
|
|
|
|
2023-09-01 12:43:36 +02:00
|
|
|
## Tests
|
2023-07-09 17:46:17 +02:00
|
|
|
|
2023-09-01 12:43:36 +02:00
|
|
|
```
|
|
|
|
PASS: TestStatic
|
|
|
|
PASS: TestParameter
|
|
|
|
PASS: TestWildcard
|
2023-09-06 16:52:22 +02:00
|
|
|
PASS: TestMap
|
2023-09-01 12:43:36 +02:00
|
|
|
PASS: TestMethods
|
|
|
|
PASS: TestGitHub
|
2023-09-06 16:52:22 +02:00
|
|
|
PASS: TestTrailingSlash
|
2023-09-06 11:44:04 +02:00
|
|
|
PASS: TestOverwrite
|
2023-09-06 16:52:22 +02:00
|
|
|
PASS: TestInvalidMethod
|
|
|
|
coverage: 99.1% of statements
|
2023-09-01 12:43:36 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
## Benchmarks
|
2023-07-09 17:46:17 +02:00
|
|
|
|
|
|
|
```
|
2023-09-02 09:19:11 +02:00
|
|
|
BenchmarkBlog/Len1-Param0-12 182590244 6.57 ns/op 0 B/op 0 allocs/op
|
|
|
|
BenchmarkBlog/Len1-Param1-12 100000000 10.95 ns/op 0 B/op 0 allocs/op
|
|
|
|
BenchmarkGitHub/Len7-Param0-12 67053636 17.95 ns/op 0 B/op 0 allocs/op
|
|
|
|
BenchmarkGitHub/Len7-Param1-12 49371550 24.12 ns/op 0 B/op 0 allocs/op
|
|
|
|
BenchmarkGitHub/Len7-Param2-12 24562465 48.83 ns/op 0 B/op 0 allocs/op
|
2023-07-10 14:48:28 +02:00
|
|
|
```
|
|
|
|
|
2023-09-01 12:43:36 +02:00
|
|
|
## License
|
|
|
|
|
|
|
|
Please see the [license documentation](https://akyoto.dev/license).
|
2023-07-10 14:48:28 +02:00
|
|
|
|
2023-09-01 12:43:36 +02:00
|
|
|
## Copyright
|
2023-07-10 14:48:28 +02:00
|
|
|
|
2023-09-01 12:43:36 +02:00
|
|
|
© 2023 Eduard Urbach
|