handler

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package handler provides OpenRPC spec generation from the method registry.

Package handler provides JSON-RPC request handling infrastructure.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ContextKey

type ContextKey string

ContextKey is a type for context keys to avoid collisions.

const (
	// ClientIDKey is the context key for the client ID.
	ClientIDKey ContextKey = "client_id"
	// AuthPayloadKey is the context key for the auth token payload.
	AuthPayloadKey ContextKey = "auth_payload"
)

type Dispatcher

type Dispatcher struct {
	// contains filtered or unexported fields
}

Dispatcher routes JSON-RPC requests to registered handlers.

func NewDispatcher

func NewDispatcher(registry *Registry) *Dispatcher

NewDispatcher creates a new dispatcher with the given registry.

func (*Dispatcher) BatchDispatch

func (d *Dispatcher) BatchDispatch(ctx context.Context, requests []*message.Request) []*message.Response

BatchDispatch handles a batch of JSON-RPC requests. Returns a slice of responses (some may be nil for notifications).

func (*Dispatcher) Dispatch

func (d *Dispatcher) Dispatch(ctx context.Context, req *message.Request) *message.Response

Dispatch handles a JSON-RPC request and returns a response. Returns nil for notifications (requests without ID).

func (*Dispatcher) DispatchBytes

func (d *Dispatcher) DispatchBytes(ctx context.Context, data []byte) ([]byte, error)

DispatchBytes parses and dispatches a JSON-RPC request from bytes. Returns the response bytes, or nil for notifications.

func (*Dispatcher) HandleMessage

func (d *Dispatcher) HandleMessage(ctx context.Context, data []byte) ([]byte, error)

HandleMessage handles an incoming message, determining if it's a single request or a batch, and returns the appropriate response(s).

func (*Dispatcher) Registry

func (d *Dispatcher) Registry() *Registry

Registry returns the underlying registry.

type HandlerFunc

type HandlerFunc func(ctx context.Context, params json.RawMessage) (interface{}, *message.Error)

HandlerFunc is the signature for RPC method handlers. It receives the context and raw params, and returns either a result or an error. If the result is nil and error is nil, an empty successful response is sent.

type MethodMeta

type MethodMeta struct {
	Summary     string
	Description string
	Params      []OpenRPCParam
	Result      *OpenRPCResult
	Errors      []string // Error names to reference
}

MethodMeta contains metadata for a registered method.

type MethodService

type MethodService interface {
	// RegisterMethods registers all methods provided by this service.
	RegisterMethods(r *Registry)
}

MethodService is an interface for services that register multiple methods.

type MiddlewareFunc

type MiddlewareFunc func(HandlerFunc) HandlerFunc

MiddlewareFunc is a function that wraps a HandlerFunc.

type OpenRPCComponents

type OpenRPCComponents struct {
	Schemas map[string]interface{} `json:"schemas,omitempty"`
	Errors  map[string]interface{} `json:"errors,omitempty"`
}

OpenRPCComponents contains reusable components.

type OpenRPCErrorRef

type OpenRPCErrorRef struct {
	Ref string `json:"$ref,omitempty"`
}

OpenRPCErrorRef references an error definition.

type OpenRPCExample

type OpenRPCExample struct {
	Name   string                   `json:"name"`
	Params []map[string]interface{} `json:"params"`
	Result map[string]interface{}   `json:"result,omitempty"`
}

OpenRPCExample represents a method example.

type OpenRPCInfo

type OpenRPCInfo struct {
	Title       string `json:"title"`
	Description string `json:"description"`
	Version     string `json:"version"`
}

OpenRPCInfo contains API metadata.

type OpenRPCMethod

type OpenRPCMethod struct {
	Name        string            `json:"name"`
	Summary     string            `json:"summary"`
	Description string            `json:"description,omitempty"`
	Params      []OpenRPCParam    `json:"params"`
	Result      *OpenRPCResult    `json:"result,omitempty"`
	Errors      []OpenRPCErrorRef `json:"errors,omitempty"`
	Examples    []OpenRPCExample  `json:"examples,omitempty"`
}

OpenRPCMethod represents a JSON-RPC method.

type OpenRPCParam

type OpenRPCParam struct {
	Name        string                 `json:"name"`
	Description string                 `json:"description,omitempty"`
	Required    bool                   `json:"required"`
	Schema      map[string]interface{} `json:"schema"`
}

OpenRPCParam represents a method parameter.

type OpenRPCResult

type OpenRPCResult struct {
	Name   string                 `json:"name"`
	Schema map[string]interface{} `json:"schema"`
}

OpenRPCResult represents a method result.

type OpenRPCServer

type OpenRPCServer struct {
	Name string `json:"name"`
	URL  string `json:"url"`
}

OpenRPCServer represents a server endpoint.

type OpenRPCSpec

type OpenRPCSpec struct {
	OpenRPC    string            `json:"openrpc"`
	Info       OpenRPCInfo       `json:"info"`
	Servers    []OpenRPCServer   `json:"servers"`
	Methods    []OpenRPCMethod   `json:"methods"`
	Components OpenRPCComponents `json:"components"`
}

OpenRPCSpec represents the OpenRPC specification.

func (*OpenRPCSpec) ToJSON

func (spec *OpenRPCSpec) ToJSON() ([]byte, error)

ToJSON returns the OpenRPC spec as JSON.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry holds registered RPC methods and provides lookup functionality.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new method registry.

func (*Registry) Clear

func (r *Registry) Clear()

Clear removes all registered handlers and middleware.

func (*Registry) GenerateOpenRPC

func (r *Registry) GenerateOpenRPC(info OpenRPCInfo, serverURL string) *OpenRPCSpec

GenerateOpenRPC generates an OpenRPC spec from the registry.

func (*Registry) Get

func (r *Registry) Get(method string) HandlerFunc

Get returns the handler for a method. It applies all registered middleware to the handler. Returns nil if the method is not registered.

func (*Registry) GetMeta

func (r *Registry) GetMeta(method string) MethodMeta

GetMeta returns the metadata for a method. If no metadata is registered, returns a default MethodMeta with just the method name.

func (*Registry) Has

func (r *Registry) Has(method string) bool

Has returns true if a handler is registered for the method.

func (*Registry) Methods

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

Methods returns a list of all registered methods.

func (*Registry) Register

func (r *Registry) Register(method string, handler HandlerFunc)

Register registers a handler for a method. If a handler is already registered for the method, it will be replaced.

func (*Registry) RegisterAll

func (r *Registry) RegisterAll(handlers map[string]HandlerFunc)

RegisterAll registers multiple handlers at once.

func (*Registry) RegisterService

func (r *Registry) RegisterService(svc MethodService)

RegisterService registers all methods from a MethodService.

func (*Registry) RegisterWithMeta

func (r *Registry) RegisterWithMeta(method string, handler HandlerFunc, meta MethodMeta)

RegisterWithMeta registers a handler with OpenRPC metadata.

func (*Registry) Unregister

func (r *Registry) Unregister(method string)

Unregister removes a handler for a method.

func (*Registry) Use

func (r *Registry) Use(mw MiddlewareFunc)

Use adds middleware to the registry. Middleware is applied in the order it is added.

Directories

Path Synopsis
Package methods provides JSON-RPC method implementations.
Package methods provides JSON-RPC method implementations.

Jump to

Keyboard shortcuts

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