【Gin-v1.9.0源码阅读】ginS/gins.go

// 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"

	"github.com/gin-gonic/gin"
)

var once sync.Once
var internalEngine *gin.Engine

func engine() *gin.Engine {
	once.Do(func() {
		internalEngine = gin.Default()
	})
	return internalEngine
}

// LoadHTMLGlob is a wrapper for Engine.LoadHTMLGlob.
// LoadHTMLGlob是Engine.LoadHTMLGlob的包装器。
func LoadHTMLGlob(pattern string) {
	engine().LoadHTMLGlob(pattern)
}

// LoadHTMLFiles is a wrapper for Engine.LoadHTMLFiles.
// LoadHTMLFiles是Engine.LoadHTMLFiles的包装器。
func LoadHTMLFiles(files ...string) {
	engine().LoadHTMLFiles(files...)
}

// SetHTMLTemplate is a wrapper for Engine.SetHTMLTemplate.
// SetHTMLTemplate是Engine.SetHTMLTemplate的包装器。
func SetHTMLTemplate(templ *template.Template) {
	engine().SetHTMLTemplate(templ)
}

// NoRoute adds handlers for NoRoute. It returns a 404 code by default.
// NoRoute为NoRoute添加处理程序。默认情况下,它返回一个404代码。
func NoRoute(handlers ...gin.HandlerFunc) {
	engine().NoRoute(handlers...)
}

// NoMethod is a wrapper for Engine.NoMethod.
// NoMethod是Engine.NoMethod的包装器。
func NoMethod(handlers ...gin.HandlerFunc) {
	engine().NoMethod(handlers...)
}

// 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.
// 例如,所有使用通用中间件进行授权的路由都可以分组。
func Group(relativePath string, handlers ...gin.HandlerFunc) *gin.RouterGroup {
	return engine().Group(relativePath, handlers...)
}

// Handle is a wrapper for Engine.Handle.
// Handle是Engine.Handle的包装器。
func Handle(httpMethod, relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
	return engine().Handle(httpMethod, relativePath, handlers...)
}

// POST is a shortcut for router.Handle("POST", path, handle)
func POST(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
	return engine().POST(relativePath, handlers...)
}

// GET is a shortcut for router.Handle("GET", path, handle)
// GET是路由器的快捷方式。Handle(“GET”,path,Handle)
func GET(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
	return engine().GET(relativePath, handlers...)
}

// DELETE is a shortcut for router.Handle("DELETE", path, handle)
// DELETE是路由器的快捷方式。句柄(“DELETE”,路径,句柄)
func DELETE(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
	return engine().DELETE(relativePath, handlers...)
}

// PATCH is a shortcut for router.Handle("PATCH", path, handle)
// PATCH是路由器的快捷方式。句柄(“PATCH”,路径,句柄)
func PATCH(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
	return engine().PATCH(relativePath, handlers...)
}

// PUT is a shortcut for router.Handle("PUT", path, handle)
// PUT是路由器的快捷方式。Handle(“PUT”,path,Handle)
func PUT(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
	return engine().PUT(relativePath, handlers...)
}

// OPTIONS is a shortcut for router.Handle("OPTIONS", path, handle)
// OPTIONS是路由器的快捷方式。句柄(“OPTIONS”,路径,句柄)
func OPTIONS(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
	return engine().OPTIONS(relativePath, handlers...)
}

// HEAD is a shortcut for router.Handle("HEAD", path, handle)
// HEAD是路由器的快捷方式。句柄(“HEAD”,路径,句柄)
func HEAD(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
	return engine().HEAD(relativePath, handlers...)
}

// Any is a wrapper for Engine.Any.
// Any是Engine.Any的包装。
func Any(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
	return engine().Any(relativePath, handlers...)
}

// StaticFile is a wrapper for Engine.StaticFile.
// StaticFile是Engine.StaticFile的包装器。
func StaticFile(relativePath, filepath string) gin.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.
// 静态服务来自给定文件系统根的文件。在内部使用http.FileServer,因此使用http.NotFound而不是路由器的NotFound处理程序。
// To use the operating system's file system implementation,
// 为了使用操作系统的文件系统实现,
// use :
// 使用:
//
//	router.Static("/static", "/var/www")
func Static(relativePath, root string) gin.IRoutes {
	return engine().Static(relativePath, root)
}

// StaticFS is a wrapper for Engine.StaticFS.
// StaticFS是Engine.StaticFS的包装器。
func StaticFS(relativePath string, fs http.FileSystem) gin.IRoutes {
	return engine().StaticFS(relativePath, fs)
}

// Use attaches a global middleware to the router. i.e. the middlewares attached through Use() will be
// included in the handlers chain for every single request. Even 404, 405, static files...
// Use将一个全局中间件连接到路由器。即通过Use()连接的中间件将被包括在每个单个请求的处理程序链中。甚至404405,静态文件。。。
// For example, this is the right place for a logger or error management middleware.
// 例如,这是记录器或错误管理中间件的正确位置。
func Use(middlewares ...gin.HandlerFunc) gin.IRoutes {
	return engine().Use(middlewares...)
}

// Routes returns a slice of registered routes.
// Routes返回已注册路由的一部分。
func Routes() gin.RoutesInfo {
	return engine().Routes()
}

// Run attaches to a http.Server and starts listening and serving HTTP requests.
// Run连接到一个http.Server,并开始侦听和提供http请求。
// It is a shortcut for http.ListenAndServe(addr, router)
// Note: this method will block the calling goroutine indefinitely unless an error happens.
// 这是http.ListnAndServe(addr,router)的快捷方式,注意:除非发生错误,否则此方法将无限期地阻止调用goroutine。
func Run(addr ...string) (err error) {
	return engine().Run(addr...)
}

// RunTLS attaches to a http.Server and starts listening and serving HTTPS requests.
// RunTLS连接到http.Server并开始侦听和提供HTTPS请求。
// It is a shortcut for http.ListenAndServeTLS(addr, certFile, keyFile, router)
// Note: this method will block the calling goroutine indefinitely unless an error happens.
// 这是http.ListnAndServeTLS(addr、certFile、keyFile、router)的快捷方式注意:除非发生错误,否则此方法将无限期地阻止调用goroutine。
func RunTLS(addr, certFile, keyFile string) (err error) {
	return engine().RunTLS(addr, certFile, keyFile)
}

// RunUnix attaches to a http.Server and starts listening and serving HTTP requests
// through the specified unix socket (i.e. a file)
// RunUnix连接到http.Server,并开始通过指定的unix套接字(即文件)侦听和服务http请求
// Note: this method will block the calling goroutine indefinitely unless an error happens.
// 注意:除非发生错误,否则此方法将无限期地阻止调用goroutine。
func RunUnix(file string) (err error) {
	return engine().RunUnix(file)
}

// RunFd attaches the router to a http.Server and starts listening and serving HTTP requests
// through the specified file descriptor.
// RunFd将路由器连接到http.Server,并通过指定的文件描述符开始侦听和服务http请求。
// Note: the method will block the calling goroutine indefinitely unless on error happens.
// 注意:除非出现错误,否则该方法将无限期地阻止调用goroutine。
func RunFd(fd int) (err error) {
	return engine().RunFd(fd)
}

猜你喜欢

转载自blog.csdn.net/qq2942713658/article/details/131029371
今日推荐