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
|
2025-02-25 16:38:39 +01:00
|
|
|
go get git.urbach.dev/go/router
|
2023-07-18 11:13:58 +02:00
|
|
|
```
|
|
|
|
|
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
|
2025-03-01 14:27:26 +01:00
|
|
|
PASS: TestMixed
|
2023-09-01 12:43:36 +02:00
|
|
|
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
|
2024-03-13 13:04:03 +01:00
|
|
|
PASS: TestTrailingSlashOverwrite
|
2023-09-06 11:44:04 +02:00
|
|
|
PASS: TestOverwrite
|
2023-09-06 16:52:22 +02:00
|
|
|
PASS: TestInvalidMethod
|
2024-03-13 13:04:03 +01:00
|
|
|
coverage: 100.0% of statements
|
2023-09-01 12:43:36 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
## Benchmarks
|
2023-07-09 17:46:17 +02:00
|
|
|
|
|
|
|
```
|
2024-03-15 09:52:34 +01:00
|
|
|
BenchmarkBlog/Len1-Param0-12 211814850 5.646 ns/op 0 B/op 0 allocs/op
|
|
|
|
BenchmarkBlog/Len1-Param1-12 132838722 8.978 ns/op 0 B/op 0 allocs/op
|
|
|
|
BenchmarkGitHub/Len7-Param0-12 84768382 14.14 ns/op 0 B/op 0 allocs/op
|
|
|
|
BenchmarkGitHub/Len7-Param1-12 55290044 20.74 ns/op 0 B/op 0 allocs/op
|
|
|
|
BenchmarkGitHub/Len7-Param2-12 26057244 46.08 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
|
|
|
|
|
2025-02-25 16:38:39 +01:00
|
|
|
Please see the [license documentation](https://urbach.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
|