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]>
47 lines
1.3 KiB
Go
47 lines
1.3 KiB
Go
// websocket example — chat-style echo server.
|
|
//
|
|
// go run ./examples/websocket
|
|
// wscat -c ws://localhost:8080/ws
|
|
//
|
|
// # Untyped on purpose — the request never completes
|
|
//
|
|
// An upgrade hands the connection to another protocol: there is no response
|
|
// body for a typed op's Out to be, and the exchange that follows is frames in
|
|
// both directions for as long as the peer stays. A typed op is one decode, one
|
|
// call, one serialize, so it cannot express this — which is why wsx.Upgrade
|
|
// takes an untyped handler and not a TypedHandler.
|
|
//
|
|
// The cost is the same one every untyped route pays: no OpenAPI entry, no MCP
|
|
// tool, not reachable by zip.Call. Anything with a request-and-response shape
|
|
// belongs in a typed op instead — see examples/hello.
|
|
package main
|
|
|
|
import (
|
|
"log"
|
|
|
|
"github.com/zap-proto/zip"
|
|
"github.com/zap-proto/zip/middleware"
|
|
"github.com/zap-proto/zip/wsx"
|
|
)
|
|
|
|
func main() {
|
|
app := zip.New(zip.Config{AppName: "websocket"})
|
|
app.Use(middleware.Recover(), middleware.RequestID())
|
|
|
|
app.Raw("GET", "/ws", wsx.Upgrade(func(c *wsx.Conn) error {
|
|
log.Printf("ws connection from %s", c.RemoteAddr())
|
|
for {
|
|
mt, msg, err := c.ReadMessage()
|
|
if err != nil {
|
|
log.Printf("ws read: %v", err)
|
|
return err
|
|
}
|
|
if err := c.WriteMessage(mt, msg); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}))
|
|
|
|
log.Fatal(app.Listen("http://:8080"))
|
|
}
|