2015-08-16 19:44:18 +03:00
|
|
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
|
|
|
// 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 04:13:53 +03:00
|
|
|
"github.com/gin-gonic/gin"
|
2015-08-16 19:44:18 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var once sync.Once
|
2017-08-25 04:13:53 +03:00
|
|
|
var internalEngine *gin.Engine
|
2015-08-16 19:44:18 +03:00
|
|
|
|
2017-08-25 04:13:53 +03:00
|
|
|
func engine() *gin.Engine {
|
2015-08-16 19:44:18 +03:00
|
|
|
once.Do(func() {
|
2017-08-25 04:13:53 +03:00
|
|
|
internalEngine = gin.Default()
|
2015-08-16 19:44:18 +03:00
|
|
|
})
|
|
|
|
return internalEngine
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadHTMLGlob(pattern string) {
|
|
|
|
engine().LoadHTMLGlob(pattern)
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadHTMLFiles(files ...string) {
|
|
|
|
engine().LoadHTMLFiles(files...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetHTMLTemplate(templ *template.Template) {
|
|
|
|
engine().SetHTMLTemplate(templ)
|
|
|
|
}
|
|
|
|
|
2016-04-15 02:16:46 +03:00
|
|
|
// NoRoute adds handlers for NoRoute. It return a 404 code by default.
|
2015-08-16 19:44:18 +03:00
|
|
|
func NoRoute(handlers ...HandlerFunc) {
|
|
|
|
engine().NoRoute(handlers...)
|
|
|
|
}
|
|
|
|
|
2016-04-15 02:16:46 +03:00
|
|
|
// NoMethod sets the handlers called when... TODO
|
2015-08-16 19:44:18 +03:00
|
|
|
func NoMethod(handlers ...HandlerFunc) {
|
|
|
|
engine().NoMethod(handlers...)
|
|
|
|
}
|
|
|
|
|
2016-04-15 02:16:46 +03:00
|
|
|
// Group creates a new router group. You should add all the routes that have common middlwares or the same path prefix.
|
2015-08-16 19:44:18 +03:00
|
|
|
// For example, all the routes that use a common middlware for authorization could be grouped.
|
|
|
|
func Group(relativePath string, handlers ...HandlerFunc) *RouterGroup {
|
|
|
|
return engine().Group(relativePath, handlers...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Handle(httpMethod, relativePath string, handlers ...HandlerFunc) IRoutes {
|
|
|
|
return engine().Handle(httpMethod, relativePath, handlers...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// POST is a shortcut for router.Handle("POST", path, handle)
|
|
|
|
func POST(relativePath string, handlers ...HandlerFunc) IRoutes {
|
|
|
|
return engine().POST(relativePath, handlers...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GET is a shortcut for router.Handle("GET", path, handle)
|
|
|
|
func GET(relativePath string, handlers ...HandlerFunc) IRoutes {
|
|
|
|
return engine().GET(relativePath, handlers...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DELETE is a shortcut for router.Handle("DELETE", path, handle)
|
|
|
|
func DELETE(relativePath string, handlers ...HandlerFunc) IRoutes {
|
|
|
|
return engine().DELETE(relativePath, handlers...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PATCH is a shortcut for router.Handle("PATCH", path, handle)
|
|
|
|
func PATCH(relativePath string, handlers ...HandlerFunc) IRoutes {
|
|
|
|
return engine().PATCH(relativePath, handlers...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PUT is a shortcut for router.Handle("PUT", path, handle)
|
|
|
|
func PUT(relativePath string, handlers ...HandlerFunc) IRoutes {
|
|
|
|
return engine().PUT(relativePath, handlers...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OPTIONS is a shortcut for router.Handle("OPTIONS", path, handle)
|
|
|
|
func OPTIONS(relativePath string, handlers ...HandlerFunc) IRoutes {
|
|
|
|
return engine().OPTIONS(relativePath, handlers...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HEAD is a shortcut for router.Handle("HEAD", path, handle)
|
|
|
|
func HEAD(relativePath string, handlers ...HandlerFunc) IRoutes {
|
|
|
|
return engine().HEAD(relativePath, handlers...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Any(relativePath string, handlers ...HandlerFunc) IRoutes {
|
|
|
|
return engine().Any(relativePath, handlers...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func StaticFile(relativePath, filepath string) IRoutes {
|
|
|
|
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 :
|
|
|
|
// router.Static("/static", "/var/www")
|
|
|
|
func Static(relativePath, root string) IRoutes {
|
|
|
|
return engine().Static(relativePath, root)
|
|
|
|
}
|
|
|
|
|
|
|
|
func StaticFS(relativePath string, fs http.FileSystem) IRoutes {
|
|
|
|
return engine().StaticFS(relativePath, fs)
|
|
|
|
}
|
|
|
|
|
2016-04-15 02:16:46 +03:00
|
|
|
// Use attachs a global middleware to the router. ie. the middlewares attached though Use() will be
|
2015-08-16 19:44:18 +03: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.
|
|
|
|
func Use(middlewares ...HandlerFunc) IRoutes {
|
|
|
|
return engine().Use(middlewares...)
|
|
|
|
}
|
|
|
|
|
2016-04-15 02:16:46 +03:00
|
|
|
// Run : The router is attached to a http.Server and starts listening and serving HTTP requests.
|
2015-08-16 19:44:18 +03:00
|
|
|
// It is a shortcut for http.ListenAndServe(addr, router)
|
|
|
|
// Note: this method will block the calling goroutine undefinitelly unless an error happens.
|
|
|
|
func Run(addr ...string) (err error) {
|
|
|
|
return engine().Run(addr...)
|
|
|
|
}
|
|
|
|
|
2016-04-15 02:16:46 +03:00
|
|
|
// RunTLS : The router is attached to a http.Server and starts listening and serving HTTPS requests.
|
2015-08-16 19:44:18 +03:00
|
|
|
// It is a shortcut for http.ListenAndServeTLS(addr, certFile, keyFile, router)
|
|
|
|
// Note: this method will block the calling goroutine undefinitelly unless an error happens.
|
|
|
|
func RunTLS(addr string, certFile string, keyFile string) (err error) {
|
|
|
|
return engine().RunTLS(addr, certFile, keyFile)
|
|
|
|
}
|
|
|
|
|
2016-04-15 02:16:46 +03:00
|
|
|
// RunUnix : The router is attached to a http.Server and starts listening and serving HTTP requests
|
2015-08-16 19:44:18 +03:00
|
|
|
// through the specified unix socket (ie. a file)
|
|
|
|
// Note: this method will block the calling goroutine undefinitelly unless an error happens.
|
|
|
|
func RunUnix(file string) (err error) {
|
|
|
|
return engine().RunUnix(file)
|
|
|
|
}
|