Routing
Routing connects URLs to handler functions. Mizu uses Go 1.22’s enhancedServeMux which supports path parameters natively.
- Path parameters using
{param}syntax (Go 1.22ServeMuxpatterns) - HTTP method helpers:
Get,Post,Put,Patch,Delete,Head,Connect,Trace - Route groups with
Group()andPrefix()for organizing related endpoints - Scoped middleware with
With()to apply middleware to specific routes - Mount http.Handler with
Mount()for interoperability with standard handlers
Request handling
Reading data from requests is explicit and straightforward. Each method does one thing clearly.| Method | Purpose | Example |
|---|---|---|
c.Param("id") | Path parameter from URL | /users/{id} → c.Param("id") |
c.Query("page") | Single query parameter | ?page=2 → c.Query("page") |
c.QueryValues() | All query parameters | Returns url.Values |
c.Form() | Form data (POST) | Returns url.Values, error |
c.MultipartForm(max) | File uploads | Returns form, cleanup, error |
c.BindJSON(&v, max) | JSON body to struct | Validates and parses |
c.Cookie("name") | Read a cookie | Returns *http.Cookie, error |
Response helpers
Response methods write data with appropriate headers and status codes.| Method | Content-Type | Usage |
|---|---|---|
c.Text(code, s) | text/plain | Plain text responses |
c.JSON(code, v) | application/json | Structs, maps, slices |
c.HTML(code, s) | text/html | HTML strings |
c.File(code, path) | Auto-detected | Serve files from disk |
c.Download(code, path, name) | With disposition | Force download |
c.Stream(fn) | Custom | Streaming responses |
c.SSE(ch) | text/event-stream | Server-Sent Events |
c.Redirect(code, url) | - | HTTP redirects |
c.NoContent() | - | 204 No Content |
Middleware
Middleware wraps handlers to add behavior before or after request processing. The pattern is simple: take a handler, return a handler.Logging
Mizu includes a request logger that uses Go’sslog package for structured logging.
mizu.Auto- Colored text for terminals, JSON for non-terminals (default)mizu.Dev- Human-readable text formatmizu.Prod- JSON lines for log collectors
Error handling
Handlers return errors directly. Mizu catches returned errors and recovered panics in one central place.Server lifecycle
Mizu handles graceful shutdown automatically. When your server receives SIGINT (Ctrl+C) or SIGTERM, it:- Stops accepting new connections
- Waits for active requests to complete (up to timeout)
- Shuts down cleanly
Static files
Serve files from disk or embedded filesystems.Summary
Mizu stays small and predictable. Everything is plain Go code with no hidden behavior.| Category | Key point |
|---|---|
| Routing | Go 1.22 ServeMux patterns with {param} syntax |
| Request | Explicit methods: Param, Query, Form, BindJSON |
| Response | Type helpers: Text, JSON, HTML, File, SSE |
| Middleware | Simple func(Handler) Handler signature |
| Logging | Built-in with slog, configurable modes |
| Errors | Return from handlers, catch in one place |
| Lifecycle | Graceful shutdown with configurable timeout |