-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
165 lines (143 loc) · 3.37 KB
/
Copy pathhandler.go
File metadata and controls
165 lines (143 loc) · 3.37 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
package rpc
import (
"context"
"encoding/json"
"fmt"
"log"
"reflect"
"golang.org/x/net/websocket"
)
func WsHandler(apiStruct any, isTokenValid func(token string, method string, params []any) bool) func(*websocket.Conn) {
return func(ws *websocket.Conn) {
apiVal := reflect.ValueOf(apiStruct)
for {
var msg []byte
if err := websocket.Message.Receive(ws, &msg); err != nil {
break
}
var req struct {
ID string `json:"id"`
Method string `json:"method"`
Params []any `json:"params"`
Token string `json:"token"`
}
if err := json.Unmarshal(msg, &req); err != nil {
_ = websocket.JSON.Send(ws, map[string]any{
"id": "",
"error": "invalid json payload",
})
continue
}
if !isTokenValid(req.Token, req.Method, req.Params) {
_ = websocket.JSON.Send(ws, map[string]any{
"id": req.ID,
"error": "token not valid",
})
return
}
method := apiVal.MethodByName(req.Method)
if !method.IsValid() {
_ = websocket.JSON.Send(ws, map[string]any{
"id": req.ID,
"error": "method not found: " + req.Method,
})
continue
}
mType := method.Type()
numIn := mType.NumIn()
hasCtx := false
startIdx := 0
ctxType := reflect.TypeOf((*context.Context)(nil)).Elem()
if numIn > 0 && mType.In(0).Implements(ctxType) {
hasCtx = true
startIdx = 1
}
expectedParams := numIn - startIdx
if len(req.Params) != expectedParams {
_ = websocket.JSON.Send(ws, map[string]any{
"id": req.ID,
"error": fmt.Sprintf(
"invalid param count: expected %d, got %d",
expectedParams, len(req.Params),
),
})
continue
}
var in []reflect.Value
if hasCtx {
ctx := context.Background()
in = append(in, reflect.ValueOf(ctx))
}
valid := true
for i := startIdx; i < numIn; i++ {
argType := mType.In(i)
raw := req.Params[i-startIdx]
argPtr := reflect.New(argType)
b, err := json.Marshal(raw)
if err != nil {
_ = websocket.JSON.Send(ws, map[string]any{
"id": req.ID,
"error": fmt.Sprintf("param %d marshal error: %v", i-startIdx, err),
})
valid = false
break
}
if err := json.Unmarshal(b, argPtr.Interface()); err != nil {
_ = websocket.JSON.Send(ws, map[string]any{
"id": req.ID,
"error": fmt.Sprintf(
"param %d unmarshal to %s failed: %v",
i-startIdx, argType.Name(), err,
),
})
valid = false
break
}
in = append(in, argPtr.Elem())
}
if !valid {
continue
}
var out []reflect.Value
func() { // protect against panics
defer func() {
if r := recover(); r != nil {
_ = websocket.JSON.Send(ws, map[string]any{
"id": req.ID,
"error": fmt.Sprintf("method panic: %v", r),
})
out = nil
}
}()
out = method.Call(in)
}()
if out == nil {
continue
}
var result any
var errVal any
if len(out) > 0 {
result = out[0].Interface()
}
if len(out) > 1 {
if errIF := out[1].Interface(); errIF != nil {
if e, ok := errIF.(error); ok {
errVal = e.Error()
} else {
errVal = "unknown error type"
}
}
}
if errVal != nil {
log.Println(errVal)
}
_ = websocket.JSON.Send(ws, map[string]any{
"id": req.ID,
"result": map[string]any{
"data": result,
"error": errVal,
},
})
}
}
}