nanoServe is a lightweight HTTP router written in Go. It provides a trie-based routing engine with support for middleware, multiple HTTP methods, and flexible route handling.
The goal of nanoServe is to remain simple, fast, and easy to extend while maintaining clean architectural separation between routing and execution.
- Trie-based path matching
- Support for all standard HTTP methods
- Global and path-specific middleware
- Variadic handler support
- Implements
http.Handlerinterface - Minimal and extensible core design
- Inbuilt param parser inside
Trierouter
go get github.com/libsib/nanoservepackage main
import (
"net/http"
"github.com/libsib/nanoserve"
)
func main() {
r := nanoserve.New()
globalMiddleware := func(c *nanoserve.Context) error {
fmt.println("path: ", c.Request.URL.Path)
c.Next() // it's necessary if you wants to invoke next handlers
return nil
}
r.Use(globalMiddleware)
r.GET("/hello", func(c *nanoserve.Context) error {
c.Text("Hello World",200) // return c.Text("Hello World",200)
// OR
return nil
})
r.Run(":8080")
}Visit http://localhost:8080/hello
nanoServe supports all standard HTTP methods:
- GET
- POST
- PUT
- PATCH
- DELETE
- HEAD
- OPTIONS
- CONNECT
- TRACE
You can also use:
r.Handle(method, path, handlers...)
r.ANY(path, handlers...)r.Use(func(c *nanoserve.Context) {
println("global middleware")
})r.Use("/api", func(c *nanoserve.Context) error {
println("api middleware")
return nil
})Middleware executes in the order they are registered.
nanoServe supports parameterized routes using : prefix.
Example:
r.GET("/users/:id", handler)Parameter extraction logic is handled by the trie router.
nanoServe follows a layered architecture:
- HTTP Layer – Implements
http.Handler - Router Layer – Manages route registration
- Trie Layer – Handles path matching
- Middleware Layer – Collects and executes middleware
This separation ensures maintainability and extensibility.
nanoserve/
├── router.go
├── trie.go
├── server.go
├── middleware.go
├── context.go (planned)
- Context abstraction
- Improved middleware chaining
- Route groups
- Subrouters
- 405 Method Not Allowed support
- Automatic OPTIONS handling
- Performance benchmarking
nanoServe aims to:
- Keep the core minimal
- Avoid unnecessary abstractions
- Maintain clean separation of concerns
- Stay idiomatic to Go
- Lazy work to keep nanoServe fast
MIT License
Contributions are welcome. Feel