1
- ## 一 Gin中间件使用
1
+ ## 一 Gin中间件
2
2
3
- #### 1.1 基本使用
3
+ ### 1.1 中间件的概念
4
4
5
5
gin框架允许在处理请求时,加入用户自己的钩子函数,该钩子函数即中间件。他的作用与Java中的拦截器,Node中的中间件相似。
6
6
7
7
中间件需要返回` gin.HandlerFunc ` 函数,多个中间件通过Next函数来依次执行。
8
8
9
- 现在设计一个中间件,在每次路由函数执行前打印一句话,在上一节的项目基础上新建` middleware ` 文件夹,新建一个中间件文件` MyFmt.go ` :
10
-
9
+ ### 1.2 入门使用案例
11
10
11
+ 现在设计一个中间件,在每次路由函数执行前打印一句话,在上一节的项目基础上新建` middleware ` 文件夹,新建一个中间件文件` MyFmt.go ` :
12
12
``` go
13
13
package middleware
14
14
15
15
import (
16
16
" fmt"
17
17
" github.com/gin-gonic/gin"
18
18
)
19
- func MyFMT () gin .HandlerFunc {
20
19
20
+ // 定义一个中间件
21
+ func MyFMT () gin .HandlerFunc {
21
22
return func (c *gin.Context ) {
22
23
host := c.Request .Host
23
24
fmt.Printf (" Before: %s \n " ,host)
24
25
c.Next ()
25
26
fmt.Println (" Next: ..." )
26
27
}
27
-
28
28
}
29
-
30
29
```
31
30
32
31
在路由函数中使用中间件:
@@ -41,8 +40,9 @@ Next: ...
41
40
[GIN] 2019/07/28 - 16:28:16 | 200 | 266.33µs | ::1 | GET /user/login
42
41
```
43
42
44
- #### 1.2 全局中间件
43
+ ### 1.2 中间件的详细使用方式
45
44
45
+ 全局中间件:直接使用 ` gin.Engine ` 结构体的` Use() ` 方法,中间件将会在项目的全局起作用。
46
46
``` go
47
47
func InitRouter () *gin .Engine {
48
48
@@ -59,3 +59,64 @@ func InitRouter() *gin.Engine {
59
59
}
60
60
```
61
61
62
+ 路由分钟中使用中间件:
63
+ ``` go
64
+ router := gin.New ()
65
+ user := router.Group (" user" , gin.Logger (),gin.Recovery ())
66
+ {
67
+ user.GET (" info" , func (context *gin.Context ) {
68
+
69
+ })
70
+ user.GET (" article" , func (context *gin.Context ) {
71
+
72
+ })
73
+ }
74
+ ```
75
+
76
+ 单个路由使用中间件(支持多个中间件的使用):
77
+ ``` go
78
+ router := gin.New ()
79
+ router.GET (" /test" ,gin.Recovery (),gin.Logger (),func (c *gin.Context ){
80
+ c.JSON (200 ," test" )
81
+ })
82
+ ```
83
+
84
+ ### 1.3 内置中间件
85
+
86
+ Gin也内置了一些中间件,可以直接使用:
87
+ ``` go
88
+ func BasicAuth (accounts Accounts ) HandlerFunc
89
+ func BasicAuthForRealm(accounts Accounts, realm string) HandlerFunc
90
+ func Bind(val interface{}) HandlerFunc // 拦截请求参数并进行绑定
91
+ func ErrorLogger () HandlerFunc // 错误日志处理
92
+ func ErrorLoggerT(typ ErrorType) HandlerFunc // 自定义类型的错误日志处理
93
+ func Logger() HandlerFunc // 日志记录
94
+ func LoggerWithConfig(conf LoggerConfig) HandlerFunc
95
+ func LoggerWithFormatter(f LogFormatter) HandlerFunc
96
+ func LoggerWithWriter(out io.Writer, notlogged ...string) HandlerFunc
97
+ func Recovery() HandlerFunc
98
+ func RecoveryWithWriter(out io.Writer) HandlerFunc
99
+ func WrapF(f http.HandlerFunc) HandlerFunc // 将http.HandlerFunc包装成中间件
100
+ func WrapH(h http.Handler) HandlerFunc // 将http.Handler包装成中间件
101
+ ```
102
+
103
+ ## 二 请求的拦截与后置
104
+
105
+ 中间件的最大作用就是拦截过滤请求,比如我们有些请求需要用户登录或者需要特定权限才能访问,这时候便可以中间件中做过滤拦截。
106
+
107
+ 下面三个方法中断请求后,直接返回200,但响应的body中不会有数据:
108
+ ```go
109
+ func (c *Context) Abort()
110
+ func (c *Context) AbortWithError(code int, err error) *Error
111
+ func (c *Context) AbortWithStatus(code int)
112
+ func (c *Context) AbortWithStatusJSON(code int, jsonObj interface{}) // 中断后可以返回json数据
113
+ ```
114
+
115
+ 如果在中间件中调用gin.Context的Next()方法,则可以请求到达并完成业务处理后,再经过中间件后置拦截处理:
116
+ ``` go
117
+ func MyMiddleware (c *gin .Context ){
118
+ // 请求前
119
+ c.Next ()
120
+ // 请求后
121
+ }
122
+ ```
0 commit comments