-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.go
129 lines (122 loc) · 3.03 KB
/
wrapper.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
package mix
import (
"bytes"
"encoding/json"
"fmt"
"github.com/gozelle/fastjson"
"github.com/gozelle/gin"
"github.com/gozelle/jsonrpc"
"io"
"io/ioutil"
"net/http"
)
const placeholder = "$params$"
const method = "method"
const module = "module"
// 包装 API
// ns == "" => gin.Post("/api/v0/:method")
// ns != "" => gin.Post("/api/v0/:namespace/:method")
func wrapAPI(ns string, h http.Handler) gin.HandlerFunc {
return func(ctx *gin.Context) {
body := ctx.Request.Body
data, err := ioutil.ReadAll(body)
if err != nil {
_ = ctx.AbortWithError(http.StatusInternalServerError, fmt.Errorf("read request body error:%s", err))
}
var m string
if ns == "" {
m = fmt.Sprintf(".%s", ctx.Param(method))
} else {
m = fmt.Sprintf("%s.%s", ctx.Param(module), ctx.Param(method))
}
r := map[string]interface{}{
"method": m,
"params": placeholder,
}
i, err := json.Marshal(r)
if err != nil {
_ = ctx.AbortWithError(http.StatusInternalServerError, fmt.Errorf("prepare params error: %s", err))
return
}
var params []byte
if len(data) > 0 {
params, err = wrapData(data)
if err != nil {
_ = ctx.AbortWithError(http.StatusNotAcceptable, err)
return
}
} else {
params = []byte{91, 93}
}
i = bytes.Replace(i, []byte(fmt.Sprintf("\"%s\"", placeholder)), params, 1)
ctx.Request.Body = ioutil.NopCloser(bytes.NewBuffer(i))
if err != nil {
log.Errorf(" error: %s", err)
return
}
h.ServeHTTP(ctx.Writer, ctx.Request)
}
}
func wrapData(data []byte) ([]byte, error) {
j, err := fastjson.ParseBytes(data)
if err != nil {
err = fmt.Errorf("parse data json error: %s", err)
return nil, fmt.Errorf("parse data json error: %s", err)
}
switch j.Type() {
case fastjson.TypeArray:
return data, nil
}
return bytes.Join([][]byte{{91}, data, {93}}, []byte{}), nil
}
func WrapHandler(wrap func(ctx *gin.Context) (any, error)) gin.HandlerFunc {
return func(ctx *gin.Context) {
r, err := wrap(ctx)
if err != nil {
if e, ok := err.(*Warn); ok {
ctx.Header(jsonrpc.X_RPC_ERROR, e.Message)
ctx.JSON(http.StatusBadRequest, &jsonrpc.Response{
ID: ctx.Writer.Header().Get(jsonrpc.X_RPC_ID),
Error: e,
})
} else {
HandleServerError(ctx, err)
}
return
}
if bs, ok1 := r.([]byte); ok1 {
_, err = ctx.Writer.Write(bs)
if err != nil {
HandleServerError(ctx, err)
return
}
} else if reader, ok2 := r.(io.Reader); ok2 {
var d []byte
d, err = ioutil.ReadAll(reader)
if err != nil {
HandleServerError(ctx, err)
return
}
_, err = ctx.Writer.Write(d)
if err != nil {
HandleServerError(ctx, err)
return
}
} else {
ctx.JSON(200, &jsonrpc.Response{
ID: ctx.Writer.Header().Get(jsonrpc.X_RPC_ID),
Result: r,
})
}
}
}
func HandleServerError(ctx *gin.Context, err error) {
ctx.Header(jsonrpc.X_RPC_ERROR, err.Error())
ctx.JSON(http.StatusInternalServerError, &jsonrpc.Response{
ID: ctx.Writer.Header().Get(jsonrpc.X_RPC_ID),
Error: &jsonrpc.Error{
Code: 500,
Message: err.Error(),
},
})
}