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]>
52 lines
1.6 KiB
Go
52 lines
1.6 KiB
Go
// sse-streaming example — Server-Sent Events via c.SendStreamWriter.
|
|
// LLM-gateway pattern: stream tokens as they arrive from upstream.
|
|
//
|
|
// go run ./examples/sse-streaming
|
|
// curl -N http://localhost:8080/v1/stream
|
|
//
|
|
// # Untyped on purpose — one of the few places it is right
|
|
//
|
|
// A typed op is (In → Out): zip decodes the input, runs the handler ONCE, and
|
|
// serializes the single value it returns. This route has no single value to
|
|
// return. It holds the connection open and writes frames as they arrive, which
|
|
// is what SSE IS, so there is nothing for a response schema to describe and
|
|
// nothing an MCP tools/call could hand back.
|
|
//
|
|
// The cost is real and worth stating: this route is in no OpenAPI document, is
|
|
// no MCP tool, and no service can reach it with zip.Call. Everything that CAN
|
|
// name its output should be a typed op — see examples/hello. Reach for an
|
|
// untyped handler when the response is a stream, an upgrade, a proxied byte
|
|
// range or a non-JSON body, and for nothing else.
|
|
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/zap-proto/zip"
|
|
)
|
|
|
|
func main() {
|
|
app := zip.New(zip.Config{AppName: "sse-streaming"})
|
|
|
|
app.Raw("GET", "/v1/stream", func(c *zip.Ctx) error {
|
|
c.SetHeader("Content-Type", "text/event-stream")
|
|
c.SetHeader("Cache-Control", "no-cache")
|
|
c.SetHeader("Connection", "keep-alive")
|
|
|
|
return c.SendStreamWriter(func(w *bufio.Writer) {
|
|
for i := 0; i < 10; i++ {
|
|
_, _ = fmt.Fprintf(w, "data: tick %d\n\n", i)
|
|
if err := w.Flush(); err != nil {
|
|
return
|
|
}
|
|
time.Sleep(200 * time.Millisecond)
|
|
}
|
|
})
|
|
})
|
|
|
|
log.Fatal(app.Listen("http://:8080"))
|
|
}
|