{"id":26085219,"url":"https://github.com/semrush/zenrpc","last_synced_at":"2025-04-07T13:04:18.399Z","repository":{"id":53480608,"uuid":"96132408","full_name":"semrush/zenrpc","owner":"semrush","description":"JSON-RPC 2.0 Server Implementation with SMD support written in Go (go generate)","archived":false,"fork":false,"pushed_at":"2024-06-15T19:51:08.000Z","size":94,"stargazers_count":172,"open_issues_count":14,"forks_count":38,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-03-31T12:03:34.192Z","etag":null,"topics":["generate","golang","json-rpc2","jsonrpc","jsonrpc2","smd-support","websocket"],"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/semrush.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"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,"publiccode":null,"codemeta":null}},"created_at":"2017-07-03T16:52:46.000Z","updated_at":"2025-01-27T19:24:56.000Z","dependencies_parsed_at":"2024-06-18T15:17:06.113Z","dependency_job_id":"721a71f7-ba1e-44b5-bd1a-48321cfcc6ff","html_url":"https://github.com/semrush/zenrpc","commit_stats":null,"previous_names":["sergeyfast/zenrpc"],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/semrush%2Fzenrpc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/semrush%2Fzenrpc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/semrush%2Fzenrpc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/semrush%2Fzenrpc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/semrush","download_url":"https://codeload.github.com/semrush/zenrpc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247657275,"owners_count":20974344,"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":["generate","golang","json-rpc2","jsonrpc","jsonrpc2","smd-support","websocket"],"created_at":"2025-03-09T05:55:00.562Z","updated_at":"2025-04-07T13:04:18.373Z","avatar_url":"https://github.com/semrush.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# zenrpc: JSON-RPC 2.0 Server Implementation with SMD support\n\n[![Go Report Card](https://goreportcard.com/badge/github.com/semrush/zenrpc)](https://goreportcard.com/report/github.com/semrush/zenrpc) [![Build Status](https://travis-ci.org/semrush/zenrpc.svg?branch=master)](https://travis-ci.org/semrush/zenrpc) [![codecov](https://codecov.io/gh/semrush/zenrpc/branch/master/graph/badge.svg)](https://codecov.io/gh/semrush/zenrpc) [![GoDoc](https://godoc.org/github.com/semrush/zenrpc?status.svg)](https://godoc.org/github.com/semrush/zenrpc)\n\n`zenrpc` is a JSON-RPC 2.0 server library with [Service Mapping Description](https://dojotoolkit.org/reference-guide/1.8/dojox/rpc/smd.html) support. \nIt's built on top of `go generate` instead of reflection. \n\n# How to Use\n\n```Service is struct with RPC methods, service represents RPC namespace.```\n\n  1. Install zenrpc generator `go get github.com/semrush/zenrpc/v2/zenrpc`\n  1. Import `github.com/semrush/zenrpc/v2` into our code with rpc service.\n  1. Add trailing comment `//zenrpc` to your service or embed `zenrpc.Service` into your service struct.\n  1. Write your funcs almost as usual.\n  1. Do not forget run `go generate` or `zenrpc` for magic\n\n### Accepted Method Signatures\n\n    func(Service) Method([args]) (\u003cvalue\u003e, \u003cerror\u003e)\n    func(Service) Method([args]) \u003cvalue\u003e\n    func(Service) Method([args]) \u003cerror\u003e\n    func(Service) Method([args])\n\n- Value could be a pointer\n- Error is error or *zenrpc.Error\n\n## Example\n```go\npackage main\n\nimport (\n\t\"flag\"\n\t\"context\"\n\t\"errors\"\n\t\"math\"\n\t\"log\"\n\t\"net/http\"\n\t\"os\"\t\n\t\n\t\"github.com/semrush/zenrpc/v2\"\n\t\"github.com/semrush/zenrpc/v2/testdata\"\n)\n\ntype ArithService struct{ zenrpc.Service }\n\n// Sum sums two digits and returns error with error code as result and IP from context.\nfunc (as ArithService) Sum(ctx context.Context, a, b int) (bool, *zenrpc.Error) {\n\tr, _ := zenrpc.RequestFromContext(ctx)\n\n\treturn true, zenrpc.NewStringError(a+b, r.Host)\n}\n\n// Multiply multiples two digits and returns result.\nfunc (as ArithService) Multiply(a, b int) int {\n\treturn a * b\n}\n\ntype Quotient struct {\n\tQuo, Rem int\n}\n\nfunc (as ArithService) Divide(a, b int) (quo *Quotient, err error) {\n\tif b == 0 {\n\t\treturn nil, errors.New(\"divide by zero\")\n\t} else if b == 1 {\n\t\treturn nil, zenrpc.NewError(401, errors.New(\"we do not serve 1\"))\n\t}\n\n\treturn \u0026Quotient{\n\t\tQuo: a / b,\n\t\tRem: a % b,\n\t}, nil\n}\n\n// Pow returns x**y, the base-x exponential of y. If Exp is not set then default value is 2.\n//zenrpc:exp=2\nfunc (as ArithService) Pow(base float64, exp float64) float64 {\n\treturn math.Pow(base, exp)\n}\n\n//go:generate zenrpc\n\nfunc main() {\n\taddr := flag.String(\"addr\", \"localhost:9999\", \"listen address\")\n\tflag.Parse()\n\n\trpc := zenrpc.NewServer(zenrpc.Options{ExposeSMD: true})\n\trpc.Register(\"arith\", testdata.ArithService{})\n\trpc.Register(\"\", testdata.ArithService{}) // public\n\trpc.Use(zenrpc.Logger(log.New(os.Stderr, \"\", log.LstdFlags)))\n\n\thttp.Handle(\"/\", rpc)\n\n\tlog.Printf(\"starting arithsrv on %s\", *addr)\n\tlog.Fatal(http.ListenAndServe(*addr, nil))\n}\n\n```\n\n\n## Magic comments\n\nAll comments are optional.\n\n    Method comments\n    //zenrpc:\u003cmethod parameter\u003e[=\u003cdefault value\u003e][whitespaces\u003cdescription\u003e]\n    //zenrpc:\u003cerror code\u003e[whitespaces\u003cdescription\u003e]\n    //zenrpc:return[whitespaces\u003cdescription\u003e]\n     \n    Struct comments\n    type MyService struct {} //zenrpc\n    \n## Need to browse your api and do some test api calls?\nWe recommend to use [SMDBox](https://github.com/semrush/smdbox). It is Swagger-like JSON RPC API browser, compatible with smd scheme, generated by zenrpc. \n\n# JSON-RPC 2.0 Supported Features\n\n  * [x] Requests\n    * [x] Single requests\n    * [x] Batch requests\n    * [x] Notifications\n  * [x] Parameters\n    * [x] Named\n    * [x] Position\n    * [x] Default values\n  * [x] SMD Schema\n    * [x] Input\n    * [x] Output\n    * [x] Codes\n    * [ ] Scopes for OAuth\n\n# Server Library Features\n\n * [x] go generate\n * [ ] Transports\n   * [x] HTTP\n   * [x] WebSocket\n   * [ ] RabbitMQ\n * [x] Server middleware\n   * [x] Basic support\n   * [x] Metrics\n   * [x] Logging\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsemrush%2Fzenrpc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsemrush%2Fzenrpc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsemrush%2Fzenrpc/lists"}