Skip to content
This repository has been archived by the owner on Apr 28, 2024. It is now read-only.

Commit

Permalink
Improvments. Breaking changes
Browse files Browse the repository at this point in the history
  • Loading branch information
neonxp committed May 21, 2022
1 parent d4708a3 commit 81389df
Show file tree
Hide file tree
Showing 12 changed files with 279 additions and 162 deletions.
72 changes: 48 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# JSON-RPC 2.0

Golang implementation of JSON-RPC 2.0 server with generics.
Expand All @@ -6,58 +7,79 @@ Go 1.18+ required

## Features:

- [x] Batch request and responses
- [x] HTTP/HTTPS transport
- [x] TCP transport
- [ ] WebSocket transport

## Usage (http transport)

1. Create JSON-RPC/HTTP server:
```go
import "go.neonxp.dev/jsonrpc2/http"
1. Create JSON-RPC server:
```go
import "go.neonxp.dev/jsonrpc2/rpc"
...
s := rpc.New()
```

2. Add required transport(s):
```go
import "go.neonxp.dev/jsonrpc2/transport"
...
s := http.New()
```
2. Write handler:
```go
s.AddTransport(&transport.HTTP{Bind: ":8000"})
s.AddTransport(&transport.TCP{Bind: ":3000"})
```

3. Write handler:
```go
func Multiply(ctx context.Context, args *Args) (int, error) {
return args.A * args.B, nil
}
```
```

Handler must have exact two arguments (context and input of any json serializable type) and exact two return values (output of any json serializable type and error)
3. Wrap handler with `rpc.Wrap` method and register it in server:
```go
s.Register("multiply", rpc.Wrap(Multiply))
```
4. Use server as common http handler:
```go
http.ListenAndServe(":8000", s)
```
```go
s.Register("multiply", rpc.H(Multiply))
```

4. Run RPC server:
```go
s.Run(ctx)
```

## Custom transport

See [http/server.go](/http/server.go) for example of transport implementation.
Any transport must implement simple interface `transport.Transport`:

```go
type Transport interface {
Run(ctx context.Context, resolver Resolver) error
}
```

## Complete example

[Full code](/examples/http)
[Full code](/example)

```go
package main

import (
"context"
"net/http"

httpRPC "go.neonxp.dev/jsonrpc2/http"
"go.neonxp.dev/jsonrpc2/rpc"
"go.neonxp.dev/jsonrpc2/transport"
)

func main() {
s := httpRPC.New()
s.Register("multiply", rpc.Wrap(Multiply))
s.Register("divide", rpc.Wrap(Divide))
s := rpc.New()

s.AddTransport(&transport.HTTP{Bind: ":8000"}) // HTTP transport
s.AddTransport(&transport.TCP{Bind: ":3000"}) // TCP transport

http.ListenAndServe(":8000", s)
s.Register("multiply", rpc.H(Multiply))
s.Register("divide", rpc.H(Divide))

s.Run(context.Background())
}

func Multiply(ctx context.Context, args *Args) (int, error) {
Expand Down Expand Up @@ -87,3 +109,5 @@ Alexander Kiryukhin <i@neonxp.dev>
## License

![GPL v3](https://www.gnu.org/graphics/gplv3-with-text-136x68.png)


22 changes: 16 additions & 6 deletions examples/http/main.go → example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,29 @@ package main
import (
"context"
"errors"
"net/http"
"log"
"os"
"os/signal"

httpRPC "go.neonxp.dev/jsonrpc2/http"
"go.neonxp.dev/jsonrpc2/rpc"
"go.neonxp.dev/jsonrpc2/transport"
)

func main() {
s := httpRPC.New()
s := rpc.New()

s.Register("multiply", rpc.Wrap(Multiply))
s.Register("divide", rpc.Wrap(Divide))
s.AddTransport(&transport.HTTP{Bind: ":8000"})
s.AddTransport(&transport.TCP{Bind: ":3000"})

http.ListenAndServe(":8000", s)
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) {
Expand Down
82 changes: 82 additions & 0 deletions example/test.http
Original file line number Diff line number Diff line change
@@ -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
42 changes: 0 additions & 42 deletions examples/http/test.http

This file was deleted.

2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module go.neonxp.dev/jsonrpc2

go 1.18

require golang.org/x/sync v0.0.0-20220513210516-0976fa681c29
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
golang.org/x/sync v0.0.0-20220513210516-0976fa681c29 h1:w8s32wxx3sY+OjLlv9qltkLU5yvJzxjjgiHWLjdIcw4=
golang.org/x/sync v0.0.0-20220513210516-0976fa681c29/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
52 changes: 0 additions & 52 deletions http/server.go

This file was deleted.

Loading

0 comments on commit 81389df

Please sign in to comment.