# README
JSON-RPC 2 Go supporting library
The library aims to bring JSON-RPC 2.0 support to Go. Goals:
- Type safe by code-generation
- Reasonable good performance
- Clean, extendable and easy-to use interface
- Protocol-agnostic solution with adapters for common-cases (HTTP, TCP, etc...)
Installation
- (recommended) look at releases page and download
- build from source
go get -v github.com/reddec/jsonrpc2/cmd/...
- From bintray repository for most debian-based distribution (
trusty
,xenial
,bionic
,buster
,wheezy
):
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 379CE192D401AB61
echo "deb https://dl.bintray.com/reddec/debian {distribution} main" | sudo tee -a /etc/apt/sources.list
sudo apt install jsonrpc2
Build requirements
- go 1.13+
Usage as library
Please see package documentation
Usage as CLI for type-safe generation
Usage:
jsonrpc2-gen [OPTIONS]
Generate tiny wrapper for JSON-RPC router
Author: Baryshnikov Aleksandr <[email protected]>
Version: dev
Application Options:
-i, --file= File to scan [$GOFILE]
-I, --interface= Interface to wrap [$INTERFACE]
--namespace= Custom namespace for functions. If not defined - interface name will be used [$NAMESPACE]
-w, --wrapper= Wrapper function name. If not defined - Register<interface> name will be used [$WRAPPER]
-o, --output= Generated output destination (- means STDOUT) (default: -) [$OUTPUT]
-p, --package= Package name (can be override by output dir) (default: events) [$PACKAGE]
-d, --doc= Generate markdown documentation [$DOC]
-c, --case=[keep|camel|pascal|snake|kebab] Method name case style (default: keep) [$CASE]
--url= URL for examples in documentation (default: https://example.com/api) [$URL]
-C, --interceptor add interceptor for each method [$INTERCEPTOR]
Help Options:
-h, --help Show this help message
Example
Assume you have an interface file (user.go
) like this:
package abc
// General user profile access
type User interface {
// Get user profile
Profile(token string) (*Profile, error)
}
Just invoke jsonrpc2-gen -i user.go -o user_gen.go -I User -p abc
You will get user_gen.go
file like that:
// Code generated by jsonrpc2-gen. DO NOT EDIT.
//go:generate jsonrpc2-gen -i user.go -o user_gen.go -I User -p abc
package abc
import (
"encoding/json"
jsonrpc2 "github.com/reddec/jsonrpc2"
)
func RegisterUser(router *jsonrpc2.Router, wrap User) []string {
router.RegisterFunc("User.Profile", func(params json.RawMessage, positional bool) (interface{}, error) {
var args struct {
Arg0 string `json:"token"`
}
var err error
if positional {
err = jsonrpc2.UnmarshalArray(params, &args.Arg0)
} else {
err = json.Unmarshal(params, &args)
}
if err != nil {
return nil, err
}
return wrap.Profile(args.Arg0)
})
return []string{"User.Profile"}
}
Generate documentation
Add -doc user.md
to generate documentations as described bellow. It will be generated and saved to the provided file (user.md
)
# User
General user profile access
## User.Profile
Get user profile
* Method: `User.Profile`
* Returns: `*Profile`
* Arguments:
| Position | Name | Type |
|----------|------|------|
| 0 | token | `string` |
KTOR (kotlin) generator
- Supports
time/Time
- (TBD)
time/Duration
Gradle requirements
def ktor_version = '1.3.1'
repositories {
mavenCentral()
}
dependencies {
implementation("io.ktor:ktor-client-cio:$ktor_version") // <-- you can choose another
implementation("io.ktor:ktor-client-gson:$ktor_version")
}
# Functions
Wrap function as JSON-RPC method for usage in router
This kind of wrapper support only positional arguments.
Expose JSON-RPC route over HTTP Rest (POST) and web sockets (GET).
No description provided by the author
Expose JSON-RPC router as HTTP handler where one requests is one execution.
No description provided by the author
Process requests over web socket (all requests are processing in parallel in a separate go-routine).
No description provided by the author
Interceptor that limiting maximum number of requests in a batch.
Expose function handler where first argument is pointer to structure and returns are payload with error.
No description provided by the author
No description provided by the author
# 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
No description provided by the author
No description provided by the author
No description provided by the author
# Structs
JSON-RPC 2.0 standard error object.
No description provided by the author
Context handling request per method.
Standard JSON-RPC 2.0 request messages.
JSON-RPC 2.0 standard response object.
Router for JSON-RPC requests.
# Interfaces
Method handler (for low-level implementation).
# Type aliases
No description provided by the author
No description provided by the author
Interceptor for each method that will be called.