mirror of
https://github.com/zap-proto/zip.git
synced 2026-10-19 15:38:57 +00:00
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]>
71 lines
2.1 KiB
Go
71 lines
2.1 KiB
Go
// The plugin. An ordinary zip app — no SDK, no schema, nothing plugin-specific
|
|
// except where it listens. Build it, and a host can load it.
|
|
//
|
|
// Its routes are TYPED ops, which is what makes a plugin worth loading: the host
|
|
// gains the routes AND their document, their MCP tools, their commands and their
|
|
// by-name call plane, because all of those are projections of the ops this
|
|
// binary registered. A plugin of untyped routes would hand the host traffic and
|
|
// nothing it can describe, address by name, or give to an agent.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
|
|
"github.com/zap-proto/zip"
|
|
)
|
|
|
|
// Invoice is one invoice.
|
|
type Invoice struct {
|
|
ID string `json:"id"`
|
|
Cents int `json:"cents"`
|
|
}
|
|
|
|
// Invoices is a page of invoices.
|
|
type Invoices struct {
|
|
Invoices []Invoice `json:"invoices"`
|
|
}
|
|
|
|
// ListIn filters the invoice list. A GET binds its input from the URL, so
|
|
// `?limit=1` reaches the field — and the document says so.
|
|
type ListIn struct {
|
|
Limit int `json:"limit"`
|
|
}
|
|
|
|
// ChargeIn is one charge to make.
|
|
type ChargeIn struct {
|
|
Cents int `json:"cents" validate:"required"`
|
|
}
|
|
|
|
// Charge is the result of making one.
|
|
type Charge struct {
|
|
Status string `json:"status"`
|
|
Cents int `json:"cents"`
|
|
}
|
|
|
|
func main() {
|
|
app := zip.New(zip.Config{AppName: "billing"})
|
|
|
|
app.Get("/v1/billing/invoices", listInvoices)
|
|
app.Post("/v1/billing/charge", charge)
|
|
|
|
// zip.Addr returns the socket a host asked this process to serve on, and
|
|
// falls back to the argument when it is run directly. That fallback is the
|
|
// whole reason the same binary works standalone and as a plugin.
|
|
log.Fatal(app.Listen(zip.Addr(":9654")))
|
|
}
|
|
|
|
// ListInvoices returns this org's invoices, newest first.
|
|
func listInvoices(_ context.Context, in *ListIn) (*Invoices, error) {
|
|
all := []Invoice{{ID: "inv_1", Cents: 1200}, {ID: "inv_2", Cents: 3400}}
|
|
if in.Limit > 0 && in.Limit < len(all) {
|
|
all = all[:in.Limit]
|
|
}
|
|
return &Invoices{Invoices: all}, nil
|
|
}
|
|
|
|
// Charge bills the caller's org for the given amount.
|
|
func charge(_ context.Context, in *ChargeIn) (*Charge, error) {
|
|
return &Charge{Status: "charged", Cents: in.Cents}, nil
|
|
}
|