group.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
  2. // 🤖 Github Repository: https://github.com/gofiber/fiber
  3. // 📌 API Documentation: https://docs.gofiber.io
  4. package fiber
  5. import (
  6. "fmt"
  7. "reflect"
  8. )
  9. // Group struct
  10. type Group struct {
  11. app *App
  12. prefix string
  13. }
  14. // Use registers a middleware route.
  15. // Middleware matches requests beginning with the provided prefix.
  16. // Providing a prefix is optional, it defaults to "/".
  17. //
  18. // - group.Use(handler)
  19. // - group.Use("/api", handler)
  20. // - group.Use("/api", handler, handler)
  21. func (grp *Group) Use(args ...interface{}) Router {
  22. var path = ""
  23. var handlers []Handler
  24. for i := 0; i < len(args); i++ {
  25. switch arg := args[i].(type) {
  26. case string:
  27. path = arg
  28. case Handler:
  29. handlers = append(handlers, arg)
  30. default:
  31. panic(fmt.Sprintf("use: invalid handler %v\n", reflect.TypeOf(arg)))
  32. }
  33. }
  34. grp.app.register(methodUse, getGroupPath(grp.prefix, path), handlers...)
  35. return grp
  36. }
  37. // Get registers a route for GET methods that requests a representation
  38. // of the specified resource. Requests using GET should only retrieve data.
  39. func (grp *Group) Get(path string, handlers ...Handler) Router {
  40. route := grp.app.register(MethodGet, getGroupPath(grp.prefix, path), handlers...)
  41. // Add head route
  42. headRoute := route
  43. grp.app.addRoute(MethodHead, &headRoute)
  44. return grp
  45. }
  46. // Head registers a route for HEAD methods that asks for a response identical
  47. // to that of a GET request, but without the response body.
  48. func (grp *Group) Head(path string, handlers ...Handler) Router {
  49. return grp.Add(MethodHead, path, handlers...)
  50. }
  51. // Post registers a route for POST methods that is used to submit an entity to the
  52. // specified resource, often causing a change in state or side effects on the server.
  53. func (grp *Group) Post(path string, handlers ...Handler) Router {
  54. return grp.Add(MethodPost, path, handlers...)
  55. }
  56. // Put registers a route for PUT methods that replaces all current representations
  57. // of the target resource with the request payload.
  58. func (grp *Group) Put(path string, handlers ...Handler) Router {
  59. return grp.Add(MethodPut, path, handlers...)
  60. }
  61. // Delete registers a route for DELETE methods that deletes the specified resource.
  62. func (grp *Group) Delete(path string, handlers ...Handler) Router {
  63. return grp.Add(MethodDelete, path, handlers...)
  64. }
  65. // Connect registers a route for CONNECT methods that establishes a tunnel to the
  66. // server identified by the target resource.
  67. func (grp *Group) Connect(path string, handlers ...Handler) Router {
  68. return grp.Add(MethodConnect, path, handlers...)
  69. }
  70. // Options registers a route for OPTIONS methods that is used to describe the
  71. // communication options for the target resource.
  72. func (grp *Group) Options(path string, handlers ...Handler) Router {
  73. return grp.Add(MethodOptions, path, handlers...)
  74. }
  75. // Trace registers a route for TRACE methods that performs a message loop-back
  76. // test along the path to the target resource.
  77. func (grp *Group) Trace(path string, handlers ...Handler) Router {
  78. return grp.Add(MethodTrace, path, handlers...)
  79. }
  80. // Patch registers a route for PATCH methods that is used to apply partial
  81. // modifications to a resource.
  82. func (grp *Group) Patch(path string, handlers ...Handler) Router {
  83. return grp.Add(MethodPatch, path, handlers...)
  84. }
  85. // Add ...
  86. func (grp *Group) Add(method, path string, handlers ...Handler) Router {
  87. grp.app.register(method, getGroupPath(grp.prefix, path), handlers...)
  88. return grp
  89. }
  90. // Static ...
  91. func (grp *Group) Static(prefix, root string, config ...Static) Router {
  92. grp.app.registerStatic(getGroupPath(grp.prefix, prefix), root, config...)
  93. return grp
  94. }
  95. // All ...
  96. func (grp *Group) All(path string, handlers ...Handler) Router {
  97. for _, method := range intMethod {
  98. grp.Add(method, path, handlers...)
  99. }
  100. return grp
  101. }
  102. // Group is used for Routes with common prefix to define a new sub-router with optional middleware.
  103. func (grp *Group) Group(prefix string, handlers ...Handler) Router {
  104. prefix = getGroupPath(grp.prefix, prefix)
  105. if len(handlers) > 0 {
  106. grp.app.register(methodUse, prefix, handlers...)
  107. }
  108. return grp.app.Group(prefix)
  109. }