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