register.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. // Register defines all router handle interface generate by RouteChain().
  6. type Register interface {
  7. All(handler any, handlers ...any) Register
  8. Get(handler any, handlers ...any) Register
  9. Head(handler any, handlers ...any) Register
  10. Post(handler any, handlers ...any) Register
  11. Put(handler any, handlers ...any) Register
  12. Delete(handler any, handlers ...any) Register
  13. Connect(handler any, handlers ...any) Register
  14. Options(handler any, handlers ...any) Register
  15. Trace(handler any, handlers ...any) Register
  16. Patch(handler any, handlers ...any) Register
  17. Add(methods []string, handler any, handlers ...any) Register
  18. RouteChain(path string) Register
  19. }
  20. var _ Register = (*Registering)(nil)
  21. // Registering provides route registration helpers for a specific path on the
  22. // application instance.
  23. type Registering struct {
  24. app *App
  25. group *Group
  26. path string
  27. }
  28. // All registers a middleware route that will match requests
  29. // with the provided path which is stored in register struct.
  30. //
  31. // app.RouteChain("/").All(func(c fiber.Ctx) error {
  32. // return c.Next()
  33. // })
  34. // app.RouteChain("/api").All(func(c fiber.Ctx) error {
  35. // return c.Next()
  36. // })
  37. // app.RouteChain("/api").All(handler, func(c fiber.Ctx) error {
  38. // return c.Next()
  39. // })
  40. //
  41. // This method will match all HTTP verbs: GET, POST, PUT, HEAD etc...
  42. func (r *Registering) All(handler any, handlers ...any) Register {
  43. converted := collectHandlers("register", append([]any{handler}, handlers...)...)
  44. r.app.register([]string{methodUse}, r.path, r.group, converted...)
  45. return r
  46. }
  47. // Get registers a route for GET methods that requests a representation
  48. // of the specified resource. Requests using GET should only retrieve data.
  49. func (r *Registering) Get(handler any, handlers ...any) Register {
  50. return r.Add([]string{MethodGet}, handler, handlers...)
  51. }
  52. // Head registers a route for HEAD methods that asks for a response identical
  53. // to that of a GET request, but without the response body.
  54. func (r *Registering) Head(handler any, handlers ...any) Register {
  55. return r.Add([]string{MethodHead}, handler, handlers...)
  56. }
  57. // Post registers a route for POST methods that is used to submit an entity to the
  58. // specified resource, often causing a change in state or side effects on the server.
  59. func (r *Registering) Post(handler any, handlers ...any) Register {
  60. return r.Add([]string{MethodPost}, handler, handlers...)
  61. }
  62. // Put registers a route for PUT methods that replaces all current representations
  63. // of the target resource with the request payload.
  64. func (r *Registering) Put(handler any, handlers ...any) Register {
  65. return r.Add([]string{MethodPut}, handler, handlers...)
  66. }
  67. // Delete registers a route for DELETE methods that deletes the specified resource.
  68. func (r *Registering) Delete(handler any, handlers ...any) Register {
  69. return r.Add([]string{MethodDelete}, handler, handlers...)
  70. }
  71. // Connect registers a route for CONNECT methods that establishes a tunnel to the
  72. // server identified by the target resource.
  73. func (r *Registering) Connect(handler any, handlers ...any) Register {
  74. return r.Add([]string{MethodConnect}, handler, handlers...)
  75. }
  76. // Options registers a route for OPTIONS methods that is used to describe the
  77. // communication options for the target resource.
  78. func (r *Registering) Options(handler any, handlers ...any) Register {
  79. return r.Add([]string{MethodOptions}, handler, handlers...)
  80. }
  81. // Trace registers a route for TRACE methods that performs a message loop-back
  82. // test along the r.Path to the target resource.
  83. func (r *Registering) Trace(handler any, handlers ...any) Register {
  84. return r.Add([]string{MethodTrace}, handler, handlers...)
  85. }
  86. // Patch registers a route for PATCH methods that is used to apply partial
  87. // modifications to a resource.
  88. func (r *Registering) Patch(handler any, handlers ...any) Register {
  89. return r.Add([]string{MethodPatch}, handler, handlers...)
  90. }
  91. // Add allows you to specify multiple HTTP methods to register a route.
  92. // The provided handlers are executed in order, starting with `handler` and then the variadic `handlers`.
  93. func (r *Registering) Add(methods []string, handler any, handlers ...any) Register {
  94. converted := collectHandlers("register", append([]any{handler}, handlers...)...)
  95. r.app.register(methods, r.path, r.group, converted...)
  96. return r
  97. }
  98. // RouteChain returns a new Register instance whose route path takes
  99. // the path in the current instance as its prefix.
  100. func (r *Registering) RouteChain(path string) Register {
  101. // Create new group
  102. route := &Registering{app: r.app, group: r.group, path: getGroupPath(r.path, path)}
  103. return route
  104. }