The handler signature
Every Mizu handler has the same signature:| Part | Purpose |
|---|---|
c *mizu.Ctx | The context - contains request data and response helpers |
error | Return value - nil for success, an error to trigger error handling |
Handler type:
Writing handlers
Basic handler
- Receives the context
cwith request info - Returns
c.Text(200, "...")which sends a response - The
errorreturn isnil(success) becausec.Textreturnsnilon success
Inline handlers
For simple routes, define handlers inline:Reading request data
Use context methods to read from the request:Processing JSON input
Returning errors
Handlers can return errors to indicate failures:- Mizu calls your
ErrorHandlerif you set one - Otherwise, it logs the error and returns 500
Response methods
Handlers send responses using context methods:Handler patterns
Early returns for validation
Separating concerns
Keep handlers thin by extracting business logic:Handler factories
Create handlers that share configuration:Summary
| Concept | Description |
|---|---|
| Signature | func(c *mizu.Ctx) error |
| Context | Request data and response helpers |
| Return nil | Success - response already sent |
| Return error | Triggers error handler |
| Response | Use c.Text(), c.JSON(), etc. |