gins.go 5.5 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 ginS
  5. import (
  6. "html/template"
  7. "net/http"
  8. "sync"
  9. "github.com/gin-gonic/gin"
  10. )
  11. var once sync.Once
  12. var internalEngine *gin.Engine
  13. func engine() *gin.Engine {
  14. once.Do(func() {
  15. internalEngine = gin.Default()
  16. })
  17. return internalEngine
  18. }
  19. // LoadHTMLGlob is a wrapper for Engine.LoadHTMLGlob.
  20. func LoadHTMLGlob(pattern string) {
  21. engine().LoadHTMLGlob(pattern)
  22. }
  23. // LoadHTMLFiles is a wrapper for Engine.LoadHTMLFiles.
  24. func LoadHTMLFiles(files ...string) {
  25. engine().LoadHTMLFiles(files...)
  26. }
  27. // SetHTMLTemplate is a wrapper for Engine.SetHTMLTemplate.
  28. func SetHTMLTemplate(templ *template.Template) {
  29. engine().SetHTMLTemplate(templ)
  30. }
  31. // NoRoute adds handlers for NoRoute. It return a 404 code by default.
  32. func NoRoute(handlers ...gin.HandlerFunc) {
  33. engine().NoRoute(handlers...)
  34. }
  35. // NoMethod is a wrapper for Engine.NoMethod.
  36. func NoMethod(handlers ...gin.HandlerFunc) {
  37. engine().NoMethod(handlers...)
  38. }
  39. // Group creates a new router group. You should add all the routes that have common middlewares or the same path prefix.
  40. // For example, all the routes that use a common middleware for authorization could be grouped.
  41. func Group(relativePath string, handlers ...gin.HandlerFunc) *gin.RouterGroup {
  42. return engine().Group(relativePath, handlers...)
  43. }
  44. // Handle is a wrapper for Engine.Handle.
  45. func Handle(httpMethod, relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
  46. return engine().Handle(httpMethod, relativePath, handlers...)
  47. }
  48. // POST is a shortcut for router.Handle("POST", path, handle)
  49. func POST(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
  50. return engine().POST(relativePath, handlers...)
  51. }
  52. // GET is a shortcut for router.Handle("GET", path, handle)
  53. func GET(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
  54. return engine().GET(relativePath, handlers...)
  55. }
  56. // DELETE is a shortcut for router.Handle("DELETE", path, handle)
  57. func DELETE(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
  58. return engine().DELETE(relativePath, handlers...)
  59. }
  60. // PATCH is a shortcut for router.Handle("PATCH", path, handle)
  61. func PATCH(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
  62. return engine().PATCH(relativePath, handlers...)
  63. }
  64. // PUT is a shortcut for router.Handle("PUT", path, handle)
  65. func PUT(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
  66. return engine().PUT(relativePath, handlers...)
  67. }
  68. // OPTIONS is a shortcut for router.Handle("OPTIONS", path, handle)
  69. func OPTIONS(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
  70. return engine().OPTIONS(relativePath, handlers...)
  71. }
  72. // HEAD is a shortcut for router.Handle("HEAD", path, handle)
  73. func HEAD(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
  74. return engine().HEAD(relativePath, handlers...)
  75. }
  76. // Any is a wrapper for Engine.Any.
  77. func Any(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
  78. return engine().Any(relativePath, handlers...)
  79. }
  80. // StaticFile is a wrapper for Engine.StaticFile.
  81. func StaticFile(relativePath, filepath string) gin.IRoutes {
  82. return engine().StaticFile(relativePath, filepath)
  83. }
  84. // Static serves files from the given file system root.
  85. // Internally a http.FileServer is used, therefore http.NotFound is used instead
  86. // of the Router's NotFound handler.
  87. // To use the operating system's file system implementation,
  88. // use :
  89. // router.Static("/static", "/var/www")
  90. func Static(relativePath, root string) gin.IRoutes {
  91. return engine().Static(relativePath, root)
  92. }
  93. // StaticFS is a wrapper for Engine.StaticFS.
  94. func StaticFS(relativePath string, fs http.FileSystem) gin.IRoutes {
  95. return engine().StaticFS(relativePath, fs)
  96. }
  97. // Use attaches a global middleware to the router. ie. the middlewares attached though Use() will be
  98. // included in the handlers chain for every single request. Even 404, 405, static files...
  99. // For example, this is the right place for a logger or error management middleware.
  100. func Use(middlewares ...gin.HandlerFunc) gin.IRoutes {
  101. return engine().Use(middlewares...)
  102. }
  103. // Routes returns a slice of registered routes.
  104. func Routes() gin.RoutesInfo {
  105. return engine().Routes()
  106. }
  107. // Run attaches to a http.Server and starts listening and serving HTTP requests.
  108. // It is a shortcut for http.ListenAndServe(addr, router)
  109. // Note: this method will block the calling goroutine indefinitely unless an error happens.
  110. func Run(addr ...string) (err error) {
  111. return engine().Run(addr...)
  112. }
  113. // RunTLS attaches to a http.Server and starts listening and serving HTTPS requests.
  114. // It is a shortcut for http.ListenAndServeTLS(addr, certFile, keyFile, router)
  115. // Note: this method will block the calling goroutine indefinitely unless an error happens.
  116. func RunTLS(addr, certFile, keyFile string) (err error) {
  117. return engine().RunTLS(addr, certFile, keyFile)
  118. }
  119. // RunUnix attaches to a http.Server and starts listening and serving HTTP requests
  120. // through the specified unix socket (ie. a file)
  121. // Note: this method will block the calling goroutine indefinitely unless an error happens.
  122. func RunUnix(file string) (err error) {
  123. return engine().RunUnix(file)
  124. }
  125. // RunFd attaches the router to a http.Server and starts listening and serving HTTP requests
  126. // through the specified file descriptor.
  127. // Note: the method will block the calling goroutine indefinitely unless on error happens.
  128. func RunFd(fd int) (err error) {
  129. return engine().RunFd(fd)
  130. }