-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
125 lines (101 loc) · 2.53 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package server
import (
"context"
"encoding/json"
"github.com/go-zoox/jsonrpc"
"github.com/go-zoox/logger"
)
// HandlerFunc is a handler function.
type HandlerFunc func(ctx context.Context, params jsonrpc.Params) (jsonrpc.Result, error)
// Server is a JSON-RPC server.
type Server interface {
Register(method string, handler HandlerFunc)
Invoke(ctx context.Context, body []byte) ([]byte, error)
//
Path() string
Run(addr ...string) error
}
type server struct {
path string
//
methods map[string]HandlerFunc
}
// New creates a new JSON-RPC server.
func New(path ...string) Server {
pathX := jsonrpc.JSONRPCDefaultPath
if len(path) > 0 {
if path[0] != "" {
pathX = path[0]
}
}
return &server{
path: pathX,
methods: make(map[string]HandlerFunc),
}
}
func (s *server) Register(method string, handler HandlerFunc) {
s.methods[method] = handler
}
func (s *server) Invoke(ctx context.Context, body []byte) ([]byte, error) {
response := &jsonrpc.Response{
JSONRPC: jsonrpc.JSONRPCVersion,
}
var request jsonrpc.Request
err := json.Unmarshal(body, &request)
if err != nil {
logger.Info("jsonrpc: invalid request: %s(%s)", err, string(body))
response.Error = &jsonrpc.Error{
Code: -32700,
Message: "Parse error",
}
return json.Marshal(response)
}
if request.JSONRPC != "2.0" {
response.Error = &jsonrpc.Error{
Code: -32600,
Message: "Invalid Request (invlid JSON-RPC version)",
}
return json.Marshal(response)
}
if request.Method == "" {
response.Error = &jsonrpc.Error{
Code: -32600,
Message: "Invalid Request (method is required)",
}
return json.Marshal(response)
}
if request.ID == "" {
response.Error = &jsonrpc.Error{
Code: -32600,
Message: "Invalid Request (id is required)",
}
return json.Marshal(response)
}
// fmt.Println("request.ID", request.ID)
// 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]
if !ok {
response.Error = &jsonrpc.Error{
Code: -32601,
Message: "Method not found",
}
return json.Marshal(response)
}
result, err := handler(ctx, request.Params)
if err != nil {
response.Error = &jsonrpc.Error{
Code: -32603,
Message: err.Error(),
}
return json.Marshal(response)
}
response.Result = result
return json.Marshal(response)
}
// Path returns the path of the server.
func (s *server) Path() string {
return s.path
}