76 lines
1.7 KiB
Markdown
Raw Normal View History

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
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-14 23:26:59 +01:00
BenchmarkBlog/Len1-Param0-12 200621386 5.963 ns/op 0 B/op 0 allocs/op
BenchmarkBlog/Len1-Param1-12 129550375 9.224 ns/op 0 B/op 0 allocs/op
BenchmarkGitHub/Len7-Param0-12 70562060 16.98 ns/op 0 B/op 0 allocs/op
BenchmarkGitHub/Len7-Param1-12 49366180 22.56 ns/op 0 B/op 0 allocs/op
BenchmarkGitHub/Len7-Param2-12 24332162 47.91 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