gins.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. func LoadHTMLGlob(pattern string) {
  20. engine().LoadHTMLGlob(pattern)
  21. }
  22. func LoadHTMLFiles(files ...string) {
  23. engine().LoadHTMLFiles(files...)
  24. }
  25. func SetHTMLTemplate(templ *template.Template) {
  26. engine().SetHTMLTemplate(templ)
  27. }
  28. // NoRoute adds handlers for NoRoute. It return a 404 code by default.
  29. func NoRoute(handlers ...gin.HandlerFunc) {
  30. engine().NoRoute(handlers...)
  31. }
  32. // NoMethod sets the handlers called when... TODO
  33. func NoMethod(handlers ...gin.HandlerFunc) {
  34. engine().NoMethod(handlers...)
  35. }
  36. // Group creates a new router group. You should add all the routes that have common middlwares or the same path prefix.
  37. // For example, all the routes that use a common middlware for authorization could be grouped.
  38. func Group(relativePath string, handlers ...gin.HandlerFunc) *gin.RouterGroup {
  39. return engine().Group(relativePath, handlers...)
  40. }
  41. func Handle(httpMethod, relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
  42. return engine().Handle(httpMethod, relativePath, handlers...)
  43. }
  44. // POST is a shortcut for router.Handle("POST", path, handle)
  45. func POST(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
  46. return engine().POST(relativePath, handlers...)
  47. }
  48. // GET is a shortcut for router.Handle("GET", path, handle)
  49. func GET(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
  50. return engine().GET(relativePath, handlers...)
  51. }
  52. // DELETE is a shortcut for router.Handle("DELETE", path, handle)
  53. func DELETE(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
  54. return engine().DELETE(relativePath, handlers...)
  55. }
  56. // PATCH is a shortcut for router.Handle("PATCH", path, handle)
  57. func PATCH(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
  58. return engine().PATCH(relativePath, handlers...)
  59. }
  60. // PUT is a shortcut for router.Handle("PUT", path, handle)
  61. func PUT(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
  62. return engine().PUT(relativePath, handlers...)
  63. }
  64. // OPTIONS is a shortcut for router.Handle("OPTIONS", path, handle)
  65. func OPTIONS(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
  66. return engine().OPTIONS(relativePath, handlers...)
  67. }
  68. // HEAD is a shortcut for router.Handle("HEAD", path, handle)
  69. func HEAD(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
  70. return engine().HEAD(relativePath, handlers...)
  71. }
  72. func Any(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
  73. return engine().Any(relativePath, handlers...)
  74. }
  75. func StaticFile(relativePath, filepath string) gin.IRoutes {
  76. return engine().StaticFile(relativePath, filepath)
  77. }
  78. // Static serves files from the given file system root.
  79. // Internally a http.FileServer is used, therefore http.NotFound is used instead
  80. // of the Router's NotFound handler.
  81. // To use the operating system's file system implementation,
  82. // use :
  83. // router.Static("/static", "/var/www")
  84. func Static(relativePath, root string) gin.IRoutes {
  85. return engine().Static(relativePath, root)
  86. }
  87. func StaticFS(relativePath string, fs http.FileSystem) gin.IRoutes {
  88. return engine().StaticFS(relativePath, fs)
  89. }
  90. // Use attachs a global middleware to the router. ie. the middlewares attached though Use() will be
  91. // included in the handlers chain for every single request. Even 404, 405, static files...
  92. // For example, this is the right place for a logger or error management middleware.
  93. func Use(middlewares ...gin.HandlerFunc) gin.IRoutes {
  94. return engine().Use(middlewares...)
  95. }
  96. // Run : The router is attached to a http.Server and starts listening and serving HTTP requests.
  97. // It is a shortcut for http.ListenAndServe(addr, router)
  98. // Note: this method will block the calling goroutine undefinitelly unless an error happens.
  99. func Run(addr ...string) (err error) {
  100. return engine().Run(addr...)
  101. }
  102. // RunTLS : The router is attached to a http.Server and starts listening and serving HTTPS requests.
  103. // It is a shortcut for http.ListenAndServeTLS(addr, certFile, keyFile, router)
  104. // Note: this method will block the calling goroutine undefinitelly unless an error happens.
  105. func RunTLS(addr string, certFile string, keyFile string) (err error) {
  106. return engine().RunTLS(addr, certFile, keyFile)
  107. }
  108. // RunUnix : The router is attached to a http.Server and starts listening and serving HTTP requests
  109. // through the specified unix socket (ie. a file)
  110. // Note: this method will block the calling goroutine undefinitelly unless an error happens.
  111. func RunUnix(file string) (err error) {
  112. return engine().RunUnix(file)
  113. }