当前位置:   article > 正文

Golang gin 框架中间件(middleware)实现原理详解_gin middleware

gin middleware

怎么理解中间件(middleware)?

做开发的特别是做后端开发的相信对中间件这个词肯定是很熟悉了,中间件的概念还是比较宽泛的,即使在后端开发领域,也有很多不同的含义。概况起来中间件可以理解为用于解耦业务和非业务代码的钩子函数或者程序。在web框架层面,程序运行到某个阶段自动执行预设的函数,运行完后再回到跳出的那个阶段继续执行原函数。

具体到 gin 框架,官方的说法是传入的HTTP请求可以由中间件链和最终操作来处理。可以理解为中间件是一种过滤路由的机制,也就是http请求来到时先经过中间件,再到具体的处理函数,传入的 HTTP 请求可以由一系列中间件和最终操作来处理,可以在中间件中实现前置和后置处理逻辑。

gin 中间件实现方法分析

1、gin 把中间件和主体函数统一定义为 handleFunc

  1. // HandlerFunc defines the handler used by gin middleware as return value.
  2. type HandlerFunc func(*Context)
  3. // Use attaches a global middleware to the router. i.e. the middleware attached through Use() will be
  4. // included in the handlers chain for every single request. Even 404, 405, static files...
  5. // For example, this is the right place for a logger or error management middleware.
  6. func (engine *Engine) Use(middleware ...HandlerFunc) IRoutes {
  7. engine.RouterGroup.Use(middleware...)
  8. engine.rebuild404Handlers()
  9. engine.rebuild405Handlers()
  10. return engine
  11. }
  12. // Handle registers a new request handle and middleware with the given path and method.
  13. // The last handler should be the real handler, the other ones should be middleware that can and should be shared among different routes.
  14. // See the example code in GitHub.
  15. //
  16. // For GET, POST, PUT, PATCH and DELETE requests the respective shortcut
  17. // functions can be used.
  18. //
  19. // This function is intended for bulk loading and to allow the usage of less
  20. // frequently used, non-standardized or custom methods (e.g. for internal
  21. // communication with a proxy).
  22. func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers ...HandlerFunc) IRoutes {
  23. if matched := regEnLetter.MatchString(httpMethod); !matched {
  24. panic("http method " + httpMethod + " is not valid")
  25. }
  26. return group.handle(httpMethod, relativePath, handlers)
  27. }

无论是是用 use 方法注册中间件,还是用 handle 方法注册主体函数,类型都是 HandlerFunc

2、把所有 handleFunc 放入入一个元素类型为 handleChain 的数组

  1. // HandlerFunc defines the handler used by gin middleware as return value.
  2. type HandlerFunc func(*Context)
  3. // HandlersChain defines a HandlerFunc slice.
  4. type HandlersChain []HandlerFunc

3、从 handleChain 的第一个元素开始执行,中间使用 Next、Abort 等函数来进行流程控制

  1. // Next should be used only inside middleware.
  2. // It executes the pending handlers in the chain inside the calling handler.
  3. // See example in GitHub.
  4. func (c *Context) Next() {
  5. c.index++
  6. for c.index < int8(len(c.handlers)) {
  7. c.handlers[c.index](c)
  8. c.index++
  9. }
  10. }
  11. // Abort prevents pending handlers from being called. Note that this will not stop the current handler.
  12. // Let's say you have an authorization middleware that validates that the current request is authorized.
  13. // If the authorization fails (ex: the password does not match), call Abort to ensure the remaining handlers
  14. // for this request are not called.
  15. func (c *Context) Abort() {
  16. c.index = abortIndex
  17. }

手动实现中间件逻辑程序

参考 gin 实现中间件逻辑的思路,手动实现一个简单的演示程序,如下

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. const maxIndex = 63
  6. type HandlerFunc func(ctx *context)
  7. type context struct {
  8. HandlersChain []HandlerFunc
  9. index int8
  10. }
  11. func (ctx *context) next() {
  12. if ctx.index < maxIndex {
  13. ctx.index++
  14. ctx.HandlersChain[ctx.index](ctx)
  15. }
  16. }
  17. func (ctx *context) abort() {
  18. ctx.index = maxIndex
  19. fmt.Println("abort...")
  20. }
  21. func (ctx *context) use(f HandlerFunc) {
  22. ctx.HandlersChain = append(ctx.HandlersChain, f)
  23. }
  24. func (ctx *context) get(relativePath string, f HandlerFunc) {
  25. ctx.HandlersChain = append(ctx.HandlersChain, f)
  26. }
  27. func (ctx *context) run() {
  28. ctx.HandlersChain[0](ctx)
  29. }
  30. func main() {
  31. ctx := &context{}
  32. ctx.use(middleware1)
  33. ctx.use(middleware2)
  34. ctx.get("hahahah", logicFunc)
  35. ctx.run()
  36. }
  37. func middleware1(ctx *context) {
  38. fmt.Println("middleware1 begin")
  39. //ctx.abort()
  40. ctx.next()
  41. fmt.Println("middleware1 end")
  42. }
  43. func middleware2(ctx *context) {
  44. fmt.Println("middleware2 begin")
  45. ctx.next()
  46. fmt.Println("middleware2 end")
  47. }
  48. func logicFunc(ctx *context) {
  49. fmt.Println("logicFunc function")
  50. }

运行结果为

  1. middleware1 begin
  2. middleware2 begin
  3. logicFunc function
  4. middleware2 end
  5. middleware1 end

相信这个简化版的中间件实现肯定可以帮助你理解其中的原理。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/626980
推荐阅读
相关标签
  

闽ICP备14008679号