{"id":13412598,"url":"https://github.com/osamingo/jsonrpc","last_synced_at":"2025-04-09T17:22:34.140Z","repository":{"id":49161443,"uuid":"72210430","full_name":"osamingo/jsonrpc","owner":"osamingo","description":"The jsonrpc package helps implement of JSON-RPC 2.0","archived":false,"fork":false,"pushed_at":"2023-05-01T16:03:25.000Z","size":102,"stargazers_count":188,"open_issues_count":6,"forks_count":23,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-10-25T04:10:09.632Z","etag":null,"topics":["api","go","json","json-rpc","rpc","rpc-api"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/osamingo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null},"funding":{"github":"osamingo","custom":"https://paypal.me/osamingo"}},"created_at":"2016-10-28T13:36:59.000Z","updated_at":"2024-09-29T01:45:15.000Z","dependencies_parsed_at":"2024-01-30T04:54:38.966Z","dependency_job_id":null,"html_url":"https://github.com/osamingo/jsonrpc","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osamingo%2Fjsonrpc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osamingo%2Fjsonrpc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osamingo%2Fjsonrpc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osamingo%2Fjsonrpc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/osamingo","download_url":"https://codeload.github.com/osamingo/jsonrpc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248075260,"owners_count":21043555,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","go","json","json-rpc","rpc","rpc-api"],"created_at":"2024-07-30T20:01:26.618Z","updated_at":"2025-04-09T17:22:34.117Z","avatar_url":"https://github.com/osamingo.png","language":"Go","readme":"# jsonrpc\n\n[![CI](https://github.com/osamingo/jsonrpc/actions/workflows/actions.yml/badge.svg)](https://github.com/osamingo/jsonrpc/actions/workflows/actions.yml)\n[![codecov](https://codecov.io/gh/osamingo/jsonrpc/branch/master/graph/badge.svg)](https://codecov.io/gh/osamingo/jsonrpc)\n[![Go Report Card](https://goreportcard.com/badge/osamingo/jsonrpc)](https://goreportcard.com/report/osamingo/jsonrpc)\n[![codebeat badge](https://codebeat.co/badges/cbd0290d-200b-4693-80dc-296d9447c35b)](https://codebeat.co/projects/github-com-osamingo-jsonrpc)\n[![Maintainability](https://api.codeclimate.com/v1/badges/e820b394cdbd47103165/maintainability)](https://codeclimate.com/github/osamingo/jsonrpc/maintainability)\n[![GoDoc](https://godoc.org/github.com/osamingo/jsonrpc?status.svg)](https://godoc.org/github.com/osamingo/jsonrpc/v2)\n[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/osamingo/jsonrpc/master/LICENSE)\n\n## About\n\n- Simple, Poetic, Pithy.\n- Compliance with [JSON-RPC 2.0](http://www.jsonrpc.org/specification).\n\n## Install\n\n```\n$ go get github.com/osamingo/jsonrpc/v2@latest\n```\n\n## Usage\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"log\"\n\t\"net/http\"\n\n\t\"github.com/goccy/go-json\"\n\t\"github.com/osamingo/jsonrpc/v2\"\n)\n\ntype (\n\tEchoHandler struct{}\n\tEchoParams  struct {\n\t\tName string `json:\"name\"`\n\t}\n\tEchoResult struct {\n\t\tMessage string `json:\"message\"`\n\t}\n\n\tPositionalHandler struct{}\n\tPositionalParams  []int\n\tPositionalResult  struct {\n\t\tMessage []int `json:\"message\"`\n\t}\n)\n\nfunc (h EchoHandler) ServeJSONRPC(c context.Context, params *json.RawMessage) (any, *jsonrpc.Error) {\n\n\tvar p EchoParams\n\tif err := jsonrpc.Unmarshal(params, \u0026p); err != nil {\n\t\treturn nil, err\n\t}\n\n\treturn EchoResult{\n\t\tMessage: \"Hello, \" + p.Name,\n\t}, nil\n}\n\nfunc (h PositionalHandler) ServeJSONRPC(c context.Context, params *json.RawMessage) (any, **jsonrpc.Error) {\n\n\tvar p PositionalParams\n\tif err := jsonrpc.Unmarshal(params, \u0026p); err != nil {\n\t\treturn nil, err\n\t}\n\n\treturn PositionalResult{\n\t\tMessage: p,\n\t}, nil\n}\n\nfunc main() {\n\n\tmr := jsonrpc.NewMethodRepository()\n\n\tif err := mr.RegisterMethod(\"Main.Echo\", EchoHandler{}, EchoParams{}, EchoResult{}); err != nil {\n\t\tlog.Fatalln(err)\n\t}\n\n\tif err := mr.RegisterMethod(\"Main.Positional\", PositionalHandler{}, PositionalParams{}, PositionalResult{}); err != nil {\n\t\tlog.Fatalln(err)\n\t}\n\n\thttp.Handle(\"/jrpc\", mr)\n\thttp.HandleFunc(\"/jrpc/debug\", mr.ServeDebug)\n\n\tif err := http.ListenAndServe(\":8080\", http.DefaultServeMux); err != nil {\n\t\tlog.Fatalln(err)\n\t}\n}\n```\n\n#### Advanced\n\n```go\npackage main\n\nimport (\n\t\"log\"\n\t\"net/http\"\n\n\t\"github.com/osamingo/jsonrpc/v2\"\n)\n\ntype (\n\tHandleParamsResulter interface {\n\t\tjsonrpc.Handler\n\t\tName() string\n\t\tParams() any\n\t\tResult() any\n\t}\n\tServicer interface {\n\t\tMethodName(HandleParamsResulter) string\n\t\tHandlers() []HandleParamsResulter\n\t}\n\tUserService struct {\n\t\tSignUpHandler HandleParamsResulter\n\t\tSignInHandler HandleParamsResulter\n\t}\n)\n\nfunc (us *UserService) MethodName(h HandleParamsResulter) string {\n\treturn \"UserService.\" + h.Name()\n}\n\nfunc (us *UserService) Handlers() []HandleParamsResulter {\n\treturn []HandleParamsResulter{us.SignUpHandler, us.SignInHandler}\n}\n\nfunc NewUserService() *UserService {\n\treturn \u0026UserService{\n\t// Initialize handlers\n\t}\n}\n\nfunc main() {\n\n\tmr := jsonrpc.NewMethodRepository()\n\n\tfor _, s := range []Servicer{NewUserService()} {\n\t\tfor _, h := range s.Handlers() {\n\t\t\tmr.RegisterMethod(s.MethodName(h), h, h.Params(), h.Result())\n\t\t}\n\t}\n\n\thttp.Handle(\"/jrpc\", mr)\n\thttp.HandleFunc(\"/jrpc/debug\", mr.ServeDebug)\n\n\tif err := http.ListenAndServe(\":8080\", http.DefaultServeMux); err != nil {\n\t\tlog.Fatalln(err)\n\t}\n}\n```\n\n### Result\n\n#### Invoke the Echo method\n\n```\nPOST /jrpc HTTP/1.1\nAccept: application/json, */*\nAccept-Encoding: gzip, deflate\nConnection: keep-alive\nContent-Length: 82\nContent-Type: application/json\nHost: localhost:8080\nUser-Agent: HTTPie/0.9.6\n\n{\n  \"jsonrpc\": \"2.0\",\n  \"method\": \"Main.Echo\",\n  \"params\": {\n    \"name\": \"John Doe\"\n  },\n  \"id\": \"243a718a-2ebb-4e32-8cc8-210c39e8a14b\"\n}\n\nHTTP/1.1 200 OK\nContent-Length: 68\nContent-Type: application/json\nDate: Mon, 28 Nov 2016 13:48:13 GMT\n\n{\n  \"jsonrpc\": \"2.0\",\n  \"result\": {\n    \"message\": \"Hello, John Doe\"\n  },\n  \"id\": \"243a718a-2ebb-4e32-8cc8-210c39e8a14b\"\n}\n```\n\n#### Invoke the Positional method\n\n```\nPOST /jrpc HTTP/1.1\nAccept: */*\nContent-Length: 133\nContent-Type: application/json\nHost: localhost:8080\nUser-Agent: curl/7.61.1\n\n{\n  \"jsonrpc\": \"2.0\",\n  \"method\": \"Main.Positional\",\n  \"params\": [3,1,1,3,5,3],\n  \"id\": \"243a718a-2ebb-4e32-8cc8-210c39e8a14b\"\n}\n\nHTTP/1.1 200 OK\nContent-Length: 97\nContent-Type: application/json\nDate: Mon, 05 Nov 2018 11:23:35 GMT\n\n{\n  \"jsonrpc\": \"2.0\",\n  \"result\": {\n    \"message\": [3,1,1,3,5,3]\n  },\n  \"id\": \"243a718a-2ebb-4e32-8cc8-210c39e8a14b\"\n}\n\n```\n\n\n#### Access to debug handler\n\n```\nGET /jrpc/debug HTTP/1.1\nAccept: */*\nAccept-Encoding: gzip, deflate\nConnection: keep-alive\nHost: localhost:8080\nUser-Agent: HTTPie/0.9.6\n\nHTTP/1.1 200 OK\nContent-Length: 408\nContent-Type: application/json\nDate: Mon, 28 Nov 2016 13:56:24 GMT\n\n[\n  {\n    \"handler\": \"EchoHandler\",\n    \"name\": \"Main.Echo\",\n    \"params\": {\n      \"$ref\": \"#/definitions/EchoParams\",\n      \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n      \"definitions\": {\n        \"EchoParams\": {\n          \"additionalProperties\": false,\n          \"properties\": {\n            \"name\": {\n              \"type\": \"string\"\n            }\n          },\n          \"required\": [\n            \"name\"\n          ],\n          \"type\": \"object\"\n        }\n      }\n    },\n    \"result\": {\n      \"$ref\": \"#/definitions/EchoResult\",\n      \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n      \"definitions\": {\n        \"EchoResult\": {\n          \"additionalProperties\": false,\n          \"properties\": {\n            \"message\": {\n              \"type\": \"string\"\n            }\n          },\n          \"required\": [\n            \"message\"\n          ],\n          \"type\": \"object\"\n        }\n      }\n    }\n  }\n]\n```\n\n## License\n\nReleased under the [MIT License](https://github.com/osamingo/jsonrpc/blob/master/LICENSE).\n","funding_links":["https://github.com/sponsors/osamingo","https://paypal.me/osamingo"],"categories":["Distributed Systems","Relational Databases","分布式系统","Uncategorized","Go","Repositories","\u003cspan id=\"分布式系统-distributed-systems\"\u003e分布式系统 Distributed Systems\u003c/span\u003e","Libraries"],"sub_categories":["Search and Analytic Databases","检索及分析资料库","Advanced Console UIs","Uncategorized","SQL 查询语句构建库","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fosamingo%2Fjsonrpc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fosamingo%2Fjsonrpc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fosamingo%2Fjsonrpc/lists"}