zaprpc

package
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 5 Imported by: 0

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

View Source
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

View Source
var ErrNoService = errors.New("zaprpc: service not registered")

ErrNoService is returned by Dispatch when the service is unregistered.

Functions

func DecodeResponse

func DecodeResponse(data []byte) ([]byte, error)

DecodeResponse extracts the payload from a response frame.

func EncodeEnvelope

func EncodeEnvelope(e Envelope) []byte

EncodeEnvelope builds the canonical ZAP RPC frame for e.

func EncodeResponse

func EncodeResponse(payload []byte) []byte

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

func HTTPHandler(reg *Registry) fiber.Handler

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

type Envelope struct {
	Service string
	Method  string
	Payload []byte
}

Envelope is the decoded form of a ZAP RPC frame.

func DecodeEnvelope

func DecodeEnvelope(data []byte) (Envelope, error)

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 NewRegistry

func NewRegistry() *Registry

NewRegistry constructs an empty registry.

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).

func (*Registry) Get

func (r *Registry) Get(name string) Service

Get returns the service for name, or nil.

func (*Registry) Names

func (r *Registry) Names() []string

Names returns the registered service names.

func (*Registry) Register

func (r *Registry) Register(s Service)

Register adds a service. Calling twice with the same name overwrites (caller bug).

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL