Documentation
¶
Index ¶
- Constants
- Variables
- func GetErrorMessage(code int) string
- func NewExtension(opts ...ConfigOption) forge.Extension
- func NewExtensionWithConfig(config Config) forge.Extension
- type Config
- type ConfigOption
- func WithAuth(header string, tokens []string) ConfigOption
- func WithAutoExposeRoutes(enabled bool) ConfigOption
- func WithBatch(enabled bool) ConfigOption
- func WithBatchLimit(limit int) ConfigOption
- func WithConfig(cfg Config) ConfigOption
- func WithDiscovery(enabled bool) ConfigOption
- func WithEnabled(enabled bool) ConfigOption
- func WithEndpoint(endpoint string) ConfigOption
- func WithExcludePatterns(patterns []string) ConfigOption
- func WithIncludePatterns(patterns []string) ConfigOption
- func WithMaxRequestSize(size int64) ConfigOption
- func WithMethodPrefix(prefix string) ConfigOption
- func WithMetrics(enabled bool) ConfigOption
- func WithNamingStrategy(strategy string) ConfigOption
- func WithOpenRPC(enabled bool) ConfigOption
- func WithOpenRPCEndpoint(endpoint string) ConfigOption
- func WithRateLimit(perMinute int) ConfigOption
- func WithRequestTimeout(seconds int) ConfigOption
- func WithRequireConfig(required bool) ConfigOption
- func WithSchemaCache(enabled bool) ConfigOption
- func WithServerInfo(name, version string) ConfigOption
- type Error
- type Extension
- type Interceptor
- type Method
- type MethodHandler
- type ORPC
- type OpenRPCComponents
- type OpenRPCContact
- type OpenRPCDocument
- type OpenRPCError
- type OpenRPCInfo
- type OpenRPCLicense
- type OpenRPCMethod
- type OpenRPCParam
- type OpenRPCResult
- type OpenRPCServer
- type OpenRPCTag
- type ParamsSchema
- type PropertySchema
- type Request
- type Response
- type ResponseSchemaDef
- type ResultSchema
- type ServerStats
Constants ¶
const ( ErrParseError = -32700 // Invalid JSON ErrInvalidRequest = -32600 // Invalid Request object ErrMethodNotFound = -32601 // Method not found ErrInvalidParams = -32602 // Invalid method parameters ErrInternalError = -32603 // Internal JSON-RPC error ErrServerError = -32000 // Server error (implementation-defined) )
Standard JSON-RPC 2.0 error codes.
Variables ¶
var ( ErrMethodExists = errors.New("orpc: method already exists") ErrMethodNotFoundError = errors.New("orpc: method not found") ErrInvalidMethodName = errors.New("orpc: invalid method name") ErrDisabled = errors.New("orpc: extension is disabled") ErrBatchDisabled = errors.New("orpc: batch requests are disabled") ErrBatchTooLarge = errors.New("orpc: batch size exceeds limit") ErrRequestTooLarge = errors.New("orpc: request size exceeds limit") )
Package-level errors for internal use.
Functions ¶
func GetErrorMessage ¶
GetErrorMessage returns the standard message for a JSON-RPC error code.
func NewExtension ¶
func NewExtension(opts ...ConfigOption) forge.Extension
NewExtension creates a new oRPC extension with functional options. Config is loaded from ConfigManager by default, with options providing overrides.
Example:
// Load from ConfigManager (tries "extensions.orpc", then "orpc")
orpc.NewExtension()
// Override specific fields
orpc.NewExtension(
orpc.WithEnabled(true),
orpc.WithEndpoint("/rpc"),
orpc.WithAutoExposeRoutes(true),
)
// Require config from ConfigManager
orpc.NewExtension(orpc.WithRequireConfig(true))
func NewExtensionWithConfig ¶
NewExtensionWithConfig creates a new oRPC extension with a complete config. This is for backward compatibility or when config is fully known at initialization.
Types ¶
type Config ¶
type Config struct {
// Core
Enabled bool `json:"enabled" mapstructure:"enabled" yaml:"enabled"`
Endpoint string `json:"endpoint" mapstructure:"endpoint" yaml:"endpoint"`
OpenRPCEndpoint string `json:"openrpc_endpoint" mapstructure:"openrpc_endpoint" yaml:"openrpc_endpoint"`
// Server info for OpenRPC
ServerName string `json:"server_name" mapstructure:"server_name" yaml:"server_name"`
ServerVersion string `json:"server_version" mapstructure:"server_version" yaml:"server_version"`
// Auto-exposure
AutoExposeRoutes bool `json:"auto_expose_routes" mapstructure:"auto_expose_routes" yaml:"auto_expose_routes"`
MethodPrefix string `json:"method_prefix" mapstructure:"method_prefix" yaml:"method_prefix"`
ExcludePatterns []string `json:"exclude_patterns" mapstructure:"exclude_patterns" yaml:"exclude_patterns"`
IncludePatterns []string `json:"include_patterns" mapstructure:"include_patterns" yaml:"include_patterns"`
// Features
EnableOpenRPC bool `json:"enable_openrpc" mapstructure:"enable_openrpc" yaml:"enable_openrpc"`
EnableDiscovery bool `json:"enable_discovery" mapstructure:"enable_discovery" yaml:"enable_discovery"`
EnableBatch bool `json:"enable_batch" mapstructure:"enable_batch" yaml:"enable_batch"`
BatchLimit int `json:"batch_limit" mapstructure:"batch_limit" yaml:"batch_limit"`
// Naming strategy: "path" (default), "method", "custom"
NamingStrategy string `json:"naming_strategy" mapstructure:"naming_strategy" yaml:"naming_strategy"`
// Security
RequireAuth bool `json:"require_auth" mapstructure:"require_auth" yaml:"require_auth"`
AuthHeader string `json:"auth_header" mapstructure:"auth_header" yaml:"auth_header"`
AuthTokens []string `json:"auth_tokens" mapstructure:"auth_tokens" yaml:"auth_tokens"`
RateLimitPerMinute int `json:"rate_limit_per_minute" mapstructure:"rate_limit_per_minute" yaml:"rate_limit_per_minute"`
// Performance
MaxRequestSize int64 `json:"max_request_size" mapstructure:"max_request_size" yaml:"max_request_size"`
RequestTimeout int `json:"request_timeout" mapstructure:"request_timeout" yaml:"request_timeout"` // seconds
SchemaCache bool `json:"schema_cache" mapstructure:"schema_cache" yaml:"schema_cache"`
// Observability
EnableMetrics bool `json:"enable_metrics" mapstructure:"enable_metrics" yaml:"enable_metrics"`
// Config loading flag (internal use)
RequireConfig bool `json:"-" mapstructure:"-" yaml:"-"`
}
Config holds the oRPC extension configuration.
func (*Config) ShouldExpose ¶
ShouldExpose checks if a route should be exposed based on include/exclude patterns.
type ConfigOption ¶
type ConfigOption func(*Config)
ConfigOption is a functional option for configuring the oRPC extension.
func WithAuth ¶
func WithAuth(header string, tokens []string) ConfigOption
WithAuth configures authentication.
func WithAutoExposeRoutes ¶
func WithAutoExposeRoutes(enabled bool) ConfigOption
WithAutoExposeRoutes sets auto-exposure of routes.
func WithBatch ¶
func WithBatch(enabled bool) ConfigOption
WithBatch enables/disables batch requests.
func WithBatchLimit ¶
func WithBatchLimit(limit int) ConfigOption
WithBatchLimit sets the maximum batch size.
func WithDiscovery ¶
func WithDiscovery(enabled bool) ConfigOption
WithDiscovery enables/disables method discovery endpoint.
func WithEndpoint ¶
func WithEndpoint(endpoint string) ConfigOption
WithEndpoint sets the RPC endpoint.
func WithExcludePatterns ¶
func WithExcludePatterns(patterns []string) ConfigOption
WithExcludePatterns sets the patterns to exclude from exposure.
func WithIncludePatterns ¶
func WithIncludePatterns(patterns []string) ConfigOption
WithIncludePatterns sets the patterns to include for exposure.
func WithMaxRequestSize ¶
func WithMaxRequestSize(size int64) ConfigOption
WithMaxRequestSize sets the maximum request size.
func WithMethodPrefix ¶
func WithMethodPrefix(prefix string) ConfigOption
WithMethodPrefix sets the method name prefix.
func WithMetrics ¶
func WithMetrics(enabled bool) ConfigOption
WithMetrics enables/disables metrics collection.
func WithNamingStrategy ¶
func WithNamingStrategy(strategy string) ConfigOption
WithNamingStrategy sets the method naming strategy.
func WithOpenRPC ¶
func WithOpenRPC(enabled bool) ConfigOption
WithOpenRPC enables/disables OpenRPC schema generation.
func WithOpenRPCEndpoint ¶
func WithOpenRPCEndpoint(endpoint string) ConfigOption
WithOpenRPCEndpoint sets the OpenRPC schema endpoint.
func WithRateLimit ¶
func WithRateLimit(perMinute int) ConfigOption
WithRateLimit sets the rate limit per minute.
func WithRequestTimeout ¶
func WithRequestTimeout(seconds int) ConfigOption
WithRequestTimeout sets the request timeout in seconds.
func WithRequireConfig ¶
func WithRequireConfig(required bool) ConfigOption
WithRequireConfig sets whether config must be loaded from ConfigManager.
func WithSchemaCache ¶
func WithSchemaCache(enabled bool) ConfigOption
WithSchemaCache enables/disables schema caching.
func WithServerInfo ¶
func WithServerInfo(name, version string) ConfigOption
WithServerInfo sets the server name and version for OpenRPC.
type Error ¶
type Error struct {
Code int `json:"code"`
Message string `json:"message"`
Data any `json:"data,omitempty"`
}
Error represents a JSON-RPC 2.0 error.
type Extension ¶
type Extension struct {
*forge.BaseExtension
// contains filtered or unexported fields
}
Extension implements forge.Extension for oRPC (JSON-RPC 2.0 / OpenRPC) functionality.
type Interceptor ¶
Interceptor wraps method execution.
type Method ¶
type Method struct {
Name string
Description string
Params *ParamsSchema
Result *ResultSchema
Handler MethodHandler
RouteInfo any // Reference to forge.RouteInfo
Tags []string
Deprecated bool
Metadata map[string]any
}
Method represents a registered JSON-RPC method.
type MethodHandler ¶
MethodHandler is a function that executes a JSON-RPC method.
type ORPC ¶
type ORPC interface {
// Method management
RegisterMethod(method *Method) error
GetMethod(name string) (*Method, error)
ListMethods() []Method
// Route introspection (auto-expose)
GenerateMethodFromRoute(route forge.RouteInfo) (*Method, error)
// Execution
HandleRequest(ctx context.Context, req *Request) *Response
HandleBatch(ctx context.Context, requests []*Request) []*Response
// OpenRPC schema
OpenRPCDocument() *OpenRPCDocument
// Interceptors
Use(interceptor Interceptor)
// Stats
GetStats() ServerStats
// Internal
SetRouter(router forge.Router)
}
ORPC represents the oRPC server interface.
type OpenRPCComponents ¶
OpenRPCComponents contains reusable schemas.
type OpenRPCContact ¶
type OpenRPCContact struct {
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
Email string `json:"email,omitempty"`
}
OpenRPCContact contains contact information.
type OpenRPCDocument ¶
type OpenRPCDocument struct {
OpenRPC string `json:"openrpc"`
Info *OpenRPCInfo `json:"info"`
Methods []*OpenRPCMethod `json:"methods"`
Components *OpenRPCComponents `json:"components,omitempty"`
Servers []*OpenRPCServer `json:"servers,omitempty"`
}
OpenRPCDocument represents an OpenRPC document.
type OpenRPCError ¶
OpenRPCError represents an error in OpenRPC schema.
type OpenRPCInfo ¶
type OpenRPCInfo struct {
Title string `json:"title"`
Version string `json:"version"`
Description string `json:"description,omitempty"`
TermsOfService string `json:"termsOfService,omitempty"`
Contact *OpenRPCContact `json:"contact,omitempty"`
License *OpenRPCLicense `json:"license,omitempty"`
}
OpenRPCInfo contains API metadata.
type OpenRPCLicense ¶
OpenRPCLicense contains license information.
type OpenRPCMethod ¶
type OpenRPCMethod struct {
Name string `json:"name"`
Summary string `json:"summary,omitempty"`
Description string `json:"description,omitempty"`
Tags []*OpenRPCTag `json:"tags,omitempty"`
Params []*OpenRPCParam `json:"params,omitempty"`
Result *OpenRPCResult `json:"result,omitempty"`
Deprecated bool `json:"deprecated,omitempty"`
Errors []*OpenRPCError `json:"errors,omitempty"`
}
OpenRPCMethod represents a method in OpenRPC schema.
type OpenRPCParam ¶
type OpenRPCParam struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Required bool `json:"required,omitempty"`
Schema map[string]any `json:"schema"`
}
OpenRPCParam represents a parameter in OpenRPC schema.
type OpenRPCResult ¶
type OpenRPCResult struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Schema map[string]any `json:"schema"`
}
OpenRPCResult represents a result in OpenRPC schema.
type OpenRPCServer ¶
type OpenRPCServer struct {
Name string `json:"name,omitempty"`
URL string `json:"url"`
Description string `json:"description,omitempty"`
}
OpenRPCServer describes a server.
type OpenRPCTag ¶
type OpenRPCTag struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
}
OpenRPCTag represents a tag for grouping methods.
type ParamsSchema ¶
type ParamsSchema struct {
Type string `json:"type"`
Properties map[string]*PropertySchema `json:"properties,omitempty"`
Required []string `json:"required,omitempty"`
Description string `json:"description,omitempty"`
}
ParamsSchema describes the parameters schema.
type PropertySchema ¶
type PropertySchema struct {
Type string `json:"type"`
Description string `json:"description,omitempty"`
Properties map[string]*PropertySchema `json:"properties,omitempty"`
Items *PropertySchema `json:"items,omitempty"`
Required []string `json:"required,omitempty"`
Example any `json:"example,omitempty"`
}
PropertySchema describes a property in a schema.
type Request ¶
type Request struct {
JSONRPC string `json:"jsonrpc"`
Method string `json:"method"`
Params json.RawMessage `json:"params,omitempty"`
ID any `json:"id,omitempty"` // string, number, or null
}
Request represents a JSON-RPC 2.0 request.
func (*Request) IsNotification ¶
IsNotification returns true if this is a notification (no ID).
type Response ¶
type Response struct {
JSONRPC string `json:"jsonrpc"`
Result any `json:"result,omitempty"`
Error *Error `json:"error,omitempty"`
ID any `json:"id"`
}
Response represents a JSON-RPC 2.0 response.
func NewErrorResponse ¶
NewErrorResponse creates an error response.
func NewErrorResponseWithData ¶
NewErrorResponseWithData creates an error response with additional data.
func NewSuccessResponse ¶
NewSuccessResponse creates a success response.
type ResponseSchemaDef ¶
ResponseSchemaDef mirrors the OpenAPI response schema definition This is imported from the router package's internal structure.
type ResultSchema ¶
type ResultSchema struct {
Type string `json:"type"`
Properties map[string]*PropertySchema `json:"properties,omitempty"`
Description string `json:"description,omitempty"`
}
ResultSchema describes the result schema.