# README
zenrpc: JSON-RPC 2.0 Server Implementation with SMD support
zenrpc
is a JSON-RPC 2.0 server library with Service Mapping Description support.
It's built on top of go generate
instead of reflection.
How to Use
Service is struct with RPC methods, service represents RPC namespace.
- Install zenrpc generator
go get github.com/lcd1232/zenrpc/v3/zenrpc
- Import
github.com/lcd1232/zenrpc/v3
into our code with rpc service. - Add trailing comment
//zenrpc
to your service or embedzenrpc.Service
into your service struct. - Write your funcs almost as usual.
- Do not forget run
go generate
orzenrpc
for magic
Accepted Method Signatures
func(Service) Method([args]) (<value>, <error>)
func(Service) Method([args]) <value>
func(Service) Method([args]) <error>
func(Service) Method([args])
- Value could be a pointer
- Error is error or *zenrpc.Error
Example
package main
import (
"flag"
"errors"
"math"
"log"
"net/http"
"os"
"github.com/lcd1232/zenrpc/v3"
"github.com/lcd1232/zenrpc/v3/testdata"
)
type ArithService struct{ zenrpc.Service }
// Sum sums two digits and returns error with error code as result and IP from context.
func (as ArithService) Sum(c zenrpc.Context, a, b int) (bool, *zenrpc.Error) {
r := c.Request()
return true, zenrpc.NewStringError(a+b, r.Host)
}
// Multiply multiples two digits and returns result.
func (as ArithService) Multiply(a, b int) int {
return a * b
}
type Quotient struct {
Quo, Rem int
}
func (as ArithService) Divide(a, b int) (quo *Quotient, err error) {
if b == 0 {
return nil, errors.New("divide by zero")
} else if b == 1 {
return nil, zenrpc.NewError(401, errors.New("we do not serve 1"))
}
return &Quotient{
Quo: a / b,
Rem: a % b,
}, nil
}
// Pow returns x**y, the base-x exponential of y. If Exp is not set then default value is 2.
//zenrpc:exp=2
func (as ArithService) Pow(base float64, exp float64) float64 {
return math.Pow(base, exp)
}
//go:generate zenrpc
func main() {
addr := flag.String("addr", "localhost:9999", "listen address")
flag.Parse()
rpc := zenrpc.NewServer(zenrpc.Options{ExposeSMD: true})
rpc.Register("arith", testdata.ArithService{})
rpc.Register("", testdata.ArithService{}) // public
rpc.Use(zenrpc.Logger(log.New(os.Stderr, "", log.LstdFlags)))
http.Handle("/", rpc)
log.Printf("starting arithsrv on %s", *addr)
log.Fatal(http.ListenAndServe(*addr, nil))
}
Magic comments
All comments are optional.
Method comments
//zenrpc:<method parameter>[=<default value>][whitespaces<description>]
//zenrpc:<error code>[whitespaces<description>]
//zenrpc:return[whitespaces<description>]
Struct comments
type MyService struct {} //zenrpc
Need to browse your api and do some test api calls?
We recommend to use SMDBox. It is Swagger-like JSON RPC API browser, compatible with smd scheme, generated by zenrpc.
JSON-RPC 2.0 Supported Features
- Requests
- Single requests
- Batch requests
- Notifications
- Parameters
- Named
- Position
- Default values
- SMD Schema
- Input
- Output
- Codes
- Scopes for OAuth
Server Library Features
- go generate
- Transports
- HTTP
- WebSocket
- RabbitMQ
- Server middleware
- Basic support
- Metrics
- Logging
# Functions
ConvertToObject converts json array into object using key by index from keys array.
ErrorMsg returns error as text for default JSON-RPC errors.
IsArray checks json message if it array or object.
Logger is middleware for JSON-RPC 2.0 Server.
Metrics is a middleware for logging duration of RPC requests via Prometheus.
No description provided by the author
NewError makes a JSON-RPC error with given code and standard error.
No description provided by the author
NewResponseError returns new Response with Error object.
NewServer returns new JSON-RPC 2.0 Server.
NewStringError makes a JSON-RPC with given code and message.
SMDBoxHandler is a handler for SMDBox web app.
# Constants
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
InternalError is error code defined by JSON-RPC 2.0 spec.
InvalidParams is error code defined by JSON-RPC 2.0 spec.
InvalidRequest is error code defined by JSON-RPC 2.0 spec.
MethodNotFound is error code defined by JSON-RPC 2.0 spec.
ParseError is error code defined by JSON-RPC 2.0 spec.
ServerError is error code defined by JSON-RPC 2.0 spec.
Version is only supported JSON-RPC Version.
# Structs
Error object used in response if function call errored.
Options is options for JSON-RPC 2.0 Server.
Request is a json structure for json-rpc request to server.
Response is json structure for json-rpc response from server.
Server is JSON-RPC 2.0 Server.
Service is as struct for discovering JSON-RPC 2.0 services for zenrpc generator cmd.
# Type aliases
No description provided by the author
InvokeFunc is a function for processing single JSON-RPC 2.0 Request after validation and parsing.
MiddlewareFunc is a function for executing as middleware.