Skip to main content
A handler is a function that processes an HTTP request and produces a response. Every route in your Mizu app connects a URL pattern to a handler function.

The handler signature

Every Mizu handler has the same signature:
This signature is Mizu’s Handler type:

Writing handlers

Basic handler

The handler:
  1. Receives the context c with request info
  2. Returns c.Text(200, "...") which sends a response
  3. The error return is nil (success) because c.Text returns nil on 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:
When a handler returns a non-nil error:
  1. Mizu calls your ErrorHandler if you set one
  2. Otherwise, it logs the error and returns 500
Set a global error handler:

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

Next steps

Context

Deeper dive into request/response context.

Response

All the ways to send responses.