...

Source file src/github.com/gin-gonic/gin/ginS/gins.go

Documentation: github.com/gin-gonic/gin/ginS

     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  	"net/http"
    10  	"sync"
    11  
    12  	"github.com/gin-gonic/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 returns 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  //
   112  //	router.Static("/static", "/var/www")
   113  func Static(relativePath, root string) gin.IRoutes {
   114  	return engine().Static(relativePath, root)
   115  }
   116  
   117  // StaticFS is a wrapper for Engine.StaticFS.
   118  func StaticFS(relativePath string, fs http.FileSystem) gin.IRoutes {
   119  	return engine().StaticFS(relativePath, fs)
   120  }
   121  
   122  // Use attaches a global middleware to the router. i.e. the middlewares attached through Use() will be
   123  // included in the handlers chain for every single request. Even 404, 405, static files...
   124  // For example, this is the right place for a logger or error management middleware.
   125  func Use(middlewares ...gin.HandlerFunc) gin.IRoutes {
   126  	return engine().Use(middlewares...)
   127  }
   128  
   129  // Routes returns a slice of registered routes.
   130  func Routes() gin.RoutesInfo {
   131  	return engine().Routes()
   132  }
   133  
   134  // Run attaches to a http.Server and starts listening and serving HTTP requests.
   135  // It is a shortcut for http.ListenAndServe(addr, router)
   136  // Note: this method will block the calling goroutine indefinitely unless an error happens.
   137  func Run(addr ...string) (err error) {
   138  	return engine().Run(addr...)
   139  }
   140  
   141  // RunTLS attaches to a http.Server and starts listening and serving HTTPS requests.
   142  // It is a shortcut for http.ListenAndServeTLS(addr, certFile, keyFile, router)
   143  // Note: this method will block the calling goroutine indefinitely unless an error happens.
   144  func RunTLS(addr, certFile, keyFile string) (err error) {
   145  	return engine().RunTLS(addr, certFile, keyFile)
   146  }
   147  
   148  // RunUnix attaches to a http.Server and starts listening and serving HTTP requests
   149  // through the specified unix socket (i.e. a file)
   150  // Note: this method will block the calling goroutine indefinitely unless an error happens.
   151  func RunUnix(file string) (err error) {
   152  	return engine().RunUnix(file)
   153  }
   154  
   155  // RunFd attaches the router to a http.Server and starts listening and serving HTTP requests
   156  // through the specified file descriptor.
   157  // Note: the method will block the calling goroutine indefinitely unless on error happens.
   158  func RunFd(fd int) (err error) {
   159  	return engine().RunFd(fd)
   160  }
   161  

View as plain text