Skip to main content
Handlers control what your app does when it receives a request.
Each handler is a Go function with this signature:
func(c *mizu.Ctx) error
It takes a *mizu.Ctx, which gives access to the request, response, and helper methods for reading input and writing output.

Define handlers

Here is a simple example that sends a plain text response.
func home(c *mizu.Ctx) error {
	return c.Text(200, "Hello, Mizu!")
}

func main() {
	app := mizu.New()
	app.Get("/", home)
	app.Listen(":3000")
}
If a handler returns an error, Mizu catches it and passes it to the global error handler if one is set.

Read request data

Handlers can read data from path parameters, query strings, forms, JSON bodies, or cookies.
func createUser(c *mizu.Ctx) error {
	id := c.Param("id")
	form, _ := c.Form()
	name := form.Get("name")
	return c.JSON(200, map[string]string{
		"id":   id,
		"name": name,
	})
}

func main() {
	app := mizu.New()
	app.Post("/users/{id}", createUser)
	app.Listen(":3000")
}
Each method is clear and explicit, so you always control how data is read and validated.

Write responses

Handlers can send different response types using helper methods on c.
func showOK(c *mizu.Ctx) error {
	return c.Text(200, "OK")
}

func showCreated(c *mizu.Ctx) error {
	return c.JSON(201, map[string]string{"status": "created"})
}

func redirectHome(c *mizu.Ctx) error {
	return c.Redirect(302, "/welcome")
}

func noContent(c *mizu.Ctx) error {
	return c.NoContent()
}
These helpers set the right headers and status codes automatically.

Add middleware

Middleware wraps handlers to add shared behavior such as logging or authentication.
func requestLogger(next mizu.Handler) mizu.Handler {
	return func(c *mizu.Ctx) error {
		start := time.Now()
		err := next(c)
		c.Logger().Info("done", "path", c.Request().URL.Path, "time", time.Since(start))
		return err
	}
}

func main() {
	app := mizu.New()
	app.Use(requestLogger)
	app.Listen(":3000")
}
You can apply middleware globally, to route groups, or to individual routes.