group.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package echo
  2. import (
  3. "path"
  4. )
  5. type (
  6. // Group is a set of sub-routes for a specified route. It can be used for inner
  7. // routes that share a common middleware or functionality that should be separate
  8. // from the parent echo instance while still inheriting from it.
  9. Group struct {
  10. prefix string
  11. middleware []MiddlewareFunc
  12. echo *Echo
  13. }
  14. )
  15. // Use implements `Echo#Use()` for sub-routes within the Group.
  16. func (g *Group) Use(middleware ...MiddlewareFunc) {
  17. g.middleware = append(g.middleware, middleware...)
  18. // Allow all requests to reach the group as they might get dropped if router
  19. // doesn't find a match, making none of the group middleware process.
  20. for _, p := range []string{"", "/*"} {
  21. g.echo.Any(path.Clean(g.prefix+p), func(c Context) error {
  22. return NotFoundHandler(c)
  23. }, g.middleware...)
  24. }
  25. }
  26. // CONNECT implements `Echo#CONNECT()` for sub-routes within the Group.
  27. func (g *Group) CONNECT(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
  28. return g.Add(CONNECT, path, h, m...)
  29. }
  30. // DELETE implements `Echo#DELETE()` for sub-routes within the Group.
  31. func (g *Group) DELETE(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
  32. return g.Add(DELETE, path, h, m...)
  33. }
  34. // GET implements `Echo#GET()` for sub-routes within the Group.
  35. func (g *Group) GET(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
  36. return g.Add(GET, path, h, m...)
  37. }
  38. // HEAD implements `Echo#HEAD()` for sub-routes within the Group.
  39. func (g *Group) HEAD(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
  40. return g.Add(HEAD, path, h, m...)
  41. }
  42. // OPTIONS implements `Echo#OPTIONS()` for sub-routes within the Group.
  43. func (g *Group) OPTIONS(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
  44. return g.Add(OPTIONS, path, h, m...)
  45. }
  46. // PATCH implements `Echo#PATCH()` for sub-routes within the Group.
  47. func (g *Group) PATCH(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
  48. return g.Add(PATCH, path, h, m...)
  49. }
  50. // POST implements `Echo#POST()` for sub-routes within the Group.
  51. func (g *Group) POST(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
  52. return g.Add(POST, path, h, m...)
  53. }
  54. // PUT implements `Echo#PUT()` for sub-routes within the Group.
  55. func (g *Group) PUT(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
  56. return g.Add(PUT, path, h, m...)
  57. }
  58. // TRACE implements `Echo#TRACE()` for sub-routes within the Group.
  59. func (g *Group) TRACE(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
  60. return g.Add(TRACE, path, h, m...)
  61. }
  62. // Any implements `Echo#Any()` for sub-routes within the Group.
  63. func (g *Group) Any(path string, handler HandlerFunc, middleware ...MiddlewareFunc) []*Route {
  64. routes := make([]*Route, len(methods))
  65. for i, m := range methods {
  66. routes[i] = g.Add(m, path, handler, middleware...)
  67. }
  68. return routes
  69. }
  70. // Match implements `Echo#Match()` for sub-routes within the Group.
  71. func (g *Group) Match(methods []string, path string, handler HandlerFunc, middleware ...MiddlewareFunc) []*Route {
  72. routes := make([]*Route, len(methods))
  73. for i, m := range methods {
  74. routes[i] = g.Add(m, path, handler, middleware...)
  75. }
  76. return routes
  77. }
  78. // Group creates a new sub-group with prefix and optional sub-group-level middleware.
  79. func (g *Group) Group(prefix string, middleware ...MiddlewareFunc) *Group {
  80. m := make([]MiddlewareFunc, 0, len(g.middleware)+len(middleware))
  81. m = append(m, g.middleware...)
  82. m = append(m, middleware...)
  83. return g.echo.Group(g.prefix+prefix, m...)
  84. }
  85. // Static implements `Echo#Static()` for sub-routes within the Group.
  86. func (g *Group) Static(prefix, root string) {
  87. static(g, prefix, root)
  88. }
  89. // File implements `Echo#File()` for sub-routes within the Group.
  90. func (g *Group) File(path, file string) {
  91. g.echo.File(g.prefix+path, file)
  92. }
  93. // Add implements `Echo#Add()` for sub-routes within the Group.
  94. func (g *Group) Add(method, path string, handler HandlerFunc, middleware ...MiddlewareFunc) *Route {
  95. // Combine into a new slice to avoid accidentally passing the same slice for
  96. // multiple routes, which would lead to later add() calls overwriting the
  97. // middleware from earlier calls.
  98. m := make([]MiddlewareFunc, 0, len(g.middleware)+len(middleware))
  99. m = append(m, g.middleware...)
  100. m = append(m, middleware...)
  101. return g.echo.Add(method, g.prefix+path, handler, m...)
  102. }