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]>
54 lines
1.4 KiB
Go
54 lines
1.4 KiB
Go
// zap-typed example — typed handler with auto-generated OpenAPI spec.
|
|
//
|
|
// go run ./examples/zap-typed
|
|
// curl -X POST http://localhost:8080/v1/validate \
|
|
// -H 'Content-Type: application/json' \
|
|
// -d '{"email":"[email protected]","age":30}'
|
|
// curl http://localhost:8080/.well-known/openapi.json | jq
|
|
// open http://localhost:8080/docs
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"strings"
|
|
|
|
"github.com/zap-proto/zip"
|
|
)
|
|
|
|
// ValidateRequest is the input type. zip derives an OpenAPI schema
|
|
// from these struct tags.
|
|
type ValidateRequest struct {
|
|
Email string `json:"email" validate:"required,minlen=3,maxlen=255"`
|
|
Age int `json:"age" validate:"required,min=18,max=120"`
|
|
}
|
|
|
|
// ValidateResponse is the output type. Same auto-schema derivation.
|
|
type ValidateResponse struct {
|
|
OK bool `json:"ok"`
|
|
Normalized string `json:"normalized"`
|
|
}
|
|
|
|
func main() {
|
|
app := zip.New(zip.Config{
|
|
AppName: "zap-typed",
|
|
OpenAPI: zip.OpenAPIConfig{
|
|
Title: "Validate API",
|
|
Description: "Example of zip's typed handler + auto-OpenAPI",
|
|
Version: "v0.1.0",
|
|
},
|
|
})
|
|
|
|
app.Post("/v1/validate", validate, zip.WithSummary("Validate an email and age"), zip.WithTags("validation"))
|
|
|
|
log.Fatal(app.Listen("http://:8080"))
|
|
}
|
|
|
|
func validate(ctx context.Context, in *ValidateRequest) (*ValidateResponse, error) {
|
|
_ = ctx
|
|
return &ValidateResponse{
|
|
OK: true,
|
|
Normalized: strings.ToLower(in.Email),
|
|
}, nil
|
|
}
|