routergroup.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package gin
  5. import (
  6. "net/http"
  7. "path"
  8. )
  9. // Used internally to configure router, a RouterGroup is associated with a prefix
  10. // and an array of handlers (middlewares)
  11. type RouterGroup struct {
  12. Handlers HandlersChain
  13. BasePath string
  14. engine *Engine
  15. }
  16. // Adds middlewares to the group, see example code in github.
  17. func (group *RouterGroup) Use(middlewares ...HandlerFunc) {
  18. group.Handlers = append(group.Handlers, middlewares...)
  19. }
  20. // Creates a new router group. You should add all the routes that have common middlwares or the same path prefix.
  21. // For example, all the routes that use a common middlware for authorization could be grouped.
  22. func (group *RouterGroup) Group(relativePath string, handlers ...HandlerFunc) *RouterGroup {
  23. return &RouterGroup{
  24. Handlers: group.combineHandlers(handlers),
  25. BasePath: group.calculateAbsolutePath(relativePath),
  26. engine: group.engine,
  27. }
  28. }
  29. // Handle registers a new request handle and middlewares with the given path and method.
  30. // The last handler should be the real handler, the other ones should be middlewares that can and should be shared among different routes.
  31. // See the example code in github.
  32. //
  33. // For GET, POST, PUT, PATCH and DELETE requests the respective shortcut
  34. // functions can be used.
  35. //
  36. // This function is intended for bulk loading and to allow the usage of less
  37. // frequently used, non-standardized or custom methods (e.g. for internal
  38. // communication with a proxy).
  39. func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers HandlersChain) {
  40. absolutePath := group.calculateAbsolutePath(relativePath)
  41. handlers = group.combineHandlers(handlers)
  42. debugPrintRoute(httpMethod, absolutePath, handlers)
  43. group.engine.handle(httpMethod, absolutePath, handlers)
  44. }
  45. // POST is a shortcut for router.Handle("POST", path, handle)
  46. func (group *RouterGroup) POST(relativePath string, handlers ...HandlerFunc) {
  47. group.Handle("POST", relativePath, handlers)
  48. }
  49. // GET is a shortcut for router.Handle("GET", path, handle)
  50. func (group *RouterGroup) GET(relativePath string, handlers ...HandlerFunc) {
  51. group.Handle("GET", relativePath, handlers)
  52. }
  53. // DELETE is a shortcut for router.Handle("DELETE", path, handle)
  54. func (group *RouterGroup) DELETE(relativePath string, handlers ...HandlerFunc) {
  55. group.Handle("DELETE", relativePath, handlers)
  56. }
  57. // PATCH is a shortcut for router.Handle("PATCH", path, handle)
  58. func (group *RouterGroup) PATCH(relativePath string, handlers ...HandlerFunc) {
  59. group.Handle("PATCH", relativePath, handlers)
  60. }
  61. // PUT is a shortcut for router.Handle("PUT", path, handle)
  62. func (group *RouterGroup) PUT(relativePath string, handlers ...HandlerFunc) {
  63. group.Handle("PUT", relativePath, handlers)
  64. }
  65. // OPTIONS is a shortcut for router.Handle("OPTIONS", path, handle)
  66. func (group *RouterGroup) OPTIONS(relativePath string, handlers ...HandlerFunc) {
  67. group.Handle("OPTIONS", relativePath, handlers)
  68. }
  69. // HEAD is a shortcut for router.Handle("HEAD", path, handle)
  70. func (group *RouterGroup) HEAD(relativePath string, handlers ...HandlerFunc) {
  71. group.Handle("HEAD", relativePath, handlers)
  72. }
  73. // LINK is a shortcut for router.Handle("LINK", path, handle)
  74. func (group *RouterGroup) LINK(relativePath string, handlers ...HandlerFunc) {
  75. group.Handle("LINK", relativePath, handlers)
  76. }
  77. // UNLINK is a shortcut for router.Handle("UNLINK", path, handle)
  78. func (group *RouterGroup) UNLINK(relativePath string, handlers ...HandlerFunc) {
  79. group.Handle("UNLINK", relativePath, handlers)
  80. }
  81. // Static serves files from the given file system root.
  82. // Internally a http.FileServer is used, therefore http.NotFound is used instead
  83. // of the Router's NotFound handler.
  84. // To use the operating system's file system implementation,
  85. // use :
  86. // router.Static("/static", "/var/www")
  87. func (group *RouterGroup) Static(relativePath, root string) {
  88. handler := group.createStaticHandler(relativePath, root)
  89. relativePath = path.Join(relativePath, "/*filepath")
  90. // Register GET and HEAD handlers
  91. group.GET(relativePath, handler)
  92. group.HEAD(relativePath, handler)
  93. }
  94. func (group *RouterGroup) createStaticHandler(relativePath, root string) func(*Context) {
  95. absolutePath := group.calculateAbsolutePath(relativePath)
  96. fileServer := http.StripPrefix(absolutePath, http.FileServer(http.Dir(root)))
  97. return func(c *Context) {
  98. fileServer.ServeHTTP(c.Writer, c.Request)
  99. }
  100. }
  101. func (group *RouterGroup) combineHandlers(handlers HandlersChain) HandlersChain {
  102. finalSize := len(group.Handlers) + len(handlers)
  103. mergedHandlers := make(HandlersChain, finalSize)
  104. copy(mergedHandlers, group.Handlers)
  105. copy(mergedHandlers[len(group.Handlers):], handlers)
  106. return mergedHandlers
  107. }
  108. func (group *RouterGroup) calculateAbsolutePath(relativePath string) string {
  109. return joinPaths(group.BasePath, relativePath)
  110. }