Files
zip/middleware.go
T
zeekayandHanzo Dev 5f71d2496b The router is three nouns, and every one of them is concrete
App, Group, Operation. A group is a prefix and what holds under it, an operation
is one typed route declared there, and the verb methods are generic methods —
which Go 1.27 allows on a concrete type and still refuses on an interface. So In
and Out come from the handler and the caller writes no type arguments:

	users := app.Group("/v1/users")
	users.Post("/", api.Create)

	mint := users.Group("/mint").With(middleware.Mint(gate))
	mint.Post("/deposit", api.Deposit)

A Group is a CLOSED HANDLE: two unexported fields, and no method that hands back
the app, the parent or the fiber underneath. A subsystem given the group at its
own prefix cannot reach above it, so confinement is a property of the type
instead of a rule something has to police — which is what a host needs in order
to hand out a bounded surface and know what comes back.

That is bought by giving up the router interface, deliberately. Router and
OpTarget are gone, and with them the ability to implement a route table outside
this package: a decorator is no longer a router, a gate is middleware on a group,
and a bound is a path. The five package-level verb functions go the same way —
a registration is a method on the thing it registers into.

Raw carries what is not a typed operation: a stream, an upgrade, a probe, a
foreign subtree. It names its method, and it is the longer spelling because
opting out of the typed model is the exception and should read like one.



Co-authored-by: Hanzo Dev <[email protected]>
2026-09-17 01:39:17 -07:00

46 lines
1.7 KiB
Go

package zip
// Middleware is a composable request transformer in the classic wrapping form:
// given the next Handler it returns a Handler that runs around it. This is a
// DIFFERENT tool from Use — they do different jobs and compose freely:
//
// - Use(Handler...) registers GLOBAL / prefix middleware. It runs for every
// matched route (or every route under a Group) in DECLARATION order and
// chains via c.Next(). Reach for it for ambient cross-cutting concerns
// that apply broadly: logging, recovery, request-id.
//
// - Middleware + With + Chain wrap ONE leaf handler explicitly, at
// registration time, with no c.Next() indirection. Reach for it when a
// specific endpoint needs a specific pipeline:
//
// app.With(RateLimit, CSRF).Post("/v1/keys", mintKey)
//
// wraps mintKey as RateLimit(CSRF(mintKey)): RateLimit is outermost and
// runs first, CSRF next, the handler last; any layer short-circuits by
// returning without calling next.
//
// A Middleware body is written by hand, no framework glue:
//
// func RequireCSRF(next zip.Handler) zip.Handler {
// return func(c *zip.Ctx) error {
// if !validCSRF(c) {
// return c.String(403, "bad csrf") // short-circuit
// }
// return next(c) // continue
// }
// }
type Middleware = func(next Handler) Handler
// Chain composes middleware left-to-right into one Middleware. Chain(a, b, c)
// nests as a(b(c(handler))): a is outermost (runs first inbound, last
// outbound), c innermost, wrapping the handler directly. Chain() with no
// arguments is the identity middleware.
func Chain(mw ...Middleware) Middleware {
return func(next Handler) Handler {
for i := len(mw) - 1; i >= 0; i-- {
next = mw[i](next)
}
return next
}
}