2022-05-28 02:42:28 +00:00
|
|
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
2015-08-16 16:44:18 +00:00
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package ginS
|
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
|
2017-08-25 01:13:53 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2015-08-16 16:44:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var once sync.Once
|
2017-08-25 01:13:53 +00:00
|
|
|
var internalEngine *gin.Engine
|
2015-08-16 16:44:18 +00:00
|
|
|
|
2017-08-25 01:13:53 +00:00
|
|
|
func engine() *gin.Engine {
|
2015-08-16 16:44:18 +00:00
|
|
|
once.Do(func() {
|
2017-08-25 01:13:53 +00:00
|
|
|
internalEngine = gin.Default()
|
2015-08-16 16:44:18 +00:00
|
|
|
})
|
|
|
|
return internalEngine
|
|
|
|
}
|
|
|
|
|
2018-09-15 02:23:32 +00:00
|
|
|
// LoadHTMLGlob is a wrapper for Engine.LoadHTMLGlob.
|
2015-08-16 16:44:18 +00:00
|
|
|
func LoadHTMLGlob(pattern string) {
|
|
|
|
engine().LoadHTMLGlob(pattern)
|
|
|
|
}
|
|
|
|
|
2018-09-15 02:23:32 +00:00
|
|
|
// LoadHTMLFiles is a wrapper for Engine.LoadHTMLFiles.
|
2015-08-16 16:44:18 +00:00
|
|
|
func LoadHTMLFiles(files ...string) {
|
|
|
|
engine().LoadHTMLFiles(files...)
|
|
|
|
}
|
|
|
|
|
2018-09-15 02:23:32 +00:00
|
|
|
// SetHTMLTemplate is a wrapper for Engine.SetHTMLTemplate.
|
2015-08-16 16:44:18 +00:00
|
|
|
func SetHTMLTemplate(templ *template.Template) {
|
|
|
|
engine().SetHTMLTemplate(templ)
|
|
|
|
}
|
|
|
|
|
2021-12-03 06:49:51 +00:00
|
|
|
// NoRoute adds handlers for NoRoute. It returns a 404 code by default.
|
2017-08-27 09:11:38 +00:00
|
|
|
func NoRoute(handlers ...gin.HandlerFunc) {
|
2015-08-16 16:44:18 +00:00
|
|
|
engine().NoRoute(handlers...)
|
|
|
|
}
|
|
|
|
|
2018-09-15 02:23:32 +00:00
|
|
|
// NoMethod is a wrapper for Engine.NoMethod.
|
2017-08-27 09:11:38 +00:00
|
|
|
func NoMethod(handlers ...gin.HandlerFunc) {
|
2015-08-16 16:44:18 +00:00
|
|
|
engine().NoMethod(handlers...)
|
|
|
|
}
|
|
|
|
|
2018-11-05 01:13:17 +00:00
|
|
|
// Group creates a new router group. You should add all the routes that have common middlewares or the same path prefix.
|
|
|
|
// For example, all the routes that use a common middleware for authorization could be grouped.
|
2017-08-27 09:11:38 +00:00
|
|
|
func Group(relativePath string, handlers ...gin.HandlerFunc) *gin.RouterGroup {
|
2015-08-16 16:44:18 +00:00
|
|
|
return engine().Group(relativePath, handlers...)
|
|
|
|
}
|
|
|
|
|
2018-09-15 02:23:32 +00:00
|
|
|
// Handle is a wrapper for Engine.Handle.
|
2017-08-27 09:11:38 +00:00
|
|
|
func Handle(httpMethod, relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
2015-08-16 16:44:18 +00:00
|
|
|
return engine().Handle(httpMethod, relativePath, handlers...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// POST is a shortcut for router.Handle("POST", path, handle)
|
2017-08-27 09:11:38 +00:00
|
|
|
func POST(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
2015-08-16 16:44:18 +00:00
|
|
|
return engine().POST(relativePath, handlers...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GET is a shortcut for router.Handle("GET", path, handle)
|
2017-08-27 09:11:38 +00:00
|
|
|
func GET(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
2015-08-16 16:44:18 +00:00
|
|
|
return engine().GET(relativePath, handlers...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DELETE is a shortcut for router.Handle("DELETE", path, handle)
|
2017-08-27 09:11:38 +00:00
|
|
|
func DELETE(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
2015-08-16 16:44:18 +00:00
|
|
|
return engine().DELETE(relativePath, handlers...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PATCH is a shortcut for router.Handle("PATCH", path, handle)
|
2017-08-27 09:11:38 +00:00
|
|
|
func PATCH(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
2015-08-16 16:44:18 +00:00
|
|
|
return engine().PATCH(relativePath, handlers...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PUT is a shortcut for router.Handle("PUT", path, handle)
|
2017-08-27 09:11:38 +00:00
|
|
|
func PUT(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
2015-08-16 16:44:18 +00:00
|
|
|
return engine().PUT(relativePath, handlers...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OPTIONS is a shortcut for router.Handle("OPTIONS", path, handle)
|
2017-08-27 09:11:38 +00:00
|
|
|
func OPTIONS(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
2015-08-16 16:44:18 +00:00
|
|
|
return engine().OPTIONS(relativePath, handlers...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HEAD is a shortcut for router.Handle("HEAD", path, handle)
|
2017-08-27 09:11:38 +00:00
|
|
|
func HEAD(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
2015-08-16 16:44:18 +00:00
|
|
|
return engine().HEAD(relativePath, handlers...)
|
|
|
|
}
|
|
|
|
|
2018-09-15 02:23:32 +00:00
|
|
|
// Any is a wrapper for Engine.Any.
|
2017-08-27 09:11:38 +00:00
|
|
|
func Any(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
2015-08-16 16:44:18 +00:00
|
|
|
return engine().Any(relativePath, handlers...)
|
|
|
|
}
|
|
|
|
|
2018-09-15 02:23:32 +00:00
|
|
|
// StaticFile is a wrapper for Engine.StaticFile.
|
2017-08-27 09:11:38 +00:00
|
|
|
func StaticFile(relativePath, filepath string) gin.IRoutes {
|
2015-08-16 16:44:18 +00:00
|
|
|
return engine().StaticFile(relativePath, filepath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Static serves files from the given file system root.
|
|
|
|
// Internally a http.FileServer is used, therefore http.NotFound is used instead
|
|
|
|
// of the Router's NotFound handler.
|
|
|
|
// To use the operating system's file system implementation,
|
|
|
|
// use :
|
2022-08-15 13:38:20 +00:00
|
|
|
//
|
|
|
|
// router.Static("/static", "/var/www")
|
2017-08-27 09:11:38 +00:00
|
|
|
func Static(relativePath, root string) gin.IRoutes {
|
2015-08-16 16:44:18 +00:00
|
|
|
return engine().Static(relativePath, root)
|
|
|
|
}
|
|
|
|
|
2018-09-15 02:23:32 +00:00
|
|
|
// StaticFS is a wrapper for Engine.StaticFS.
|
2017-08-27 09:11:38 +00:00
|
|
|
func StaticFS(relativePath string, fs http.FileSystem) gin.IRoutes {
|
2015-08-16 16:44:18 +00:00
|
|
|
return engine().StaticFS(relativePath, fs)
|
|
|
|
}
|
|
|
|
|
2022-01-12 14:12:32 +00:00
|
|
|
// Use attaches a global middleware to the router. i.e. the middlewares attached through Use() will be
|
2015-08-16 16:44:18 +00:00
|
|
|
// included in the handlers chain for every single request. Even 404, 405, static files...
|
|
|
|
// For example, this is the right place for a logger or error management middleware.
|
2017-08-27 09:11:38 +00:00
|
|
|
func Use(middlewares ...gin.HandlerFunc) gin.IRoutes {
|
2015-08-16 16:44:18 +00:00
|
|
|
return engine().Use(middlewares...)
|
|
|
|
}
|
|
|
|
|
2019-03-21 07:12:06 +00:00
|
|
|
// Routes returns a slice of registered routes.
|
|
|
|
func Routes() gin.RoutesInfo {
|
|
|
|
return engine().Routes()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run attaches to a http.Server and starts listening and serving HTTP requests.
|
2015-08-16 16:44:18 +00:00
|
|
|
// It is a shortcut for http.ListenAndServe(addr, router)
|
2018-11-05 01:13:17 +00:00
|
|
|
// Note: this method will block the calling goroutine indefinitely unless an error happens.
|
2015-08-16 16:44:18 +00:00
|
|
|
func Run(addr ...string) (err error) {
|
|
|
|
return engine().Run(addr...)
|
|
|
|
}
|
|
|
|
|
2019-03-21 07:12:06 +00:00
|
|
|
// RunTLS attaches to a http.Server and starts listening and serving HTTPS requests.
|
2015-08-16 16:44:18 +00:00
|
|
|
// It is a shortcut for http.ListenAndServeTLS(addr, certFile, keyFile, router)
|
2018-11-05 01:13:17 +00:00
|
|
|
// Note: this method will block the calling goroutine indefinitely unless an error happens.
|
2018-08-05 05:29:26 +00:00
|
|
|
func RunTLS(addr, certFile, keyFile string) (err error) {
|
2015-08-16 16:44:18 +00:00
|
|
|
return engine().RunTLS(addr, certFile, keyFile)
|
|
|
|
}
|
|
|
|
|
2019-03-21 07:12:06 +00:00
|
|
|
// RunUnix attaches to a http.Server and starts listening and serving HTTP requests
|
2021-12-03 06:49:51 +00:00
|
|
|
// through the specified unix socket (i.e. a file)
|
2018-11-05 01:13:17 +00:00
|
|
|
// Note: this method will block the calling goroutine indefinitely unless an error happens.
|
2015-08-16 16:44:18 +00:00
|
|
|
func RunUnix(file string) (err error) {
|
|
|
|
return engine().RunUnix(file)
|
|
|
|
}
|
2019-03-21 07:12:06 +00:00
|
|
|
|
|
|
|
// RunFd attaches the router to a http.Server and starts listening and serving HTTP requests
|
|
|
|
// through the specified file descriptor.
|
2019-04-22 15:11:57 +00:00
|
|
|
// Note: the method will block the calling goroutine indefinitely unless on error happens.
|
2019-03-21 07:12:06 +00:00
|
|
|
func RunFd(fd int) (err error) {
|
|
|
|
return engine().RunFd(fd)
|
|
|
|
}
|