128 lines
3.2 KiB
Markdown
128 lines
3.2 KiB
Markdown
# FastRouter
|
|
|
|
A high-performance router for fasthttp with support for path parameters, wildcards, middleware, and route grouping. Built on a prefix tree data structure with minimal allocations—🔥*blazingly* fast🔥.
|
|
|
|
## Features
|
|
- Built for fasthttp
|
|
- Zero dependencies beyond fasthttp
|
|
- Fast path matching with radix tree structure
|
|
- Path parameters (`[id]`) and wildcards (`*path`) support
|
|
- Middleware support for processing pipelines
|
|
- Route grouping with shared prefixes and middleware
|
|
- Up to 15x faster than standard routers for dynamic routes
|
|
- Zero allocations for static routes
|
|
|
|
## Installation
|
|
|
|
```shell
|
|
go get git.sharkk.net/Go/FastRouter
|
|
```
|
|
|
|
## Usage
|
|
|
|
### Basic Routing
|
|
|
|
```go
|
|
// Create a new router
|
|
r := router.New()
|
|
|
|
// Static routes
|
|
r.Get("/", func(ctx router.Ctx, params []string) {
|
|
fmt.Fprintf(ctx, "Root handler")
|
|
})
|
|
|
|
// Parameter routes
|
|
r.Get("/users/[id]", func(ctx router.Ctx, params []string) {
|
|
userID := params[0]
|
|
fmt.Fprintf(ctx, "User ID: %s", userID)
|
|
})
|
|
|
|
// Wildcard routes
|
|
r.Get("/files/*path", func(ctx router.Ctx, params []string) {
|
|
filePath := params[0]
|
|
fmt.Fprintf(ctx, "File path: %s", filePath)
|
|
})
|
|
|
|
// Standard fasthttp handler adapter
|
|
r.Get("/simple", router.StandardHandler(func(ctx *fasthttp.RequestCtx) {
|
|
fmt.Fprintf(ctx, "Simple handler without params")
|
|
}))
|
|
|
|
// Start server
|
|
fasthttp.ListenAndServe(":8080", r.Handler())
|
|
```
|
|
|
|
### Middleware
|
|
|
|
```go
|
|
// Create logging middleware
|
|
func LoggingMiddleware(next router.Handler) router.Handler {
|
|
return router.newHandler(func(ctx router.Ctx, params []string) {
|
|
fmt.Println("Request started")
|
|
next.Serve(ctx, params)
|
|
fmt.Println("Request completed")
|
|
})
|
|
}
|
|
|
|
// Apply middleware globally
|
|
r := router.New()
|
|
r.Use(LoggingMiddleware)
|
|
|
|
// Apply middleware to specific routes
|
|
r.WithMiddleware(AuthMiddleware).Get("/admin", adminHandler)
|
|
```
|
|
|
|
### Route Groups
|
|
|
|
```go
|
|
// Create a router
|
|
r := router.New()
|
|
|
|
// Create an API group
|
|
api := r.Group("/api")
|
|
api.Get("/users", listUsersHandler) // matches /api/users
|
|
|
|
// Nested groups
|
|
v1 := api.Group("/v1")
|
|
v1.Get("/products", listProductsHandler) // matches /api/v1/products
|
|
|
|
// Group with middleware
|
|
admin := api.Group("/admin")
|
|
admin.Use(AuthMiddleware)
|
|
admin.Get("/stats", statsHandler) // matches /api/admin/stats
|
|
```
|
|
|
|
## Benchmarks
|
|
|
|
Benchmark comparing FastRouter to standard routers:
|
|
|
|
```
|
|
cpu: AMD Ryzen 9 7950X 16-Core Processor
|
|
|
|
BenchmarkComparison/root_path
|
|
Router: 2.098 ns/op 0 B/op 0 allocs/op
|
|
ServeMux: 32.010 ns/op 0 B/op 0 allocs/op
|
|
|
|
BenchmarkComparison/static_path
|
|
Router: 16.050 ns/op 0 B/op 0 allocs/op
|
|
ServeMux: 67.980 ns/op 0 B/op 0 allocs/op
|
|
|
|
BenchmarkComparison/dynamic_path
|
|
Router: 39.170 ns/op 16 B/op 1 allocs/op
|
|
ServeMux: 174.000 ns/op 48 B/op 3 allocs/op
|
|
|
|
BenchmarkComparison/not_found
|
|
Router: 10.580 ns/op 0 B/op 0 allocs/op
|
|
ServeMux: 178.100 ns/op 56 B/op 3 allocs/op
|
|
```
|
|
|
|
Key Performance Points:
|
|
- Root path lookups are 15x faster
|
|
- Static paths are 4x faster with zero allocations
|
|
- Dynamic paths are 4.4x faster with fewer allocations
|
|
- Not found paths are 16.8x faster with zero allocations
|
|
|
|
## License
|
|
|
|
[Sharkk Minimal License](LICENSE); do what you like!
|