Run code before handlers (globally or per route) and pass typed values through the RequestContext.
Middleware is a function that receives the RequestContext before a handler runs. It comes in two flavors: global middleware runs for every request before routing; handler middleware is attached to specific routes with @WithMiddleware and runs after the route is matched, just before the handler.
Write a middleware
A middleware shapes the request context. To pass data along, define a typed context key and set it:
Use global middleware for cross-cutting concerns (request tagging, telemetry, tenant resolution) and @WithMiddleware for anything only some routes need.