Documentation
¶
Overview ¶
Package zaprpc is an OPTIONAL named-service RPC dispatch helper for zip apps that want a Cap'n-Proto/gRPC-style service registry (name → method → handler) rather than plain REST routes.
It is DECOUPLED from the transport: zip's primary transport is ZAP, wired once in the framework (App.Listen serves the whole fiber handler over zap-proto/http — the routes ARE the ZAP surface, no registry needed). This package is for the separate case where you want to expose generated zapc <svc>_server.go services by name; mount it as an ordinary route with zaprpc.HTTPHandler(registry) (see http.go), reachable over either transport.
Index ¶
Constants ¶
const ( HeaderService = "X-ZAP-Service" HeaderMethod = "X-ZAP-Method" )
HTTP header hints. The envelope in the body is canonical; these are a fast-path so a router/proxy can route without decoding the body. On disagreement the binary envelope wins (see decodeRequest).
Variables ¶
var ErrNoService = errors.New("zaprpc: service not registered")
ErrNoService is returned by Dispatch when the service is unregistered.
Functions ¶
func DecodeResponse ¶
DecodeResponse extracts the payload from a response frame.
func EncodeEnvelope ¶
EncodeEnvelope builds the canonical ZAP RPC frame for e.
func EncodeResponse ¶
EncodeResponse wraps a method's response payload in a ZAP frame. The response uses the same envelope shape with empty service/method so a single decoder handles both directions; callers that only need the payload can use DecodeResponse.
func HTTPHandler ¶
HTTPHandler returns a fiber.Handler that serves the ZAP RPC plane over HTTP POST. The request body is a canonical ZAP envelope (service, method, payload); the handler decodes it, dispatches through reg, and writes a ZAP response envelope. Returns fiber.Handler (not zip.Handler) because zaprpc is imported by zip — depending back would cycle.
Mount it on any zip/Fiber router:
app.Fiber().Post("/zap", zaprpc.HTTPHandler(app.ZAPRegistry()))
Types ¶
type Envelope ¶
Envelope is the decoded form of a ZAP RPC frame.
func DecodeEnvelope ¶
DecodeEnvelope parses a ZAP RPC frame. Returns an error if the bytes are not a valid ZAP message.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry holds a set of named services, dispatched via HTTPHandler.
func (*Registry) Dispatch ¶
func (r *Registry) Dispatch(ctx context.Context, service, method string, payload []byte) ([]byte, error)
Dispatch invokes the named service+method against the registry. The wire-decode happens upstream (HTTPHandler decodes the request envelope, then calls Dispatch to route it to the right handler).
type Service ¶
type Service interface {
// Name returns the service identifier (e.g. "validate.v1").
Name() string
// Handle dispatches one RPC call on the service. method is the
// fully-qualified method name; payload is the wire-encoded ZAP
// request body; the returned bytes are the wire-encoded response.
Handle(ctx context.Context, method string, payload []byte) ([]byte, error)
}
Service is the minimal ZAP service interface zip can dispatch to. zapc-generated <svc>_server.go satisfies this naturally.