This repository has been archived by the owner on Apr 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgin.go
112 lines (94 loc) · 3.74 KB
/
gin.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
/*
@Time : 2020/4/19 1:38 下午
@Author : Rebeta
@Email : [email protected]
@File : chaos
@Software: GoLand
*/
package gin
import (
"context"
"github.com/offcn-jl/gscf/fake-http"
"github.com/tencentyun/scf-go-lib/cloudevents/scf"
"github.com/tencentyun/scf-go-lib/cloudfunction"
)
// HandlerFunc defines the handler used by gin middleware as return value. // fixme 翻译
type HandlerFunc func(*Context)
// HandlersChain defines a HandlerFunc array. // fixme 翻译
type HandlersChain []HandlerFunc
// Last returns the last handler in the chain. ie. the last handler is the main own. // fixme 翻译
func (c HandlersChain) Last() HandlerFunc {
if length := len(c); length > 0 {
return c[length-1]
}
return nil
}
// Engine is the framework's instance, it contains the muxer, middleware and configuration settings.
// Create an instance of Engine, by using New() or Default() // fixme 翻译
type Engine struct {
Handlers HandlersChain // 将中间件列表从 router 中提取到 engine 中
}
// New returns a new blank Engine instance without any middleware attached.
// By default the configuration is:
// - RedirectTrailingSlash: true
// - RedirectFixedPath: false
// - HandleMethodNotAllowed: false
// - ForwardedByClientIP: true
// - UseRawPath: false
// - UnescapePathValues: true // fixme 翻译
func New() *Engine {
debugPrintWARNINGNew()
engine := &Engine{
//trees: make(methodTrees, 0, 9), fixme 未确定是否保留
//delims: render.Delims{Left: "{{", Right: "}}"}, fixme 未确定是否保留
}
//engine.RouterGroup.engine = engine fixme 未确定是否保留
//engine.pool.New = func() interface{} { fixme 未确定是否保留
// return engine.allocateContext()
//}
return engine
}
// Default returns an Engine instance with the Logger and Recovery middleware already attached. // fixme 翻译
func Default() *Engine {
debugPrintWARNINGDefault()
engine := New()
engine.Use(Logger(), Recovery())
return engine
}
func (engine *Engine) allocateContext() *Context {
return &Context{engine: engine}
}
// Use attaches a global middleware to the router. ie. the middleware attached though Use() will be
// included in the handlers chain for every single request. Even 404, 405, static files...
// For example, this is the right place for a logger or error management middleware. // fixme 翻译
func (engine *Engine) Use(middleware ...HandlerFunc) *Engine {
engine.Handlers = append(engine.Handlers, middleware...) // 将添加中间件的逻辑从 router 中提取到 engine 中进行处理,并且精简掉了 router 中的 GET、 POST 等方法,直接将最终的处理函数作为 USE 的最后一个参数即可
return engine
}
// Run attaches the router to a http.Server and starts listening and serving HTTP requests.
// It is a shortcut for http.ListenAndServe(addr, router)
// Note: this method will block the calling goroutine indefinitely unless an error happens. // fixme 翻译
// 此函数与 Gin 不同, 没有会返回 err 的步骤, 所以去掉了返回值中的 err, 以及用于 handle err 的 defer
func (engine *Engine) Run() {
debugPrint("Start Running.")
cloudfunction.Start(engine.start)
}
// 基于 scf 实现 handleHTTPRequest 的逻辑
func (engine *Engine) start(ctx context.Context, event scf.APIGatewayProxyRequest) (scf.APIGatewayProxyResponse, error) {
// 初始化上下文
c := new(Context)
// 向上下文添加引擎
c.engine = engine
// 向上下文添加请求内容
c.Request = event
// 初始化引擎
c.reset()
// 执行调用链
c.Next()
// 如果未设置响应状态码, 则添加默认响应状态码 StatusOK
if c.Response.StatusCode == 0 {
c.Response.StatusCode = http.StatusOK
}
// 返回响应体给 SCF
return c.Response, nil
}