router.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package api
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "net/http"
  5. )
  6. // Handler 请求处理函数
  7. type Handler func(c *Context)
  8. // Router 路由接口
  9. type Router interface {
  10. // AddMiddlewares 添加中间件
  11. AddMiddlewares(middlewares ...Handler) Router
  12. // RegisterVersionedRouter 注册版本路由
  13. RegisterVersionedRouter(version string, middlewares ...Handler) Router
  14. // VersionedRouter 获取版本路由
  15. VersionedRouter(version string) Router
  16. // AddRoute 添加路由路径
  17. AddRoute(method string, relativePath string, handlers ...Handler) Router
  18. // Static 静态路由(指向目录)
  19. Static(relativePath string, root string)
  20. // StaticFile 静态路由(指向文件)
  21. StaticFile(relativePath string, filepath string)
  22. // BasePath 路由的基础路径,即去掉http://ip:port之后的URL部分(不包括AddRoute的RelativePath)
  23. BasePath() string
  24. }
  25. type RootRouter struct {
  26. engine *gin.Engine
  27. versioned map[string]*PrefixRouter
  28. }
  29. func newRootRouter(engine *gin.Engine) *RootRouter {
  30. return &RootRouter{
  31. engine: engine,
  32. versioned: make(map[string]*PrefixRouter),
  33. }
  34. }
  35. func (r *RootRouter) AddMiddlewares(middlewares ...Handler) Router {
  36. r.engine.Use(transferHandlers(middlewares...)...)
  37. return r
  38. }
  39. func (r *RootRouter) RegisterVersionedRouter(version string, middlewares ...Handler) Router {
  40. versioned := r.engine.Group(version, transferHandlers(middlewares...)...)
  41. r.versioned[version] = newPrefixRouter(versioned)
  42. return r.versioned[version]
  43. }
  44. func (r *RootRouter) Static(relativePath string, root string) {
  45. r.engine.Static(relativePath, root)
  46. }
  47. func (r *RootRouter) StaticFile(relativePath string, filepath string) {
  48. r.engine.StaticFile(relativePath, filepath)
  49. }
  50. func (r *RootRouter) BasePath() string {
  51. return r.engine.BasePath()
  52. }
  53. func (r *RootRouter) VersionedRouter(version string) Router {
  54. return r.versioned[version]
  55. }
  56. func (r *RootRouter) AddRoute(method string, relativePath string, handlers ...Handler) Router {
  57. r.engine.Handle(method, relativePath, transferHandlers(handlers...)...)
  58. return r
  59. }
  60. func (r *RootRouter) ServerHttp(w http.ResponseWriter, req *http.Request) {
  61. r.engine.ServeHTTP(w, req)
  62. }
  63. type PrefixRouter struct {
  64. groupRouter *gin.RouterGroup
  65. versioned map[string]*PrefixRouter
  66. }
  67. func newPrefixRouter(group *gin.RouterGroup) *PrefixRouter {
  68. return &PrefixRouter{
  69. groupRouter: group,
  70. versioned: make(map[string]*PrefixRouter),
  71. }
  72. }
  73. func (r *PrefixRouter) AddMiddlewares(middlewares ...Handler) Router {
  74. r.groupRouter.Use(transferHandlers(middlewares...)...)
  75. return r
  76. }
  77. func (r *PrefixRouter) RegisterVersionedRouter(version string, middlewares ...Handler) Router {
  78. ginMiddlewares := make([]gin.HandlerFunc, 0)
  79. for _, m := range middlewares {
  80. innerM := m
  81. ginMiddlewares = append(ginMiddlewares, func(c *gin.Context) {
  82. innerM(&Context{Context: c})
  83. })
  84. }
  85. versioned := r.groupRouter.Group(version, ginMiddlewares...)
  86. r.versioned[version] = newPrefixRouter(versioned)
  87. return r.versioned[version]
  88. }
  89. func (r *PrefixRouter) VersionedRouter(version string) Router {
  90. return r.versioned[version]
  91. }
  92. func (r *PrefixRouter) AddRoute(method string, relativePath string, handlers ...Handler) Router {
  93. r.groupRouter.Handle(method, relativePath, transferHandlers(handlers...)...)
  94. return r
  95. }
  96. func (r *PrefixRouter) Static(relativePath string, root string) {
  97. r.groupRouter.Static(relativePath, root)
  98. }
  99. func (r *PrefixRouter) StaticFile(relativePath string, filepath string) {
  100. r.groupRouter.StaticFile(relativePath, filepath)
  101. }
  102. func (r *PrefixRouter) BasePath() string {
  103. return r.groupRouter.BasePath()
  104. }
  105. func transferHandlers(handlers ...Handler) []gin.HandlerFunc {
  106. ginHandlers := make([]gin.HandlerFunc, 0)
  107. for _, handler := range handlers {
  108. innerHandler := handler
  109. ginHandlers = append(ginHandlers, func(c *gin.Context) {
  110. innerHandler(&Context{Context: c})
  111. })
  112. }
  113. return ginHandlers
  114. }