mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-23 01:57:55 +08:00
update docs
This commit is contained in:
parent
452a911769
commit
c27057c03a
@ -8,26 +8,26 @@ The middleware has two parts:
|
|||||||
|
|
||||||
- part two is what executes on every request. For example, a database middleware you simply inject your "global" database object into the context. Once it's inside the context, you can retrieve it from within other middlewares and your handler furnction.
|
- part two is what executes on every request. For example, a database middleware you simply inject your "global" database object into the context. Once it's inside the context, you can retrieve it from within other middlewares and your handler furnction.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func funcName(params string) gin.HandlerFunc {
|
func funcName(params string) gin.HandlerFunc {
|
||||||
// <---
|
// <---
|
||||||
// This is part one
|
// This is part one
|
||||||
// --->
|
// --->
|
||||||
// The follow code is an example
|
// The follow code is an example
|
||||||
if err := check(params); err != nil {
|
if err := check(params); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
// <---
|
// <---
|
||||||
// This is part two
|
// This is part two
|
||||||
// --->
|
// --->
|
||||||
// The follow code is an example
|
// The follow code is an example
|
||||||
c.Set("TestVar", params)
|
c.Set("TestVar", params)
|
||||||
c.Next()
|
c.Next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Execution process
|
## Execution process
|
||||||
|
|
||||||
@ -41,8 +41,6 @@ func main() {
|
|||||||
|
|
||||||
router.GET("/rest/n/api/*some", mid1(), mid2(), handler)
|
router.GET("/rest/n/api/*some", mid1(), mid2(), handler)
|
||||||
|
|
||||||
fmt.Println("end")
|
|
||||||
|
|
||||||
router.Run()
|
router.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,3 +79,59 @@ func mid2() gin.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
According to [Consitituent part](#consitituent-part) said, when we run the gin process, **part one** will execute firstly and will print the follow information:
|
||||||
|
|
||||||
|
```go
|
||||||
|
globalMiddleware...1
|
||||||
|
mid1...1
|
||||||
|
mid2...1
|
||||||
|
```
|
||||||
|
|
||||||
|
And init order are:
|
||||||
|
|
||||||
|
```go
|
||||||
|
globalMiddleware...1
|
||||||
|
|
|
||||||
|
v
|
||||||
|
mid1...1
|
||||||
|
|
|
||||||
|
v
|
||||||
|
mid2...1
|
||||||
|
```
|
||||||
|
|
||||||
|
When we curl one request `curl -v localhost:8080/rest/n/api/some`, **part two** will execute their middleware and output the following information:
|
||||||
|
|
||||||
|
```go
|
||||||
|
globalMiddleware...2
|
||||||
|
mid1...2
|
||||||
|
mid2...2
|
||||||
|
exec handler.
|
||||||
|
mid2...3
|
||||||
|
mid1...3
|
||||||
|
globalMiddleware...3
|
||||||
|
```
|
||||||
|
|
||||||
|
In other words, run order are:
|
||||||
|
|
||||||
|
```go
|
||||||
|
globalMiddleware...2
|
||||||
|
|
|
||||||
|
v
|
||||||
|
mid1...2
|
||||||
|
|
|
||||||
|
v
|
||||||
|
mid2...2
|
||||||
|
|
|
||||||
|
v
|
||||||
|
exec handler.
|
||||||
|
|
|
||||||
|
v
|
||||||
|
mid2...3
|
||||||
|
|
|
||||||
|
v
|
||||||
|
mid1...3
|
||||||
|
|
|
||||||
|
v
|
||||||
|
globalMiddleware...3
|
||||||
|
```
|
||||||
|
Loading…
x
Reference in New Issue
Block a user