gins.go
This file
Path
ginS/gins.go
164 lines · 5.7 KiB · Go
Last changed by
feat(gin): add support for the HTTP QUERY method (RFC 10008) (#4806)Gaurav Patilbefore adoptionb1f77cf
History of this fileTake it
1// Copyright 2014 Manu Martinez-Almeida. All rights reserved.2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45package ginS67import (8 "html/template"9 "net/http"10 "sync"1112 "github.com/gin-gonic/gin"13)1415var engine = sync.OnceValue(func() *gin.Engine {16 return gin.Default()17})1819// LoadHTMLGlob is a wrapper for Engine.LoadHTMLGlob.20func LoadHTMLGlob(pattern string) {21 engine().LoadHTMLGlob(pattern)22}2324// LoadHTMLFiles is a wrapper for Engine.LoadHTMLFiles.25func LoadHTMLFiles(files ...string) {26 engine().LoadHTMLFiles(files...)27}2829// LoadHTMLFS is a wrapper for Engine.LoadHTMLFS.30func LoadHTMLFS(fs http.FileSystem, patterns ...string) {31 engine().LoadHTMLFS(fs, patterns...)32}3334// SetHTMLTemplate is a wrapper for Engine.SetHTMLTemplate.35func SetHTMLTemplate(templ *template.Template) {36 engine().SetHTMLTemplate(templ)37}3839// NoRoute adds handlers for NoRoute. It returns a 404 code by default.40func NoRoute(handlers ...gin.HandlerFunc) {41 engine().NoRoute(handlers...)42}4344// NoMethod is a wrapper for Engine.NoMethod.45func NoMethod(handlers ...gin.HandlerFunc) {46 engine().NoMethod(handlers...)47}4849// Group creates a new router group. You should add all the routes that have common middlewares or the same path prefix.50// For example, all the routes that use a common middleware for authorization could be grouped.51func Group(relativePath string, handlers ...gin.HandlerFunc) *gin.RouterGroup {52 return engine().Group(relativePath, handlers...)53}5455// Handle is a wrapper for Engine.Handle.56func Handle(httpMethod, relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {57 return engine().Handle(httpMethod, relativePath, handlers...)58}5960// POST is a shortcut for router.Handle("POST", path, handle)61func POST(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {62 return engine().POST(relativePath, handlers...)63}6465// GET is a shortcut for router.Handle("GET", path, handle)66func GET(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {67 return engine().GET(relativePath, handlers...)68}6970// QUERY is a shortcut for router.Handle("QUERY", path, handle)71func QUERY(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {72 return engine().QUERY(relativePath, handlers...)73}7475// DELETE is a shortcut for router.Handle("DELETE", path, handle)76func DELETE(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {77 return engine().DELETE(relativePath, handlers...)78}7980// PATCH is a shortcut for router.Handle("PATCH", path, handle)81func PATCH(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {82 return engine().PATCH(relativePath, handlers...)83}8485// PUT is a shortcut for router.Handle("PUT", path, handle)86func PUT(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {87 return engine().PUT(relativePath, handlers...)88}8990// OPTIONS is a shortcut for router.Handle("OPTIONS", path, handle)91func OPTIONS(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {92 return engine().OPTIONS(relativePath, handlers...)93}9495// HEAD is a shortcut for router.Handle("HEAD", path, handle)96func HEAD(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {97 return engine().HEAD(relativePath, handlers...)98}99100// Any is a wrapper for Engine.Any.101func Any(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {102 return engine().Any(relativePath, handlers...)103}104105// StaticFile is a wrapper for Engine.StaticFile.106func StaticFile(relativePath, filepath string) gin.IRoutes {107 return engine().StaticFile(relativePath, filepath)108}109110// Static serves files from the given file system root.111// Internally a http.FileServer is used, therefore http.NotFound is used instead112// of the Router's NotFound handler.113// To use the operating system's file system implementation,114// use :115//116// router.Static("/static", "/var/www")117func Static(relativePath, root string) gin.IRoutes {118 return engine().Static(relativePath, root)119}120121// StaticFS is a wrapper for Engine.StaticFS.122func StaticFS(relativePath string, fs http.FileSystem) gin.IRoutes {123 return engine().StaticFS(relativePath, fs)124}125126// Use attaches a global middleware to the router. i.e. the middlewares attached through Use() will be127// included in the handlers chain for every single request. Even 404, 405, static files...128// For example, this is the right place for a logger or error management middleware.129func Use(middlewares ...gin.HandlerFunc) gin.IRoutes {130 return engine().Use(middlewares...)131}132133// Routes returns a slice of registered routes.134func Routes() gin.RoutesInfo {135 return engine().Routes()136}137138// Run attaches to a http.Server and starts listening and serving HTTP requests.139// It is a shortcut for http.ListenAndServe(addr, router)140// Note: this method will block the calling goroutine indefinitely unless an error happens.141func Run(addr ...string) (err error) {142 return engine().Run(addr...)143}144145// RunTLS attaches to a http.Server and starts listening and serving HTTPS requests.146// It is a shortcut for http.ListenAndServeTLS(addr, certFile, keyFile, router)147// Note: this method will block the calling goroutine indefinitely unless an error happens.148func RunTLS(addr, certFile, keyFile string) (err error) {149 return engine().RunTLS(addr, certFile, keyFile)150}151152// RunUnix attaches to a http.Server and starts listening and serving HTTP requests153// through the specified unix socket (i.e. a file)154// Note: this method will block the calling goroutine indefinitely unless an error happens.155func RunUnix(file string) (err error) {156 return engine().RunUnix(file)157}158159// RunFd attaches the router to a http.Server and starts listening and serving HTTP requests160// through the specified file descriptor.161// Note: the method will block the calling goroutine indefinitely unless an error happens.162func RunFd(fd int) (err error) {163 return engine().RunFd(fd)164}