Documentation
¶
Index ¶
- Constants
- func GetAuthToken(r *http.Request) any
- func HasUnsubstitutedParam(params map[string]string) bool
- func ListenAndServe(addr string, handler http.Handler) error
- func MatchPattern(pattern, path string) (map[string]string, bool)
- func SetAuthToken(r *http.Request, token any) *http.Request
- func SplitPath(path string) []string
- func SubstituteParams(rpcParams map[string]string, routeParams map[string]string) map[string]string
- func ToProtoBytes(v any) string
- type Assets
- type CORSConfig
- type Dispatcher
- type DispatcherOption
- type MessageReader
- type Middleware
- type PreloadEngine
- type PreloadEngineConfig
- type PreloadFunc
- type PreloadedRpc
- type RouteSpec
- type RpcError
- type RpcHandler
- type RpcSpec
- type StreamAdapter
- type StreamHandler
- type UnaryHandler
- type ViteManifest
- type ViteManifestEntry
Constants ¶
const ( CodeValidationError = "VALIDATION_ERROR" CodeNotFound = "NOT_FOUND" CodeAlreadyExists = "ALREADY_EXISTS" CodeUnauthenticated = "UNAUTHENTICATED" CodePermissionDenied = "PERMISSION_DENIED" CodeRateLimited = "RATE_LIMITED" CodeInternal = "INTERNAL" )
Error codes for structured RPC error responses.
Variables ¶
This section is empty.
Functions ¶
func GetAuthToken ¶
GetAuthToken retrieves the auth token from the request context. Returns nil if no token has been set.
func HasUnsubstitutedParam ¶
HasUnsubstitutedParam checks if any parameter values still contain unresolved :param placeholders.
func ListenAndServe ¶
ListenAndServe starts an HTTP server and blocks until a SIGINT or SIGTERM signal is received, at which point it initiates a graceful shutdown with a 30-second timeout. Returns http.ErrServerClosed on clean shutdown.
func MatchPattern ¶
MatchPattern matches a URL pattern against a path, extracting route parameters.
func SetAuthToken ¶
SetAuthToken returns a new request with the given token stored in its context.
func SubstituteParams ¶
SubstituteParams replaces :param placeholders in RPC params with actual route parameter values.
func ToProtoBytes ¶
ToProtoBytes marshals a proto message, gzip-compresses it, and base64-encodes the result.
Types ¶
type Assets ¶
Assets holds the resolved asset paths from Vite manifest
func LoadAssetsFromManifest ¶
LoadAssetsFromManifest reads the Vite manifest to get hashed asset filenames.
type CORSConfig ¶
type CORSConfig struct {
AllowedOrigins []string // specific origins, or ["*"] for all
AllowOrigin func(origin string) bool // dynamic check, takes precedence over AllowedOrigins
AllowedHeaders []string // defaults to standard RPC headers if nil
}
CORSConfig controls Cross-Origin Resource Sharing behavior.
type Dispatcher ¶
type Dispatcher struct {
Unary map[string]UnaryHandler
Streaming map[string]StreamHandler
// contains filtered or unexported fields
}
Dispatcher routes RPC calls to registered handlers.
func NewDispatcher ¶
func NewDispatcher(opts ...DispatcherOption) *Dispatcher
NewDispatcher creates a new Dispatcher with the given options.
func (*Dispatcher) ServeHTTP ¶
func (d *Dispatcher) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements http.Handler for the RPC dispatcher.
func (*Dispatcher) Use ¶
func (d *Dispatcher) Use(m Middleware)
Use adds a middleware to the dispatcher. Middlewares are applied in order: first added = outermost (runs first), last added = innermost (runs last, closest to handler).
type DispatcherOption ¶
type DispatcherOption func(*Dispatcher)
DispatcherOption configures a Dispatcher.
func WithCORS ¶
func WithCORS(config CORSConfig) DispatcherOption
WithCORS sets the CORS configuration for the dispatcher.
type MessageReader ¶
type MessageReader struct {
// contains filtered or unexported fields
}
MessageReader reads length-prefixed messages from a byte buffer. Each message is expected to be preceded by a 4-byte big-endian length prefix, matching the format used by StreamAdapter.Send and the client streaming transport.
func NewMessageReader ¶
func NewMessageReader(data []byte) *MessageReader
NewMessageReader creates a MessageReader over the given data.
func (*MessageReader) Next ¶
func (r *MessageReader) Next() ([]byte, error)
Next returns the next message from the buffer. Returns io.EOF when no more messages are available.
type Middleware ¶
type Middleware func(next RpcHandler) RpcHandler
Middleware wraps an RpcHandler, allowing pre/post processing of RPC calls.
func AuthMiddleware ¶
func AuthMiddleware(validate func(r *http.Request) any) Middleware
AuthMiddleware creates a middleware that validates requests using the provided function. If validate returns a non-nil token, it's stored in the request context via SetAuthToken. If validate returns nil, the request proceeds without a token (unauthenticated). This allows handlers to decide individually whether auth is required.
type PreloadEngine ¶
type PreloadEngine struct {
Routes []RouteSpec
PreloadFunc PreloadFunc
// contains filtered or unexported fields
}
PreloadEngine handles route-based RPC preloading and HTML rendering.
func NewPreloadEngine ¶
func NewPreloadEngine(config PreloadEngineConfig) *PreloadEngine
func (*PreloadEngine) HandlePreloadEndpoint ¶
func (p *PreloadEngine) HandlePreloadEndpoint(w http.ResponseWriter, r *http.Request)
HandlePreloadEndpoint handles the /__preload?path=... endpoint used by the Vite plugin in dev mode.
func (*PreloadEngine) ServeHTML ¶
func (p *PreloadEngine) ServeHTML(w http.ResponseWriter, r *http.Request)
ServeHTML serves the HTML page with preloaded data for the matched route.
type PreloadEngineConfig ¶
type PreloadEngineConfig struct {
Routes []RouteSpec
PreloadFunc PreloadFunc
ManifestPath string // path to .vite/manifest.json, defaults to "public/.vite/manifest.json"
AppName string // defaults to "App"
}
type PreloadFunc ¶
type PreloadFunc func(ctx context.Context, r *http.Request, method string, params map[string]string) (request, response proto.Message, err error)
PreloadFunc is the callback that executes an RPC for preloading. It receives the context, method name, and substituted route params. It returns the request and response proto messages.
type PreloadedRpc ¶
type PreloadedRpc struct {
RequestBytes string `json:"requestBytes"`
ResponseBytes string `json:"responseBytes"`
}
PreloadedRpc contains base64-encoded gzip-compressed protobuf bytes for request and response
type RpcError ¶
type RpcError struct {
Code string `json:"code"`
Message string `json:"message"`
Details map[string]string `json:"details,omitempty"`
}
RpcError is a structured error that serializes to JSON for RPC responses.
func ErrAlreadyExists ¶
func ErrInternal ¶
func ErrNotFound ¶
func ErrPermissionDenied ¶
func ErrRateLimited ¶
func ErrUnauthenticated ¶
func ErrValidation ¶
type RpcHandler ¶
type RpcHandler func(w http.ResponseWriter, r *http.Request, method string, body []byte) ([]byte, error)
RpcHandler is the callback signature used by middleware and the dispatcher.
type StreamAdapter ¶
type StreamAdapter struct {
// contains filtered or unexported fields
}
StreamAdapter provides length-prefixed streaming over HTTP responses. Each message is sent with a 4-byte big-endian length prefix followed by the protobuf-encoded message bytes.
func NewStreamAdapter ¶
func NewStreamAdapter(w http.ResponseWriter) *StreamAdapter
func (*StreamAdapter) Send ¶
func (sa *StreamAdapter) Send(data []byte) error
Send writes a length-prefixed message to the stream.
func (*StreamAdapter) SendHeaders ¶
func (sa *StreamAdapter) SendHeaders() error
SendHeaders writes streaming response headers and flushes them to the client.
type StreamHandler ¶
StreamHandler handles a streaming RPC call. It receives the method name, request body, and a StreamAdapter for sending responses. It should return nil after writing to the stream.
type UnaryHandler ¶
type UnaryHandler func(w http.ResponseWriter, r *http.Request, method string, body []byte) ([]byte, error)
UnaryHandler handles a unary RPC call. It receives the method name and request body, and returns the serialized response bytes or an error.
func RequireAuth ¶
func RequireAuth(handler UnaryHandler) UnaryHandler
RequireAuth wraps a UnaryHandler to reject unauthenticated requests with 401. Use this on individual handlers that require authentication.
type ViteManifest ¶
type ViteManifest map[string]ViteManifestEntry
ViteManifest represents the Vite build manifest