Skip to content
Myr

document: add docs dir and middleware document (#1521)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* init docs dir * add middleware document * fix indent * update docs

authored by 田欧 · committed by GitHub · parent 523435e · Browse files · 1 file+137 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+137 added−0 removed

  • other:+137 added−0 removed

1 file+137 added−0 removed

added:docs/how-to-build-an-effective-middleware.md+137 added
1# How to build one effective middleware?23## Consitituent part45The middleware has two parts:67 - part one is what is executed once, when you initalize your middleware. That's where you set up all the global objects, logicals etc. Everything that happens one per application lifetime.89 - 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.1011```go12func funcName(params string) gin.HandlerFunc {13 // <---14 // This is part one15 // --->16 // The follow code is an example17 if err := check(params); err != nil {18 panic(err)19 }2021 return func(c *gin.Context) {22 // <---23 // This is part two24 // --->25 // The follow code is an example26 c.Set("TestVar", params)27 c.Next() 28 }29}30```3132## Execution process3334Firstly, we have the follow example code:3536```go37func main() {38 router := gin.Default()3940 router.Use(globalMiddleware())4142 router.GET("/rest/n/api/*some", mid1(), mid2(), handler)4344 router.Run()45}4647func globalMiddleware() gin.HandlerFunc {48 fmt.Println("globalMiddleware...1")4950 return func(c *gin.Context) {51 fmt.Println("globalMiddleware...2")52 c.Next()53 fmt.Println("globalMiddleware...3")54 }55}5657func handler(c *gin.Context) {58 fmt.Println("exec handler.")59}6061func mid1() gin.HandlerFunc {62 fmt.Println("mid1...1")6364 return func(c *gin.Context) {6566 fmt.Println("mid1...2")67 c.Next()68 fmt.Println("mid1...3")69 }70}7172func mid2() gin.HandlerFunc {73 fmt.Println("mid2...1")7475 return func(c *gin.Context) {76 fmt.Println("mid2...2")77 c.Next()78 fmt.Println("mid2...3")79 }80}81```8283According to [Consitituent part](#consitituent-part) said, when we run the gin process, **part one** will execute firstly and will print the follow information:8485```go86globalMiddleware...187mid1...188mid2...189```9091And init order are:9293```go94globalMiddleware...195 |96 v97mid1...198 |99 v100mid2...1101```102103When we curl one request `curl -v localhost:8080/rest/n/api/some`, **part two** will execute their middleware and output the following information:104105```go106globalMiddleware...2107mid1...2108mid2...2109exec handler.110mid2...3111mid1...3112globalMiddleware...3113```114115In other words, run order are:116117```go118globalMiddleware...2119 |120 v121mid1...2122 |123 v124mid2...2125 |126 v127exec handler.128 |129 v130mid2...3131 |132 v133mid1...3134 |135 v136globalMiddleware...3137```