-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
189 lines (172 loc) · 4.18 KB
/
main.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"reflect"
"strings"
"mock_server/test"
"github.com/gorilla/websocket"
)
var listen string
var id string
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
var registerMap map[string]any
func init() {
registerMap = map[string]any{
"test": new(test.TestService),
}
}
// set header
func headerAdd(w *http.ResponseWriter) {
(*w).Header().Add("Onf-Endpoint", id)
}
func handleWebSocketConnection(w http.ResponseWriter, r *http.Request) {
headerAdd(&w)
conn, err := upgrader.Upgrade(w, r, w.Header())
if err != nil {
log.Println("WebSocket upgrade failed:", err)
return
}
defer conn.Close()
conn.PongHandler()
for true {
_, message, err := conn.ReadMessage()
if err != nil {
log.Println(err)
}
jsonRpcArr, err := test.DecodeJSONRPC(message)
//reflect.TypeOf()
//对应 map xxx.xxx
if err != nil {
log.Println("decode is err", err)
return
}
//batch
for _, RpcArr := range jsonRpcArr {
// TODO
val, err := register(RpcArr, false)
if err != nil {
fmt.Fprint(w, err)
} else {
conn.WriteJSON(val)
}
}
}
}
func register(RpcArr *test.RPC, Json bool) (any, error) {
methodsStrArr := strings.Split(RpcArr.RPCHeader.Method, "_")
if len(methodsStrArr) < 2 {
return nil, errors.New("method must be xxx_xxx")
}
end, ok := registerMap[methodsStrArr[0]]
if !ok {
//return nil, errors.New("no register router")
}
valueReflect := reflect.TypeOf(end)
v := reflect.ValueOf(end)
for i := 0; i < valueReflect.NumMethod(); i++ {
method := valueReflect.Method(i)
if strings.ToLower(method.Name) == strings.ToLower(methodsStrArr[1]) {
//method.Func
var data []interface{}
err := json.Unmarshal([]byte(RpcArr.Params), &data)
if err != nil {
return nil, fmt.Errorf("json unmarshall err:%v", err)
}
if len(data) != method.Type.NumIn()-1 {
return nil, errors.New("input params of quantity != the method of parameter")
}
args := make([]reflect.Value, len(data))
for i := 0; i < method.Type.NumIn(); i++ {
paramType := method.Type.In(i)
// -1 name of methods
if i == 0 {
continue
}
// more layer structure
if reflect.TypeOf(map[string]interface{}{}) == reflect.TypeOf(data[i-1]) {
jsonBytes, err := json.Marshal(data[i-1])
if err != nil {
return nil, fmt.Errorf("Error: %v", err)
}
value := reflect.New(paramType)
err = json.Unmarshal(jsonBytes, value.Interface())
if err != nil {
return nil, fmt.Errorf("Error: %v", err)
}
args[i-1] = value.Elem()
} else {
//base type
args[i-1] = reflect.ValueOf(data[i-1]).Convert(paramType)
}
//fmt.Printf("Parameter %d: %s\n", i-1, paramType)
}
result := v.MethodByName(method.Name).Call(args)
results := make([]interface{}, len(result))
for i, v := range result {
results[i] = v.Interface()
}
if !Json {
return results, nil
}
val, err := json.Marshal(results)
if err != nil {
log.Println("marshal err:", err)
}
return val, nil
}
}
return nil, errors.New("can't find this method ,but struct is existed")
}
func handleJSONRPCRequest(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Invalid method", http.StatusMethodNotAllowed)
return
}
headerAdd(&w)
body, _ := ioutil.ReadAll(r.Body)
//[]byte
jsonRpcArr, err := test.DecodeJSONRPC(body)
//reflect.TypeOf()
//对应 map xxx.xxx
if err != nil {
log.Println("decode is err", err)
return
}
//batch
for _, RpcArr := range jsonRpcArr {
// TODO
val, err := register(RpcArr, true)
if err != nil {
fmt.Fprint(w, err)
} else {
byptVal := val.([]byte)
fmt.Fprint(w, string(byptVal))
}
}
}
func main() {
listen = "3000"
//TODO compatible command line startup
flag.StringVar(&listen, "listen", "3000", "address to listen to")
flag.StringVar(&id, "id", "", "id for rpc server ")
flag.Parse()
if id == "" {
id = listen
}
//rpc
http.HandleFunc("/ws", handleWebSocketConnection)
http.HandleFunc("/rpc", handleJSONRPCRequest)
log.Fatal(http.ListenAndServe(":"+listen, nil))
}