Documentation
¶
Overview ¶
Package rpc provides a JSON-RPC style framework for building service-oriented APIs with PocketBase, featuring automatic service registration and method discovery.
The RPC framework uses reflection to automatically discover and register service methods, providing a type-safe way to build APIs with minimal boilerplate. It supports both POST requests for method calls and GET requests for entity retrieval.
Example usage:
import "github.com/sospartan/pb-toolkit/pkg/rpc"
// Create RPC server
rpcServer := rpc.NewServer()
// Register services
rpcServer.RegisterService("user", &UserService{})
// Bind to router
rpcServer.Bind(router.Group("/rpc"))
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type RPCMethod ¶
type RPCMethod struct {
Method reflect.Method // The reflection method information
Type reflect.Type // The parameter type for the method (nil if no parameters)
HasParams bool // Whether the method has parameters
HasResult bool // Whether the method returns a result value
ResultType reflect.Type // The result type (if HasResult is true)
}
RPCMethod represents a registered RPC method with its reflection information.
RPCMethod contains the method's reflection data and parameter type information, which is used by the RPC server to validate and execute method calls.
type RPCService ¶
type RPCService struct {
// contains filtered or unexported fields
}
RPCService represents an RPC service with registered methods.
RPCService contains a service instance and a map of its registered methods. Each method is validated to ensure it follows the required signature pattern.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server handles RPC requests and manages registered services.
Server provides methods for registering services, binding to routers, and handling incoming RPC requests. It uses reflection to automatically discover and validate service methods.
func NewServer ¶
func NewServer() *Server
NewServer creates a new RPC server instance.
Returns a new Server that can be used to register services and handle RPC requests.
Example:
server := rpc.NewServer()
func (*Server) Bind ¶
func (s *Server) Bind(g *router.RouterGroup[*core.RequestEvent])
Bind binds the RPC server to a router group, setting up the necessary routes.
This method creates two types of routes: - POST /{service}/{method} for method calls - GET /{service}/{entity}/{id} for entity retrieval
The router group should be configured with any necessary middleware (e.g., authentication, CORS, etc.).
Example:
g := se.Router.Group("/rpc")
g.Bind(apis.RequireAuth("users"))
server.Bind(g)
func (*Server) RegisterService ¶
RegisterService registers a service with the RPC server.
This method uses reflection to automatically discover all exported methods in the service that follow the required signature patterns:
- Method must be exported (start with uppercase)
- Method must have either: a) Exactly one parameter (the request struct) b) No parameters (for parameterless methods)
- Method must return either: a) Exactly two values: (result, error) - where the second return value must implement the error interface b) Exactly one value: error - where the return value must implement the error interface
The name parameter is used as the service identifier in URLs. The service parameter should be a pointer to a struct with methods. If service is nil, it will be registered but with no methods.
This implementation is based on the Ethereum go-ethereum approach.
Example:
type UserService struct{}
// Method with result and error
func (s *UserService) CreateUser(req CreateUserRequest) (CreateUserResponse, error) {
// Implementation
return response, nil
}
// Method with error only
func (s *UserService) DeleteUser(req DeleteUserRequest) error {
// Implementation
return nil // success
}
// Parameterless method with result and error
func (s *UserService) GetStats() (StatsResponse, error) {
// Implementation
return stats, nil
}
// Parameterless method with error only
func (s *UserService) RefreshCache() error {
// Implementation
return nil // success
}
err := server.RegisterService("user", &UserService{})