forked from YaoApp/gou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.http.go
450 lines (400 loc) · 11.5 KB
/
api.http.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package gou
import (
"bytes"
"fmt"
"io/ioutil"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"github.com/gin-gonic/gin"
jsoniter "github.com/json-iterator/go"
"github.com/yaoapp/gou/query/share"
"github.com/yaoapp/gou/session"
"github.com/yaoapp/kun/any"
"github.com/yaoapp/kun/exception"
"github.com/yaoapp/kun/maps"
"github.com/yaoapp/xun"
)
// HTTPGuards 支持的中间件
var HTTPGuards = map[string]gin.HandlerFunc{}
// ProcessGuard guard process
func ProcessGuard(name string) gin.HandlerFunc {
return func(c *gin.Context) {
var body interface{}
bodyBytes, err := ioutil.ReadAll(c.Request.Body)
if err == nil {
if strings.HasPrefix(strings.ToLower(c.Request.Header.Get("Content-Type")), "application/json") {
jsoniter.Unmarshal(bodyBytes, &body)
} else {
body = string(bodyBytes)
}
}
// Reset body
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
params := map[string]string{}
for _, param := range c.Params {
params[param.Key] = param.Value
}
args := []interface{}{
c.FullPath(), // api path
params, // api params
c.Request.URL.Query(), // query string
body, // payload
c.Request.Header, // Request headers
}
var process = NewProcess(name, args...)
if sid, has := c.Get("__sid"); has { // 设定会话ID
if sid, ok := sid.(string); ok {
process.WithSID(sid)
}
}
if global, has := c.Get("__global"); has { // 设定全局变量
if global, ok := global.(map[string]interface{}); ok {
process.WithGlobal(global)
}
}
process.Run()
}
}
// IsAllowed check if the referer is in allow list
func IsAllowed(c *gin.Context, allowsMap map[string]bool) bool {
referer := c.Request.Referer()
if referer != "" {
url, err := url.Parse(referer)
if err != nil {
return true
}
port := fmt.Sprintf(":%s", url.Port())
if port == ":" || port == ":80" || port == ":443" {
port = ""
}
host := fmt.Sprintf("%s%s", url.Hostname(), port)
// fmt.Println(url, host, c.Request.Host)
// fmt.Println(allowsMap)
if host == c.Request.Host {
return true
}
if _, has := allowsMap[host]; !has {
return false
}
}
return true
}
// Routes 配置转换为路由
func (http HTTP) Routes(router *gin.Engine, root string, allows ...string) {
var group gin.IRoutes = router
if http.Group != "" {
root = path.Join(root, "/", http.Group)
}
group = router.Group(root)
for _, path := range http.Paths {
http.Route(group, path, allows...)
}
}
// Route 路径配置转换为路由
func (http HTTP) Route(router gin.IRoutes, path Path, allows ...string) {
getArgs := http.parseIn(path.In)
handlers := []gin.HandlerFunc{}
// 跨域访问
if allows != nil && len(allows) > 0 {
allowsMap := map[string]bool{}
for _, allow := range allows {
allowsMap[allow] = true
}
// Cross domain
http.crossDomain(path.Path, allowsMap, router)
handlers = append(handlers, func(c *gin.Context) {
referer := c.Request.Referer()
if referer != "" {
if !IsAllowed(c, allowsMap) {
c.AbortWithStatus(403)
return
}
// url parse
url, _ := url.Parse(referer)
referer = fmt.Sprintf("%s://%s", url.Scheme, url.Host)
// fmt.Println("referer is:", referer)
c.Writer.Header().Set("Access-Control-Allow-Origin", referer)
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
}
})
}
// 中间件
http.guard(&handlers, path.Guard, http.Guard)
// API响应逻辑
handlers = append(handlers, func(c *gin.Context) {
if strings.HasPrefix(strings.ToLower(c.GetHeader("content-type")), "application/json") {
bytes, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
panic(err)
}
if bytes == nil || len(bytes) == 0 {
c.Set("__payloads", map[string]interface{}{})
} else {
payloads := map[string]interface{}{}
err = jsoniter.Unmarshal(bytes, &payloads)
if err != nil {
panic(err)
}
c.Set("__payloads", payloads)
}
}
// 运行 Process
var args []interface{} = getArgs(c)
// 如果 path.Guard == "in-process" 在调用中鉴权
// if path.Guard == "in-process" || (path.Guard == "" && http.Guard == "in-process") {
// args = append(args, c)
// }
var process = NewProcess(path.Process, args...)
if sid, has := c.Get("__sid"); has { // 设定会话ID
if sid, ok := sid.(string); ok {
process.WithSID(sid)
}
}
if global, has := c.Get("__global"); has { // 设定全局变量
if global, ok := global.(map[string]interface{}); ok {
process.WithGlobal(global)
}
}
var resp interface{} = process.Run()
var status int = path.Out.Status
var contentType string = path.Out.Type
if contentType != "" {
c.Writer.Header().Set("Content-Type", contentType)
}
// Response Headers
if len(path.Out.Headers) > 0 {
res := any.Of(resp)
if res.IsMap() { // 处理变量
data := res.Map().MapStrAny.Dot()
for name, value := range path.Out.Headers {
v := share.Bind(value, data)
if v != nil {
c.Writer.Header().Set(name, fmt.Sprintf("%v", v))
}
}
} else {
for name, value := range path.Out.Headers {
c.Writer.Header().Set(name, value)
}
}
}
if resp == nil {
c.Done()
return
}
switch resp.(type) {
case maps.Map, map[string]interface{}, []interface{}, []maps.Map, []map[string]interface{}:
c.JSON(status, resp)
c.Done()
return
default:
if contentType == "application/json" {
c.JSON(status, resp)
} else {
c.String(status, "%v", resp)
}
c.Done()
return
}
})
http.method(path.Method, path.Path, router, handlers...)
}
// 加载特定中间件
func (http HTTP) guard(handlers *[]gin.HandlerFunc, guard string, defaults string) {
if guard == "" {
guard = defaults
}
if guard != "-" {
guards := strings.Split(guard, ",")
for _, name := range guards {
name = strings.TrimSpace(name)
if handler, has := HTTPGuards[name]; has {
*handlers = append(*handlers, handler)
} else { // run process process
*handlers = append(*handlers, ProcessGuard(name))
}
}
}
}
// crossDomain 跨域许可
func (http HTTP) crossDomain(path string, allows map[string]bool, router gin.IRoutes) {
http.method("OPTIONS", path, router, func(c *gin.Context) {
referer := c.Request.Referer()
if referer != "" {
if !IsAllowed(c, allows) {
c.AbortWithStatus(403)
return
}
// url parse
url, _ := url.Parse(referer)
referer = fmt.Sprintf("%s://%s", url.Scheme, url.Host)
c.Writer.Header().Set("Access-Control-Allow-Origin", referer)
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
c.AbortWithStatus(204)
}
})
}
// parseIn 接口传参解析 (这个函数应该重构)
func (http HTTP) parseIn(in []string) func(c *gin.Context) []interface{} {
getValues := []func(c *gin.Context) interface{}{}
for _, v := range in {
if v == ":body" {
getValues = append(getValues, func(c *gin.Context) interface{} {
bytes, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
panic(err)
}
return string(bytes)
})
continue
} else if v == ":fullpath" {
getValues = append(getValues, func(c *gin.Context) interface{} {
return c.FullPath()
})
continue
} else if v == ":headers" {
getValues = append(getValues, func(c *gin.Context) interface{} {
return c.Request.Header
})
continue
} else if v == ":payload" {
getValues = append(getValues, func(c *gin.Context) interface{} {
value, has := c.Get("__payloads")
if !has {
return maps.MapStr{}
}
valueMap, ok := value.(map[string]interface{})
if !ok {
return maps.MapStr{}
}
return valueMap
})
continue
} else if v == ":query" {
getValues = append(getValues, func(c *gin.Context) interface{} {
return c.Request.URL.Query()
})
continue
} else if v == ":form" {
getValues = append(getValues, func(c *gin.Context) interface{} {
values := c.Request.PostForm
return values
})
continue
} else if v == ":params" || v == ":query-param" {
getValues = append(getValues, func(c *gin.Context) interface{} {
values := c.Request.URL.Query()
return URLToQueryParam(values)
})
continue
} else if v == ":context" {
getValues = append(getValues, func(c *gin.Context) interface{} {
return c
})
continue
}
arg := strings.Split(v, ".")
length := len(arg)
if arg[0] == "$form" && length == 2 {
getValues = append(getValues, func(c *gin.Context) interface{} {
return c.PostForm(arg[1])
})
} else if arg[0] == "$param" && length == 2 {
getValues = append(getValues, func(c *gin.Context) interface{} {
return c.Param(arg[1])
})
} else if arg[0] == "$query" && length == 2 {
getValues = append(getValues, func(c *gin.Context) interface{} {
return c.Query(arg[1])
})
} else if arg[0] == "$payload" && length == 2 {
getValues = append(getValues, func(c *gin.Context) interface{} {
if payloads, has := c.Get("__payloads"); has {
if value, has := payloads.(map[string]interface{})[arg[1]]; has {
return value
}
}
return ""
})
} else if arg[0] == "$session" && length == 2 {
getValues = append(getValues, func(c *gin.Context) interface{} {
if sid := c.GetString("__sid"); sid != "" {
name := arg[1]
return session.Global().ID(sid).MustGet(name)
}
return ""
})
} else if arg[0] == "$file" && length == 2 {
getValues = append(getValues, func(c *gin.Context) interface{} {
file, err := c.FormFile(arg[1])
ext := filepath.Ext(file.Filename)
if err != nil {
exception.New("读取上传文件出错 %s", 500, err).Throw()
}
dir, err := ioutil.TempDir(os.TempDir(), "upload")
if err != nil {
exception.New("创建临时文件夹 %s", 500, err).Throw()
}
tmpfile, err := ioutil.TempFile(dir, fmt.Sprintf("file-*%s", ext))
if err != nil {
exception.New("创建临时文件出错 %s", 500, err).Throw()
}
err = c.SaveUploadedFile(file, tmpfile.Name())
if err != nil {
exception.New("保存文件出错 %s", 500, err).Throw()
}
return xun.UploadFile{
Name: file.Filename,
TempFile: tmpfile.Name(),
Size: file.Size,
Header: file.Header,
}
})
} else { // 原始数值
new := v
getValues = append(getValues, func(c *gin.Context) interface{} {
return new
})
}
}
return func(c *gin.Context) []interface{} {
values := []interface{}{}
for _, get := range getValues {
values = append(values, get(c))
}
return values
}
}
// router 方法设定
func (http HTTP) method(name string, path string, router gin.IRoutes, handlers ...gin.HandlerFunc) {
switch name {
case "POST":
router.POST(path, handlers...)
return
case "GET":
router.GET(path, handlers...)
return
case "PUT":
router.PUT(path, handlers...)
return
case "DELETE":
router.DELETE(path, handlers...)
return
case "HEAD":
router.HEAD(path, handlers...)
return
case "Any":
router.Any(path, handlers...)
return
case "OPTIONS":
router.OPTIONS(path, handlers...)
return
}
}