Files
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

67 lines
2.2 KiB
Go

// migrate-from-chi example — chi → zip via stdlib adapter.
//
// Use zip.AdaptNetHTTP(chiRouter) to mount an existing chi.Router as a
// migration step. Replace adapted routes with native zip handlers when
// feasible; the adapter costs ~5% perf vs native Fiber dispatch.
//
// chi is NOT a dep of this example — we adapt any http.Handler, and a
// chi.Router satisfies that interface natively.
//
// The adapted subtree is a wildcard route and registers no operation, so nothing
// behind /legacy/chi is in the OpenAPI document, the MCP tool list, the CLI or
// the call plane. That is the price of not rewriting it yet, and it is the
// reason to: new work goes in as a TYPED op, and a native route added later
// still wins by specificity, so a legacy path can be replaced one op at a time
// with no un-mount step.
package main
import (
"context"
"log"
"net/http"
"github.com/zap-proto/zip"
"github.com/zap-proto/zip/middleware"
)
// legacyHandler stands in for an existing chi.Router. Same shape
// (http.Handler) so zip.AdaptNetHTTP works without changes.
type legacyHandler struct{}
func (legacyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"legacy":true}`))
}
// GetUserIn addresses one user; `id` is the path segment.
type GetUserIn struct {
ID string `json:"id"`
}
// User is one user record.
type User struct {
ID string `json:"id"`
}
// GetUser returns one user by id.
func getUser(_ context.Context, in *GetUserIn) (*User, error) {
return &User{ID: in.ID}, nil
}
func main() {
app := zip.New(zip.Config{AppName: "migrate-from-chi"})
app.Use(middleware.Recover())
// New work goes in typed: one declaration, and the route, the document, the
// tool list, the CLI and the call plane all follow from it.
app.Get("/v1/users/:id", getUser)
// Front the legacy chi router under /legacy/chi for incremental
// migration — one adapted wildcard route. Replace one path at a time
// with native zip handlers; a native route added later wins by
// specificity, no un-mount step needed.
app.Group("/legacy/chi").Raw(zip.MethodAll, "/*", zip.AdaptNetHTTP(legacyHandler{}))
log.Fatal(app.Listen("http://:8080"))
}