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