-
Notifications
You must be signed in to change notification settings - Fork 0
/
poteto_rpc.go
152 lines (136 loc) · 2.99 KB
/
poteto_rpc.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package poteto
import (
"net/http"
"reflect"
"strings"
"github.com/goccy/go-json"
"github.com/poteto-go/poteto/utils"
)
// TODO: error status code
var (
rpcVersion = "2.0"
rpcErrorStatus = -32700
rpcErrorStatusBadRequest = -32600
rpcErrorStatusNotFound = -32601
)
// inspired by
// https://github.com/kanocz/goginjsonrpc/blob/master/jsonrpc.go
// * Only Support "POST" method
func PotetoJsonRPCAdapter[T any, S any](ctx Context, api *T) error {
if ctx.GetRequest().Method != http.MethodPost {
return ctx.JSONRPCError(
rpcErrorStatus,
"parse error",
"POST method excepted",
0,
)
}
if ctx.GetRequest().Body == nil {
return ctx.JSONRPCError(
rpcErrorStatus,
"parse error",
"No Post data",
0,
)
}
data := make(map[string]any)
if err := ctx.Bind(&data); err != nil {
return ctx.JSONRPCError(
rpcErrorStatus,
"parse error",
"error during decode json",
0,
)
}
id, ok := utils.AssertToInt(data["id"])
if !ok {
return ctx.JSONRPCError(
rpcErrorStatusBadRequest,
"BadRequest",
"invalid id",
0,
)
}
version, ok := data["jsonrpc"]
if !ok || version != rpcVersion {
return ctx.JSONRPCError(
rpcErrorStatusBadRequest,
"BadRequest",
"version of jsonrpc is not 2.0",
id,
)
}
methodName, ok := data["method"].(string)
if !ok {
return ctx.JSONRPCError(
rpcErrorStatusBadRequest,
"BadRequest",
"invalid method",
id,
)
}
methodArr := strings.Split(methodName, ".")
if len(methodArr) != 2 {
return ctx.JSONRPCError(
rpcErrorStatusNotFound,
"NotFound",
"Method is not found",
id,
)
}
className, method := methodArr[0], methodArr[1]
fullClass := strings.Split(reflect.TypeOf(api).String(), ".")
extractNamespace := fullClass[len(fullClass)-1]
if className != extractNamespace {
return ctx.JSONRPCError(
rpcErrorStatusNotFound,
"NotFound",
"Method is not found",
id,
)
}
call := reflect.ValueOf(api).MethodByName(method)
if !call.IsValid() {
return ctx.JSONRPCError(
rpcErrorStatusNotFound,
"NotFound",
"Method is not found",
id,
)
}
if call.Type().NumIn() != 2 {
return ctx.JSONRPCError(
rpcErrorStatusBadRequest,
"BadRequest",
"invalid params length",
id,
)
}
var params S
bytes, _ := json.Marshal(data["params"])
err := json.Unmarshal(bytes, ¶ms)
if err != nil {
return ctx.JSONRPCError(
rpcErrorStatusBadRequest,
"BadRequest",
"invalid params",
id,
)
}
args := make([]reflect.Value, 2)
args[0] = reflect.ValueOf(ctx.GetRequest())
args[1] = reflect.ValueOf(¶ms)
result := call.Call(args)
if len(result) <= 0 {
return ctx.JSON(http.StatusOK, map[string]any{
"result": nil,
"jsonrpc": "2.0",
"id": id,
})
}
return ctx.JSON(http.StatusOK, map[string]any{
"result": result[0].Interface(),
"jsonrpc": "2.0",
"id": id,
})
}