Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dnbsd/jsonrpc
Build modular JSON-RPC services.
https://github.com/dnbsd/jsonrpc
json-rpc
Last synced: 17 days ago
JSON representation
Build modular JSON-RPC services.
- Host: GitHub
- URL: https://github.com/dnbsd/jsonrpc
- Owner: dnbsd
- License: mit
- Created: 2024-03-02T14:13:14.000Z (10 months ago)
- Default Branch: master
- Last Pushed: 2024-05-14T05:30:12.000Z (8 months ago)
- Last Synced: 2024-11-02T01:23:47.833Z (2 months ago)
- Topics: json-rpc
- Language: Go
- Homepage:
- Size: 23.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# json-rpc
[![GoDoc][GoDoc-Image]][GoDoc-Url]
[GoDoc-Image]: https://img.shields.io/badge/GoDoc-reference-007d9c
[GoDoc-Url]: https://pkg.go.dev/github.com/dnbsd/jsonrpc## Installation
```
go get github.com/dnbsd/jsonrpc
```## Usage
```go
import "github.com/dnbsd/jsonrpc"builder := jsonrpc.Builder{
Modules: []jsonrpc.Module{
{
Name: "math",
Methods: []jsonrpc.Method{
{
Name: "version",
Handler: func(c *jsonrpc.Context) (any, error) {
return c.Result("v1.0")
},
},
{
Name: "add",
Handler: func(c *jsonrpc.Context) (any, error) {
params, err := c.ParamsArray()
if err != nil {
return c.Error(err)
}var sum int
for i := range params {
n, err := params.Number(i)
if err != nil {
return c.Error(err)
}
sum += n.Int()
}return c.Result(sum)
},
},
},
},
},
}service, err := builder.Build()
if err != nil {
panic(err)
}req := jsonrpc.NewRequest(1, "math.add", jsonrpc.ParamsArray{10, 20})
resp := service.Call(context.Background(), req)
if resp.Error != nil {
panic(err)
}println("sum", resp.Result.(int))
```## License
MIT