Skip to content

Commit

Permalink
Merge pull request #7 from go-zoox/feat/refactor-jsonrpc
Browse files Browse the repository at this point in the history
feat: refactor jsonrpc
  • Loading branch information
whatwewant committed Aug 17, 2023
2 parents c3b5ef3 + 4f8f2a5 commit 13f9d5f
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 21 deletions.
16 changes: 10 additions & 6 deletions components/application/websocket/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ func (c *Client) Pong(message string) error {
return err
}

// ReadWriter ...
func (c *Client) ReadWriter() io.ReadWriteCloser {
return newRW(c)
}
Expand All @@ -342,16 +343,19 @@ func newRW(c *Client) io.ReadWriteCloser {
return rwx
}

func (w *rw) Write(p []byte) (n int, err error) {
return len(p), w.c.WriteBinary(p)
// Write ...
func (rw *rw) Write(p []byte) (n int, err error) {
return len(p), rw.c.WriteBinary(p)
}

func (r *rw) Read(p []byte) (n int, err error) {
n = copy(p, <-r.buf)
// Read ...
func (rw *rw) Read(p []byte) (n int, err error) {
n = copy(p, <-rw.buf)
return
}

func (r *rw) Close() error {
close(r.buf)
// Close ...
func (rw *rw) Close() error {
close(rw.buf)
return nil
}
1 change: 1 addition & 0 deletions components/router/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func (n *Node) Search(parts []string, height int) *Node {
return nil
}

// IsWild ...
func (n *Node) IsWild() bool {
return n.isWild
}
6 changes: 3 additions & 3 deletions rpc/jsonrpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// Client is a JSON-RPC client.
type Client[C any] interface {
Call(method string, params interface{}) (interface{}, error)
Call(method string, params Params) (Result, error)
}

type client[C any] struct {
Expand All @@ -34,9 +34,9 @@ func NewClient[C any](server string, path ...string) Client[C] {
}

// Call calls a JSON-RPC method.
func (c *client[C]) Call(method string, params interface{}) (interface{}, error) {
func (c *client[C]) Call(method string, params Params) (Result, error) {
response, err := fetch.Post(c.server+c.path, &fetch.Config{
Body: map[string]interface{}{
Body: map[string]any{
"jsonrpc": "2.0",
"method": method,
"params": params,
Expand Down
20 changes: 20 additions & 0 deletions rpc/jsonrpc/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package jsonrpc

import (
"github.com/go-zoox/core-utils/object"
"github.com/go-zoox/tag"
"github.com/go-zoox/tag/datasource"
)

// Params is a map of params.
type Params map[string]any

// Bind binds the jsonrpc params into the given struct.
func (p Params) Bind(obj any) error {
return tag.New("json", datasource.NewMapDataSource(p)).Decode(obj)
}

// Get returns the value of the given key.
func (p Params) Get(key string) any {
return object.Get(p, key)
}
16 changes: 8 additions & 8 deletions rpc/jsonrpc/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package jsonrpc

// Request is a JSON-RPC request.
type Request struct {
JSONRPC string `json:"jsonrpc"`
Method string `json:"method"`
Params interface{} `json:"params"`
ID string `json:"id"`
JSONRPC string `json:"jsonrpc"`
Method string `json:"method"`
Params Params `json:"params"`
ID string `json:"id"`
}

// Response is a JSON-RPC response.
type Response struct {
JSONRPC string `json:"jsonrpc"`
Result interface{} `json:"result"`
Error *Error `json:"error"`
ID string `json:"id"`
JSONRPC string `json:"jsonrpc"`
Result Result `json:"result"`
Error *Error `json:"error"`
ID string `json:"id"`
}

// Error is a JSON-RPC error.
Expand Down
4 changes: 4 additions & 0 deletions rpc/jsonrpc/result.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package jsonrpc

// Result is a map of result.
type Result map[string]any
13 changes: 9 additions & 4 deletions rpc/jsonrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,27 @@ import (
"github.com/go-zoox/logger"
)

// HandlerFunc is a handler function.
type HandlerFunc[C any] func(ctx C, params Params) (Result, error)

// Server is a JSON-RPC server.
type Server[C any] interface {
Register(method string, handler func(ctx C, params interface{}) (interface{}, error))
Register(method string, handler HandlerFunc[C])
Invoke(ctx C, body []byte) ([]byte, error)
}

type server[C any] struct {
methods map[string]func(ctx C, params interface{}) (interface{}, error)
methods map[string]HandlerFunc[C]
}

// NewServer creates a new JSON-RPC server.
func NewServer[C any]() Server[C] {
return &server[C]{
methods: make(map[string]func(ctx C, params interface{}) (interface{}, error)),
methods: make(map[string]HandlerFunc[C]),
}
}

func (s *server[C]) Register(method string, handler func(ctx C, params interface{}) (interface{}, error)) {
func (s *server[C]) Register(method string, handler HandlerFunc[C]) {
s.methods[method] = handler
}

Expand Down Expand Up @@ -73,6 +76,8 @@ func (s *server[C]) Invoke(ctx C, body []byte) ([]byte, error) {
// fmt.Println("request.Method", request.Method)
// fmt.Println("request.Params", request.Params)

logger.Infof("[jsonrpc][id: %s][method: %s]", request.ID, request.Method)

response.ID = request.ID

handler, ok := s.methods[request.Method]
Expand Down

0 comments on commit 13f9d5f

Please sign in to comment.