routergroup.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. "strings"
  9. )
  10. // Used internally to configure router, a RouterGroup is associated with a prefix
  11. // and an array of handlers (middlewares)
  12. type RouterGroup struct {
  13. Handlers HandlersChain
  14. BasePath string
  15. engine *Engine
  16. }
  17. // Adds middlewares to the group, see example code in github.
  18. func (group *RouterGroup) Use(middlewares ...HandlerFunc) {
  19. group.Handlers = append(group.Handlers, middlewares...)
  20. }
  21. // Creates a new router group. You should add all the routes that have common middlwares or the same path prefix.
  22. // For example, all the routes that use a common middlware for authorization could be grouped.
  23. func (group *RouterGroup) Group(relativePath string, handlers ...HandlerFunc) *RouterGroup {
  24. return &RouterGroup{
  25. Handlers: group.combineHandlers(handlers),
  26. BasePath: group.calculateAbsolutePath(relativePath),
  27. engine: group.engine,
  28. }
  29. }
  30. // Handle registers a new request handle and middlewares with the given path and method.
  31. // The last handler should be the real handler, the other ones should be middlewares that can and should be shared among different routes.
  32. // See the example code in github.
  33. //
  34. // For GET, POST, PUT, PATCH and DELETE requests the respective shortcut
  35. // functions can be used.
  36. //
  37. // This function is intended for bulk loading and to allow the usage of less
  38. // frequently used, non-standardized or custom methods (e.g. for internal
  39. // communication with a proxy).
  40. func (group *RouterGroup) handle(httpMethod, relativePath string, handlers HandlersChain) {
  41. absolutePath := group.calculateAbsolutePath(relativePath)
  42. handlers = group.combineHandlers(handlers)
  43. group.engine.addRoute(httpMethod, absolutePath, handlers)
  44. }
  45. func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers ...HandlerFunc) {
  46. group.handle(httpMethod, relativePath, handlers)
  47. }
  48. // POST is a shortcut for router.Handle("POST", path, handle)
  49. func (group *RouterGroup) POST(relativePath string, handlers ...HandlerFunc) {
  50. group.handle("POST", relativePath, handlers)
  51. }
  52. // GET is a shortcut for router.Handle("GET", path, handle)
  53. func (group *RouterGroup) GET(relativePath string, handlers ...HandlerFunc) {
  54. group.handle("GET", relativePath, handlers)
  55. }
  56. // DELETE is a shortcut for router.Handle("DELETE", path, handle)
  57. func (group *RouterGroup) DELETE(relativePath string, handlers ...HandlerFunc) {
  58. group.handle("DELETE", relativePath, handlers)
  59. }
  60. // PATCH is a shortcut for router.Handle("PATCH", path, handle)
  61. func (group *RouterGroup) PATCH(relativePath string, handlers ...HandlerFunc) {
  62. group.handle("PATCH", relativePath, handlers)
  63. }
  64. // PUT is a shortcut for router.Handle("PUT", path, handle)
  65. func (group *RouterGroup) PUT(relativePath string, handlers ...HandlerFunc) {
  66. group.handle("PUT", relativePath, handlers)
  67. }
  68. // OPTIONS is a shortcut for router.Handle("OPTIONS", path, handle)
  69. func (group *RouterGroup) OPTIONS(relativePath string, handlers ...HandlerFunc) {
  70. group.handle("OPTIONS", relativePath, handlers)
  71. }
  72. // HEAD is a shortcut for router.Handle("HEAD", path, handle)
  73. func (group *RouterGroup) HEAD(relativePath string, handlers ...HandlerFunc) {
  74. group.handle("HEAD", relativePath, handlers)
  75. }
  76. func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc) {
  77. // GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE
  78. group.handle("GET", relativePath, handlers)
  79. group.handle("POST", relativePath, handlers)
  80. group.handle("PUT", relativePath, handlers)
  81. group.handle("PATCH", relativePath, handlers)
  82. group.handle("HEAD", relativePath, handlers)
  83. group.handle("OPTIONS", relativePath, handlers)
  84. group.handle("DELETE", relativePath, handlers)
  85. group.handle("CONNECT", relativePath, handlers)
  86. group.handle("TRACE", relativePath, handlers)
  87. }
  88. func (group *RouterGroup) StaticFile(relativePath, filepath string) {
  89. if strings.Contains(relativePath, ":") || strings.Contains(relativePath, "*") {
  90. panic("URL parameters can not be used when serving a static file")
  91. }
  92. handler := func(c *Context) {
  93. c.File(filepath)
  94. }
  95. group.GET(relativePath, handler)
  96. group.HEAD(relativePath, handler)
  97. }
  98. // Static serves files from the given file system root.
  99. // Internally a http.FileServer is used, therefore http.NotFound is used instead
  100. // of the Router's NotFound handler.
  101. // To use the operating system's file system implementation,
  102. // use :
  103. // router.Static("/static", "/var/www")
  104. func (group *RouterGroup) Static(relativePath, root string) {
  105. group.StaticFS(relativePath, http.Dir(root), false)
  106. }
  107. func (group *RouterGroup) StaticFS(relativePath string, fs http.FileSystem, listDirectory bool) {
  108. if strings.Contains(relativePath, ":") || strings.Contains(relativePath, "*") {
  109. panic("URL parameters can not be used when serving a static folder")
  110. }
  111. handler := group.createStaticHandler(relativePath, fs, listDirectory)
  112. relativePath = path.Join(relativePath, "/*filepath")
  113. // Register GET and HEAD handlers
  114. group.GET(relativePath, handler)
  115. group.HEAD(relativePath, handler)
  116. }
  117. func (group *RouterGroup) createStaticHandler(relativePath string, fs http.FileSystem, listDirectory bool) HandlerFunc {
  118. absolutePath := group.calculateAbsolutePath(relativePath)
  119. fileServer := http.StripPrefix(absolutePath, http.FileServer(fs))
  120. return func(c *Context) {
  121. if !listDirectory && lastChar(c.Request.URL.Path) == '/' {
  122. http.NotFound(c.Writer, c.Request)
  123. return
  124. }
  125. fileServer.ServeHTTP(c.Writer, c.Request)
  126. }
  127. }
  128. func (group *RouterGroup) combineHandlers(handlers HandlersChain) HandlersChain {
  129. finalSize := len(group.Handlers) + len(handlers)
  130. mergedHandlers := make(HandlersChain, finalSize)
  131. copy(mergedHandlers, group.Handlers)
  132. copy(mergedHandlers[len(group.Handlers):], handlers)
  133. return mergedHandlers
  134. }
  135. func (group *RouterGroup) calculateAbsolutePath(relativePath string) string {
  136. return joinPaths(group.BasePath, relativePath)
  137. }