🔗 HTTP router based on radix trees.
19 Commits
Eduard Urbach 7ec90a691b
Improved performance
2024-03-15 09:52:34 +01:00
testdata Added trailing slash for static routes 2024-03-13 13:04:03 +01:00
.gitignore Improved code quality 2023-09-01 12:43:36 +02:00
benchmarks_test.go Added trailing slash for static routes 2024-03-13 13:04:03 +01:00
flow.go Added trailing slash for static routes 2024-03-13 13:04:03 +01:00
go.mod Improved performance 2024-03-14 23:26:59 +01:00
go.sum Improved code quality 2023-09-01 12:43:36 +02:00
Parameter.go Improved performance 2024-03-14 23:26:59 +01:00
README.md Improved performance 2024-03-15 09:52:34 +01:00
Router_test.go Improved performance 2024-03-14 23:26:59 +01:00
Router.go Improved performance 2024-03-14 23:26:59 +01:00
Tree.go Improved performance 2024-03-15 09:52:34 +01:00
treeNode.go Added trailing slash for static routes 2024-03-13 13:04:03 +01:00

router

HTTP router based on radix trees.

Features

  • Efficient lookup
  • Generic data structure
  • Zero dependencies (excluding tests)

Installation

go get git.akyoto.dev/go/router

Usage

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)
})

Tests

PASS: TestStatic
PASS: TestParameter
PASS: TestWildcard
PASS: TestMap
PASS: TestMethods
PASS: TestGitHub
PASS: TestTrailingSlash
PASS: TestTrailingSlashOverwrite
PASS: TestOverwrite
PASS: TestInvalidMethod
coverage: 100.0% of statements

Benchmarks

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

License

Please see the license documentation.

© 2023 Eduard Urbach