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]>
57 lines
1.6 KiB
Go
57 lines
1.6 KiB
Go
// Hello, zip — minimal example.
|
|
//
|
|
// go run ./examples/hello
|
|
// curl http://localhost:8080/hello
|
|
// curl http://localhost:8080/.well-known/openapi.json # the same two ops
|
|
// curl -XPOST -d '{"method":"tools/list"}' http://localhost:8080/mcp
|
|
//
|
|
// Both routes are TYPED ops, which is the default. zip.Get[In, Out] registers
|
|
// ONE operation, and zip projects it into the REST route, the OpenAPI document,
|
|
// an MCP tool, a `<service> <operation>` command line and a by-name call plane.
|
|
// An untyped app.Get(path, func(c *zip.Ctx) error) registers no operation and
|
|
// therefore appears in NONE of them — see examples/sse-streaming for the case
|
|
// where that is still the right trade.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
|
|
"github.com/zap-proto/zip"
|
|
"github.com/zap-proto/zip/middleware"
|
|
)
|
|
|
|
// Greeting is what GET /hello answers.
|
|
type Greeting struct {
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
// Health is what GET /health answers.
|
|
type Health struct {
|
|
Status string `json:"status"`
|
|
}
|
|
|
|
// Empty is an operation that takes nothing. A GET binds its input from the URL,
|
|
// and these two read no part of it.
|
|
type Empty struct{}
|
|
|
|
func main() {
|
|
app := zip.New(zip.Config{AppName: "hello"})
|
|
app.Use(middleware.Recover(), middleware.RequestID())
|
|
|
|
app.Get("/hello", hello)
|
|
app.Get("/health", health)
|
|
|
|
log.Fatal(app.Listen("http://:8080"))
|
|
}
|
|
|
|
// Hello greets the caller.
|
|
func hello(_ context.Context, _ *Empty) (*Greeting, error) {
|
|
return &Greeting{Message: "hello world"}, nil
|
|
}
|
|
|
|
// Health reports whether the service is up.
|
|
func health(_ context.Context, _ *Empty) (*Health, error) {
|
|
return &Health{Status: "ok"}, nil
|
|
}
|