mirror of
https://github.com/zap-proto/zip.git
synced 2026-10-19 15:38:57 +00:00
Ten of eleven examples declared their surface with untyped handlers. An untyped route registers no op, so every one of them modelled the single pattern that leaves an endpoint out of the OpenAPI document, out of the MCP tool list, out of the CLI and out of the call plane — while the README two files away calls that registry the one source of truth. The examples are where the default is learned, and they were teaching the wrong one. hello, subsystem-mount, migrate-from-chi, migrate-from-beego, plugin/billing, plugin/host and internal/testplugin now declare typed ops. The migrate-from-* examples show the port as the two steps it is: mechanical first (same handler shape, one edit, and invisible to every projection), typed second, because stopping after the first leaves exactly the surface you were migrating away from. sse-streaming and websocket stay untyped and now say why in the file header: a typed op is one decode, one call, one serialize, and a stream has no single value to return while an upgrade has no response body at all. That is the whole list of reasons — a stream, an upgrade, a proxied byte range, a non-JSON body. express-in-zip registered two routes on app.Fiber(), BELOW zip entirely: no *zip.Ctx, no error handler, no middleware, no Authorizer, and no projection — served by the app but not governed by it, which is a security seam and not a shortcut. Both move onto the zip router. /runtime/:lang becomes a typed op, so "run this source in this language" is now in the document and is a tool an agent can call; the legacy JS wildcard stays a wildcard and says what that costs. testplugin's catch-all stays untyped for the reason it exists: it answers paths that do not exist, so a host mounting it at two prefixes can tell its own 404 from the plugin's. A wildcard is deliberately not one named endpoint with one schema. Every example was run and probed, not just built: 30 assertions across all eleven, including the openapi/mcp routes the typed ops now light up. 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"})
|
|
|
|
zip.Get(app, "/v1/billing/invoices", listInvoices)
|
|
zip.Post(app, "/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
|
|
}
|