Use Ctx to access the request, response, and logger inside a handler.
Every handler in Mizu receives a *mizu.Ctx - short for context. This is your primary interface for working with HTTP requests and responses. Understanding Ctx is essential for building any Mizu application.
In web development, context refers to all the information associated with a single HTTP request. When a user visits your website:
Their browser sends a request (URL, headers, body)
Your server processes it and creates a response (status, headers, body)
Various metadata is tracked (timing, request ID, user info)
Mizu wraps all of this into a single Ctx object, giving you easy access to everything you need.
The Ctx type is a thin wrapper around Go’s standard http.Request and http.ResponseWriter. It adds convenience methods while maintaining full compatibility with the standard library.
package mainimport ( "github.com/go-mizu/mizu")func hello(c *mizu.Ctx) error { // c gives you access to everything about this request name := c.Query("name") if name == "" { name = "World" } return c.Text(200, "Hello, "+name+"!")}func main() { app := mizu.New() app.Get("/hello", hello) app.Listen(":3000")}
Visit http://localhost:3000/hello?name=Mizu and you’ll see “Hello, Mizu!”.
The response writer is how you send data back to the client. While Mizu provides convenient helper methods, you can also access the raw writer:
func rawResponse(c *mizu.Ctx) error { w := c.Writer() // Set status code w.WriteHeader(http.StatusOK) // Write response body w.Write([]byte("Hello from raw writer!")) return nil}
Once you call w.WriteHeader() or w.Write(), headers are sent to the client and cannot be modified. Always set headers before writing the body.
Always pass the request context to database and external service calls:
func getUser(c *mizu.Ctx) error { ctx := c.Context() id := c.Param("id") // The context will cancel the query if the request is cancelled user, err := db.GetUserByID(ctx, id) if err != nil { return err } return c.JSON(200, user)}
func setHeaders(c *mizu.Ctx) error { // Use c.Header() to set response headers c.Header().Set("X-Custom-Header", "custom-value") c.Header().Set("Cache-Control", "max-age=3600") c.Header().Add("Set-Cookie", "session=abc123") // IMPORTANT: Set headers BEFORE writing the response body return c.Text(200, "Headers set!")}