# Router A high-performance router for Go with support for path parameters, wildcards, middleware, and route grouping. Sports an incredibly simple API over the top of a prefix tree data structure. Minimal allocations and, dare we say, 🔥*blazingly* fast🔥. ## Features - Zero dependencies - Fast path matching with radix tree structure - Support for path parameters (`[id]`) and wildcards (`*path`) - Middleware support for request processing pipelines - Route grouping with shared prefixes and middleware - Implements `http.Handler` for direct integration - Up to 15x faster than `http.ServeMux` for dynamic routes - Zero allocations for static routes ## Installation ```shell go get git.sharkk.net/Go/Router ``` ## Usage ### Basic Routing ```go // Create a new router r := router.New() // Static routes r.Get("/", func(w router.Res, r router.Req, params []string) { fmt.Fprintf(w, "Root handler") }) // Parameter routes r.Get("/users/[id]", func(w router.Res, r router.Req, params []string) { userID := params[0] fmt.Fprintf(w, "User ID: %s", userID) }) // Wildcard routes r.Get("/files/*path", func(w router.Res, r router.Req, params []string) { filePath := params[0] fmt.Fprintf(w, "File path: %s", filePath) }) // Standard http.HandlerFunc adapter r.Get("/simple", router.StandardHandler(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Simple handler without params") })) // Lookup routes manually if handler, params, ok := r.Lookup("GET", "/users/123"); ok { handler.Serve(params) } // Or simply serve them http.ListenAndServe(":8080", r) ``` ### Middleware ```go // Create logging middleware func LoggingMiddleware(next router.Handler) router.Handler { return &router.simpleHandler{ fn: func(params []string) { fmt.Println("Request started") next.Serve(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) http.ListenAndServe(":8080", r) ``` ### 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 http.ListenAndServe(":8080", r) ``` ## Benchmarks Benchmark comparing Router to the standard `http.ServeMux`: ``` 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 ``` - 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!