Skip to content

Repository files navigation

jsonrpc

CIcodecovGo Report Cardcodebeat badgeMaintainabilityGoDocGitHub license

About

Install

$ go get github.com/osamingo/jsonrpc/v2@latest

Usage

package main
import (
"context""log""net/http""github.com/goccy/go-json""github.com/osamingo/jsonrpc/v2"
)
type (
EchoHandlerstruct{}
EchoParamsstruct {
Namestring`json:"name"`
}
EchoResultstruct {
Messagestring`json:"message"`
}
PositionalHandlerstruct{}
PositionalParams []intPositionalResultstruct {
Message []int`json:"message"`
}
)
func (hEchoHandler) ServeJSONRPC(c context.Context, params*json.RawMessage) (any, *jsonrpc.Error) {
varpEchoParamsiferr:=jsonrpc.Unmarshal(params, &p); err!=nil {
returnnil, err
}
returnEchoResult{
Message: "Hello, "+p.Name,
}, nil
}
func (hPositionalHandler) ServeJSONRPC(c context.Context, params*json.RawMessage) (any, **jsonrpc.Error) {
varpPositionalParamsiferr:=jsonrpc.Unmarshal(params, &p); err!=nil {
returnnil, err
}
returnPositionalResult{
Message: p,
}, nil
}
funcmain() {
mr:=jsonrpc.NewMethodRepository()
iferr:=mr.RegisterMethod("Main.Echo", EchoHandler{}, EchoParams{}, EchoResult{}); err!=nil {
log.Fatalln(err)
}
iferr:=mr.RegisterMethod("Main.Positional", PositionalHandler{}, PositionalParams{}, PositionalResult{}); err!=nil {
log.Fatalln(err)
}
http.Handle("/jrpc", mr)
http.HandleFunc("/jrpc/debug", mr.ServeDebug)
iferr:=http.ListenAndServe(":8080", http.DefaultServeMux); err!=nil {
log.Fatalln(err)
}
}

Advanced

package main
import (
"log""net/http""github.com/osamingo/jsonrpc/v2"
)
type (
HandleParamsResulterinterface {
jsonrpc.HandlerName() stringParams() anyResult() any
}
Servicerinterface {
MethodName(HandleParamsResulter) stringHandlers() []HandleParamsResulter
}
UserServicestruct {
SignUpHandlerHandleParamsResulterSignInHandlerHandleParamsResulter
}
)
func (us*UserService) MethodName(hHandleParamsResulter) string {
return"UserService."+h.Name()
}
func (us*UserService) Handlers() []HandleParamsResulter {
return []HandleParamsResulter{us.SignUpHandler, us.SignInHandler}
}
funcNewUserService() *UserService {
return&UserService{
// Initialize handlers
}
}
funcmain() {
mr:=jsonrpc.NewMethodRepository()
for_, s:=range []Servicer{NewUserService()} {
for_, h:=ranges.Handlers() {
mr.RegisterMethod(s.MethodName(h), h, h.Params(), h.Result())
}
}
http.Handle("/jrpc", mr)
http.HandleFunc("/jrpc/debug", mr.ServeDebug)
iferr:=http.ListenAndServe(":8080", http.DefaultServeMux); err!=nil {
log.Fatalln(err)
}
}

Result

Invoke the Echo method

POST /jrpc HTTP/1.1
Accept: application/json, */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 82
Content-Type: application/json
Host: localhost:8080
User-Agent: HTTPie/0.9.6
{
"jsonrpc": "2.0",
"method": "Main.Echo",
"params": {
"name": "John Doe"
},
"id": "243a718a-2ebb-4e32-8cc8-210c39e8a14b"
}
HTTP/1.1 200 OK
Content-Length: 68
Content-Type: application/json
Date: Mon, 28 Nov 2016 13:48:13 GMT
{
"jsonrpc": "2.0",
"result": {
"message": "Hello, John Doe"
},
"id": "243a718a-2ebb-4e32-8cc8-210c39e8a14b"
}

Invoke the Positional method

POST /jrpc HTTP/1.1
Accept: */*
Content-Length: 133
Content-Type: application/json
Host: localhost:8080
User-Agent: curl/7.61.1
{
"jsonrpc": "2.0",
"method": "Main.Positional",
"params": [3,1,1,3,5,3],
"id": "243a718a-2ebb-4e32-8cc8-210c39e8a14b"
}
HTTP/1.1 200 OK
Content-Length: 97
Content-Type: application/json
Date: Mon, 05 Nov 2018 11:23:35 GMT
{
"jsonrpc": "2.0",
"result": {
"message": [3,1,1,3,5,3]
},
"id": "243a718a-2ebb-4e32-8cc8-210c39e8a14b"
}

Access to debug handler

GET /jrpc/debug HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: localhost:8080
User-Agent: HTTPie/0.9.6
HTTP/1.1 200 OK
Content-Length: 408
Content-Type: application/json
Date: Mon, 28 Nov 2016 13:56:24 GMT
[
{
"handler": "EchoHandler",
"name": "Main.Echo",
"params": {
"$ref": "#/definitions/EchoParams",
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"EchoParams": {
"additionalProperties": false,
"properties": {
"name": {
"type": "string"
}
},
"required": [
"name"
],
"type": "object"
}
}
},
"result": {
"$ref": "#/definitions/EchoResult",
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"EchoResult": {
"additionalProperties": false,
"properties": {
"message": {
"type": "string"
}
},
"required": [
"message"
],
"type": "object"
}
}
}
}
]

License

Released under the MIT License.

About

The jsonrpc package helps implement of JSON-RPC 2.0

Topics

Resources

Stars

193 stars

Watchers

5 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages