Documentation
¶
Index ¶
- Constants
- func HTTPHandler(server *Server) http.Handler
- func HTTPRequestFromContext(ctx context.Context) *http.Request
- func RegisterMethod[P, R any](r Registerer, name string, fn func(context.Context, P) (R, error), ...)
- func SpecJSONHandler(server *Server) http.Handler
- func SpecUIHandler(server *Server) http.Handler
- func WithHTTPRequest(ctx context.Context, r *http.Request) context.Context
- type FieldInfo
- type FuncType
- type Group
- type HandlerFunc
- type MethodInfo
- type Middleware
- type MiddlewareChain
- type RPCError
- type RPCErrorProvider
- type RPCRequest
- type RPCResponse
- type Registerer
- type Server
- type ServerSpec
- type TypeInfo
- type UnmarshalKind
- type ValidateErrorHandler
Constants ¶
const ( CodeParseError = -32700 CodeInvalidRequest = -32600 CodeMethodNotFound = -32601 CodeInvalidParams = -32602 CodeInternalError = -32603 )
Variables ¶
This section is empty.
Functions ¶
func HTTPHandler ¶
func HTTPRequestFromContext ¶ added in v1.0.1
func RegisterMethod ¶
func RegisterMethod[P, R any]( r Registerer, name string, fn func(context.Context, P) (R, error), middlewares ...Middleware, )
RegisterMethod registers a method with the given name and function. The first parameter can be either *Server or *Group. The function must have the signature: func(context.Context, ParamsType) (ResultType, error)
Example with Server:
RegisterMethod(server, "add", func(ctx context.Context, params []int) (int, error) {
if len(params) != 2 {
return 0, errors.New("expected 2 numbers")
}
return params[0] + params[1], nil
})
Example with Group:
mathGroup := server.Group("math.")
RegisterMethod(mathGroup, "add", AddFunc)
With middleware:
RegisterMethod(server, "add", AddFunc, AuthMiddleware(), LoggingMiddleware())
func SpecJSONHandler ¶
func SpecUIHandler ¶ added in v1.0.2
Types ¶
type FieldInfo ¶
type FieldInfo struct {
Name string `json:"name"`
JSONName string `json:"jsonName,omitempty"`
Type string `json:"type"`
Kind string `json:"kind"`
Required bool `json:"required,omitempty"`
ValidationRules []string `json:"validationRules,omitempty"`
IsArray bool `json:"isArray,omitempty"`
ArrayDepth int `json:"arrayDepth,omitempty"` // 0 = not an array, 1 = []T, 2 = [][]T, etc.
IsPointer bool `json:"isPointer,omitempty"`
PointerDepth int `json:"pointerDepth,omitempty"` // 0 = not a pointer, 1 = *T, 2 = **T, etc.
ElementType string `json:"elementType,omitempty"` // string representation of element type (hint)
KeyType string `json:"keyType,omitempty"` // key type for map types
ValueType string `json:"valueType,omitempty"` // value type for map types
Fields []FieldInfo `json:"fields,omitempty"` // nested fields if this is a struct type
}
type Group ¶ added in v1.0.1
type Group struct {
// contains filtered or unexported fields
}
func (*Group) Use ¶ added in v1.0.1
func (g *Group) Use(middlewares ...Middleware)
Use adds middleware to the group. Middleware added to a group will apply to all methods registered in that group.
type HandlerFunc ¶ added in v1.0.1
type HandlerFunc func(ctx context.Context, req RPCRequest) (RPCResponse, error)
type MethodInfo ¶
type Middleware ¶ added in v1.0.1
type Middleware func(ctx context.Context, req RPCRequest, next HandlerFunc) (RPCResponse, error)
type MiddlewareChain ¶ added in v1.0.1
type MiddlewareChain struct {
// contains filtered or unexported fields
}
func NewMiddlewareChain ¶ added in v1.0.1
func NewMiddlewareChain(middlewares ...Middleware) *MiddlewareChain
func (*MiddlewareChain) Add ¶ added in v1.0.1
func (mc *MiddlewareChain) Add(middleware Middleware)
func (*MiddlewareChain) Build ¶ added in v1.0.1
func (mc *MiddlewareChain) Build(finalHandler HandlerFunc) HandlerFunc
func (*MiddlewareChain) Len ¶ added in v1.0.1
func (mc *MiddlewareChain) Len() int
type RPCErrorProvider ¶
type RPCRequest ¶
type RPCRequest struct {
JSONRPC string `json:"jsonrpc"`
Method string `json:"method"`
Params json.RawMessage `json:"params"`
ID json.RawMessage `json:"id"`
}
type RPCResponse ¶
type RPCResponse struct {
JSONRPC string `json:"jsonrpc"`
Result interface{} `json:"result,omitempty"`
Error *RPCError `json:"error,omitempty"`
ID json.RawMessage `json:"id"`
}
type Registerer ¶ added in v1.0.1
type Registerer interface {
// contains filtered or unexported methods
}
Both Server and Group implement this interface.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
func (*Server) GetMethodSpecs ¶
func (s *Server) GetMethodSpecs() ServerSpec
GetMethodSpecs returns information about all registered RPC methods. This can be used to generate API specifications
Example usage:
spec := server.GetMethodSpecs()
for _, method := range spec.Methods {
fmt.Printf("Method: %s\n", method.Name)
fmt.Printf("Params: %s\n", method.Params)
fmt.Printf("Result: %s\n", method.Result)
}
for typeName, typeInfo := range spec.Types {
fmt.Printf("Type: %s\n", typeName)
fmt.Printf("Fields: %+v\n", typeInfo.Fields)
}
func (*Server) Group ¶ added in v1.0.1
func (s *Server) Group(prefix string, middlewares ...Middleware) *Group
Group creates a new group with the given prefix. The prefix will be prepended to all method names registered in this group. An empty prefix is allowed. Middleware can be provided as variadic parameters and will apply to all methods in the group.
Example:
mathGroup := server.Group("math.", TimingMiddleware, AuthMiddleware())
func (*Server) SetValidateErrorHandler ¶
func (s *Server) SetValidateErrorHandler(handler ValidateErrorHandler)
func (*Server) Use ¶ added in v1.0.1
func (s *Server) Use(middlewares ...Middleware)
type ServerSpec ¶ added in v1.0.2
type ServerSpec struct {
Methods []MethodInfo `json:"methods"`
Types map[string]TypeInfo `json:"types"`
}
type TypeInfo ¶
type TypeInfo struct {
Name string `json:"name"` // struct name only (without package)
Package string `json:"package,omitempty"` // package path
Kind string `json:"kind"`
IsArray bool `json:"isArray,omitempty"`
ArrayDepth int `json:"arrayDepth,omitempty"` // 0 = not an array, 1 = []T, 2 = [][]T, etc.
IsPointer bool `json:"isPointer,omitempty"`
PointerDepth int `json:"pointerDepth,omitempty"` // 0 = not a pointer, 1 = *T, 2 = **T, etc.
ElementType string `json:"elementType,omitempty"` // string representation of element type (hint)
KeyType string `json:"keyType,omitempty"` // key type for map types
ValueType string `json:"valueType,omitempty"` // value type for map types
Fields []FieldInfo `json:"fields,omitempty"` // nested fields if this is a struct type
}
type UnmarshalKind ¶ added in v1.0.2
type UnmarshalKind interface {
UnmarshalKind() string
}
UnmarshalKind is an interface that types can implement to specify their JSON unmarshaling kind. This is useful for types like types.Time or types.Duration that unmarshal from strings or numbers but are represented as structs in Go. If a type implements this interface, the returned kind will be used in the API specification instead of "struct".
type ValidateErrorHandler ¶
type ValidateErrorHandler func(*validator.ValidationErrors) *RPCError