Product Promotion
0x5a.live
for different kinds of informations and explorations.
GitHub - osamingo/jsonrpc: The jsonrpc package helps implement of JSON-RPC 2.0
The jsonrpc package helps implement of JSON-RPC 2.0 - osamingo/jsonrpc
Visit SiteGitHub - osamingo/jsonrpc: The jsonrpc package helps implement of JSON-RPC 2.0
The jsonrpc package helps implement of JSON-RPC 2.0 - osamingo/jsonrpc
Powered by 0x5a.live ๐
jsonrpc
About
- Simple, Poetic, Pithy.
- Compliance with JSON-RPC 2.0.
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 (
EchoHandler struct{}
EchoParams struct {
Name string `json:"name"`
}
EchoResult struct {
Message string `json:"message"`
}
PositionalHandler struct{}
PositionalParams []int
PositionalResult struct {
Message []int `json:"message"`
}
)
func (h EchoHandler) ServeJSONRPC(c context.Context, params *json.RawMessage) (any, *jsonrpc.Error) {
var p EchoParams
if err := jsonrpc.Unmarshal(params, &p); err != nil {
return nil, err
}
return EchoResult{
Message: "Hello, " + p.Name,
}, nil
}
func (h PositionalHandler) ServeJSONRPC(c context.Context, params *json.RawMessage) (any, **jsonrpc.Error) {
var p PositionalParams
if err := jsonrpc.Unmarshal(params, &p); err != nil {
return nil, err
}
return PositionalResult{
Message: p,
}, nil
}
func main() {
mr := jsonrpc.NewMethodRepository()
if err := mr.RegisterMethod("Main.Echo", EchoHandler{}, EchoParams{}, EchoResult{}); err != nil {
log.Fatalln(err)
}
if err := mr.RegisterMethod("Main.Positional", PositionalHandler{}, PositionalParams{}, PositionalResult{}); err != nil {
log.Fatalln(err)
}
http.Handle("/jrpc", mr)
http.HandleFunc("/jrpc/debug", mr.ServeDebug)
if err := http.ListenAndServe(":8080", http.DefaultServeMux); err != nil {
log.Fatalln(err)
}
}
Advanced
package main
import (
"log"
"net/http"
"github.com/osamingo/jsonrpc/v2"
)
type (
HandleParamsResulter interface {
jsonrpc.Handler
Name() string
Params() any
Result() any
}
Servicer interface {
MethodName(HandleParamsResulter) string
Handlers() []HandleParamsResulter
}
UserService struct {
SignUpHandler HandleParamsResulter
SignInHandler HandleParamsResulter
}
)
func (us *UserService) MethodName(h HandleParamsResulter) string {
return "UserService." + h.Name()
}
func (us *UserService) Handlers() []HandleParamsResulter {
return []HandleParamsResulter{us.SignUpHandler, us.SignInHandler}
}
func NewUserService() *UserService {
return &UserService{
// Initialize handlers
}
}
func main() {
mr := jsonrpc.NewMethodRepository()
for _, s := range []Servicer{NewUserService()} {
for _, h := range s.Handlers() {
mr.RegisterMethod(s.MethodName(h), h, h.Params(), h.Result())
}
}
http.Handle("/jrpc", mr)
http.HandleFunc("/jrpc/debug", mr.ServeDebug)
if err := 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.
GoLang Resources
are all listed below.
GitHub - GuilhermeCaruso/anko: :crystal_ball: Simple application watcher
resource
~/github.com
resource
GitHub - jidicula/go-fuzz-action: GitHub Action for Go 1.18 fuzz testing
resource
~/github.com
resource
GitHub - tucnak/climax: Climax is an alternative CLI with the human face
resource
~/github.com
resource
GitHub - lawrencewoodman/roveralls: A Go recursive coverage testing tool
resource
~/github.com
resource
GitHub - nakagami/firebirdsql: Firebird RDBMS sql driver for Go (golang)
resource
~/github.com
resource
GitHub - liweiyi88/onedump: Effortlessly database dump with one command.
resource
~/github.com
resource
GitHub - beefsack/go-astar: Go implementation of the A* search algorithm
resource
~/github.com
resource
GitHub - lxn/walk: A Windows GUI toolkit for the Go Programming Language
resource
~/github.com
resource
GitHub - mongodb/mongo-go-driver: The Official Golang driver for MongoDB
resource
~/github.com
resource
GitHub - bykof/gostradamus: Gostradamus: Better DateTimes for Go ๐ฐ๏ธ
resource
~/github.com
resource
GitHub - mozillazg/go-unidecode: ASCII transliterations of Unicode text.
resource
~/github.com
resource
GitHub - bolknote/go-gd: Go bingings for GD (http://www.boutell.com/gd/)
resource
~/github.com
resource
GitHub - mosajjal/dnsmonster: Passive DNS Capture and Monitoring Toolkit
resource
~/github.com
resource
GitHub - haxpax/gosms: :mailbox_closed: Your own local SMS gateway in Go
resource
~/github.com
resource
GitHub - wajox/gobase: This is a simple skeleton for golang applications
resource
~/github.com
resource
GitHub - VividCortex/gohistogram: Streaming approximate histograms in Go
resource
~/github.com
resource
GitHub - malaschitz/randomForest: Random Forest implementation in golang
resource
~/github.com
resource
GitHub - google/gopacket: Provides packet processing capabilities for Go
resource
~/github.com
resource
GitHub - khezen/evoli: Genetic Algorithm and Particle Swarm Optimization
resource
~/github.com
resource
GitHub - didip/tollbooth: Simple middleware to rate-limit HTTP requests.
resource
~/github.com
resource
GitHub - mustafaakin/gongular: A different approach to Go web frameworks
resource
~/github.com
resource
GitHub - songgao/colorgo: Colorize (highlight) `go build` command output
resource
~/github.com
resource
Made with โค๏ธ
to provide different kinds of informations and resources.