Lightweight, idiomatic and composable router for building Go HTTP services .
Chi (go-chi/chi/v5) is a lightweight, composable, and idiomatic Go HTTP router designed specifically for building REST API services,. It aims to provide strong routing features without the complexity or "bloat" found in full frameworks,.
Here are the principal functionalities and features of Chi:
Lightweight and Idiomatic: Chi is characterized by its lightweight nature and its adherence to the standard net/http interfaces, making it highly idiomatic to the Go language,,.
Composability: It simplifies the construction of large REST API services by enabling the system to be broken down into smaller, manageable components.
Performance: Chi generally offers better performance than Gorilla Mux and consumes less memory. Compared to Gin, Chi typically uses fewer allocations while maintaining exceptional performance under high load.
Installation: It is installed as a dependency using the Go module system via the command go get -u github.com/go-chi/chi/v5,.
Standard HTTP Methods: Chi supports routing for all standard HTTP methods, including GET, POST, HEAD, PUT, PATCH, DELETE, OPTIONS, TRACE, and CONNECT.
Radix Tree Structure: The router is built on an efficient radix tree structure for quick and flexible routing,.
URL Parameter Handling: It simplifies the extraction of input parameters, whether they come from the URL, the request body, or query parameters. Specific parameters, such as a requested book ID, are easily read from the URL using the chi.URLParam function.
Sub-routers and Grouping: Chi allows for the creation of sub-routers using the r.Mount method, which helps organize and link related routes (e.g., all book routes linked to /books),,. Related routes can also be organized using r.Group.
Advanced Pattern Matching: It supports sophisticated URL pattern matching, including regex constraints (e.g., /{id:+}) and wildcard patterns (e.g., /files/{filename:.+}).
Flexible and Focused Design: Chi's design places its middleware system at the forefront, allowing developers to easily compose and maintain their middleware stack with minimal boilerplate,.
Compatibility: Its middleware handlers are standard stdlib net/http handlers, ensuring compatibility with any middleware available in the Go community,.
Usage: Middleware defines pre-handler and post-handler stages for implementing cross-cutting concerns like authentication, logging, tracing, and general request processing,. Middleware can be applied globally (r.Use) or scoped to specific handlers or groups (r.Group or r.With).
Request-Scoped Data: Chi brilliantly leverages Go’s built-in context package to manage requests. Contexts act as carriers for important, request-scoped data, such as user IDs or request metadata, seamlessly passing information through the middleware chain to the handler,,.
Timeouts and Cancellation: Context is used for managing crucial elements like request timeouts to ensure the application remains responsive. Developers are advised to always call the cancel function when using contexts with timeouts or deadlines to prevent resource leaks.
Simplified Error Handling: Error handling is simple, relying on the standard http package's Error() function to respond with a specific error message and HTTP status code constant (e.g., http.StatusNotFound),.
Unit Testing: Chi encourages unit testing by allowing the handler functions to use an interface for storage access (e.g., BookStorage), which makes it easy to substitute real storage with mock (fake) storage during tests,,. The httptest package can be used to construct the necessary http.ResponseWriter and http.Request parameters for isolated handler tests.
Integration Testing: Integration tests can be performed by starting a test server using the httptest.NewServer function, allowing tests to make actual HTTP requests against the defined router without mocking components,.