diff options
Diffstat (limited to 'example')
-rw-r--r-- | example/main.go | 53 | ||||
-rw-r--r-- | example/test.http | 82 |
2 files changed, 135 insertions, 0 deletions
diff --git a/example/main.go b/example/main.go new file mode 100644 index 0000000..9f25e61 --- /dev/null +++ b/example/main.go @@ -0,0 +1,53 @@ +package main + +import ( + "context" + "errors" + "log" + "os" + "os/signal" + + "go.neonxp.dev/jsonrpc2/rpc" + "go.neonxp.dev/jsonrpc2/transport" +) + +func main() { + s := rpc.New() + + s.AddTransport(&transport.HTTP{Bind: ":8000"}) + s.AddTransport(&transport.TCP{Bind: ":3000"}) + + s.Register("multiply", rpc.H(Multiply)) + s.Register("divide", rpc.H(Divide)) + + ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill) + defer cancel() + + if err := s.Run(ctx); err != nil { + log.Fatal(err) + } +} + +func Multiply(ctx context.Context, args *Args) (int, error) { + return args.A * args.B, nil +} + +func Divide(ctx context.Context, args *Args) (*Quotient, error) { + if args.B == 0 { + return nil, errors.New("divide by zero") + } + quo := new(Quotient) + quo.Quo = args.A / args.B + quo.Rem = args.A % args.B + return quo, nil +} + +type Args struct { + A int `json:"a"` + B int `json:"b"` +} + +type Quotient struct { + Quo int `json:"quo"` + Rem int `json:"rem"` +} diff --git a/example/test.http b/example/test.http new file mode 100644 index 0000000..7ed134a --- /dev/null +++ b/example/test.http @@ -0,0 +1,82 @@ +POST http://localhost:8000/ +Content-Type: application/json +{ + "jsonrpc": "2.0", + "method": "multiply", + "params": { + "a": 2, + "b": 3 + }, + "id": 1 +} + + +date: Sat, 21 May 2022 16:53:31 GMT +content-length: 36 +content-type: text/plain; charset=utf-8 +connection: close + +HTTP/1.1 200 - OK +date: Sat, 21 May 2022 16:53:31 GMT +content-length: 36 +content-type: text/plain; charset=utf-8 +connection: close + + +### +POST http://localhost:8000/ +Content-Type: application/json +{ + "jsonrpc": "2.0", + "method": "divide", + "params": { + "a": 10, + "b": 3 + }, + "id": 2 +} + + +date: Sat, 21 May 2022 16:53:51 GMT +content-length: 52 +content-type: text/plain; charset=utf-8 +connection: close + +HTTP/1.1 200 - OK +date: Sat, 21 May 2022 16:53:51 GMT +content-length: 52 +content-type: text/plain; charset=utf-8 +connection: close + + +### Batch request +POST http://localhost:8000/ +Content-Type: application/json + { "jsonrpc": "2.0", "method": "multiply", "params": { "a": 2, "b": 3 }, "id": 10 } + {"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"} + {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]} + {"jsonrpc": "2.0", "method": "subtract", "params": [42,23], "id": "2"} + { + "jsonrpc": "2.0", + "method": "divide", + "params": { + "a": 10, + "b": 3 + }, + "id": "divide" + } + {"foo": "boo"} + {"jsonrpc": "2.0", "method": "foo.get", "params": {"name": "myself"}, "id": "5"} + {"jsonrpc": "2.0", "method": "get_data", "id": "9"} + + +date: Sat, 21 May 2022 17:18:17 GMT +content-type: text/plain; charset=utf-8 +connection: close +transfer-encoding: chunked + +HTTP/1.1 200 - OK +date: Sat, 21 May 2022 17:18:17 GMT +content-type: text/plain; charset=utf-8 +connection: close +transfer-encoding: chunked
\ No newline at end of file |