rex is a minimalistic but robust HTTP router built on Go 1.22’s enhanced http.ServeMux
. It offers a range of features for rapid web application development, including:
- Middleware Support: Apply middleware globally or to specific routes and groups.
- Helper Methods: Simplify defining routes and working with request/response handlers.
- Template Rendering: Includes automatic template inheritance (e.g., using a base template), passing context data to templates, an error template that displays error messages, and more.
- Body Parsing: Supports decoding multiple content types:
- JSON
- XML
- URL-encoded and multipart forms
Works with standard Go types, pointers, slices, and custom types implementing therex.FormScanner
interface.
- Validation: Validate request data using the
validator
package. - SPA Support:
User.SPA
to serve a single-page application. - Route Grouping and Subgroups:
Apply middleware to groups or individual routes for better organization. - Built-in Middleware:
- Logging: Uses Go’s
slog
package. - Panic Recovery: Gracefully handles panics.
- ETag Support: For caching optimization.
- CORS Handling: Cross-Origin Resource Sharing middleware.
- Seesion based cookie auth, Basic Auth & JWT Middleware: Secure your routes with seesion, basic or token-based authentication.
- CSRF Protection: Protect your routes from CSRF attacks with the CSRF middleware.
- Logging: Uses Go’s
- Custom Middleware:
Implement your own middleware by wrappingrex.Handler
. - Static File Serving:
User.Static
to serve static files from a directory orr.StaticFS
to serve files from ahttp.FileSystem
.Both of these method can serve the minified version of the files if present and rex.ServeMinifiedAssetsIfPresent is set to true. You can also easily convert standard HTTP handlers to
rex
handlers: - Use
rex.WrapHandler
to wrap ahttp.Handler
. - Use
rex.WrapFunc
to wrap ahttp.HandlerFunc
. - Centralize error handling and logging by overriding the routers error handler. The default error handler handles
rex.FormError
fromr.BodyParser
and validator.ValidationErrors if there is a validation error.
go get -u github.com/abiiranathan/rex
This example shows how to implement a custom type that satisfies the FormScanner
interface.
type Date time.Time // Date in format YYYY-MM-DD
// FormScan implements the FormScanner interface.
func (d *Date) FormScan(value interface{}) error {
v, ok := value.(string)
if !ok {
return fmt.Errorf("date value is not a string")
}
t, err := time.Parse("2006-01-02", v)
if err != nil {
return fmt.Errorf("invalid date format")
}
*d = Date(t)
return nil
}
For a complete example of template rendering and router usage, see the example in cmd/server/main.go.
rex includes a few external libraries, used only in the middleware subpackage. Explore the middleware package for more details and usage examples.
Run all tests with the following command:
go test -v ./...
Run benchmarks with memory profiling enabled:
go test -bench=. ./... -benchmem
Pull requests are welcome! For major changes, please open an issue to discuss your ideas first.
Don’t forget to update tests as needed.
This project is licensed under the MIT License.