In this lesson, you will learn what middleware is and how to use it in Mizu. Middleware is a function that runs before or after your main handler. It can log requests, check authentication, handle errors, or modify responses, all without changing your route logic. Mizu middleware follows Go’sDocumentation Index
Fetch the complete documentation index at: https://docs.go-mizu.dev/llms.txt
Use this file to discover all available pages before exploring further.
net/http design, so you can use standard handlers or write your own in a simple, composable way.
Code
Create a file namedmain.go and add this code:
Run
Start your server:-
http://localhost:8080/
Response:
Home page - no auth requiredThe logger middleware runs for this route and prints something like:INFO request completed method=GET path=/ status=200 duration=1.2ms -
http://localhost:8080/dashboard
Response:
Unauthorized -
Add a token to simulate an authenticated request:
http://localhost:8080/dashboard?token=secret
Response:
Welcome to your dashboard
How it works
Middleware in Mizu is a function that takes amizu.Handler and returns another mizu.Handler.
Each middleware can decide whether to continue to the next handler or stop early.
| Type of Middleware | Description | Example |
|---|---|---|
| Global middleware | Runs for every route, used for logging or errors. | app.Use(logger) |
| Route-specific middleware | Runs only on certain routes. | app.With(auth).Get("/dashboard", dashboard) |
| Middleware chain | Wraps multiple layers around your handler. | Logging → Auth → Handler |
*mizu.Ctx object and can read or modify the request and response. The next(c) call runs the next function in the chain.