• Gin源码之gin.Engine结构体及其方法


    Gin源码之gin.Engine结构体及其方法

    1.Engine结构体

    gin.Engine
    Engine是gin里面的一个结构体,里面有很多字段,它还嵌套了RouterGroup结构体

    type Engine struct {
    	RouterGroup
    	RedirectTrailingSlash bool
    	RedirectFixedPath bool
    	HandleMethodNotAllowed bool
    	ForwardedByClientIP bool
    	AppEngine bool
    	UseRawPath bool
    	UnescapePathValues bool
    	RemoveExtraSlash bool
    	RemoteIPHeaders []string
    	TrustedPlatform string
    	MaxMultipartMemory int64
    	delims           render.Delims
    	secureJSONPrefix string
    	HTMLRender       render.HTMLRender
    	FuncMap          template.FuncMap
    	allNoRoute       HandlersChain
    	allNoMethod      HandlersChain
    	noRoute          HandlersChain
    	noMethod         HandlersChain
    	pool             sync.Pool
    	trees            methodTrees
    	maxParams        uint16
    	maxSections      uint16
    	trustedProxies   []string
    	trustedCIDRs     []*net.IPNet
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    2.Run方法

    如果不传参数的话,默认是8080窗口;
    r.Run(“:9090”)进行指定

    // Run attaches the router to a http.Server and starts listening and serving HTTP requests.
    // It is a shortcut for http.ListenAndServe(addr, router)
    // Note: this method will block the calling goroutine indefinitely unless an error happens.
    func (engine *Engine) Run(addr ...string) (err error) {
       defer func() { debugPrintError(err) }()
    
       if engine.isUnsafeTrustedProxies() {
          debugPrint("[WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.\n" +
             "Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.")
       }
    
       address := resolveAddress(addr)
       debugPrint("Listening and serving HTTP on %s\n", address)
       err = http.ListenAndServe(address, engine.Handler())
       return
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    3.NoRoute方法

    // NoRoute adds handlers for NoRoute. It return a 404 code by default.
    func (engine *Engine) NoRoute(handlers ...HandlerFunc) {
    	engine.noRoute = handlers
    	engine.rebuild404Handlers()
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    4.Use方法

    // Use attaches a global middleware to the router. ie. the middleware attached though Use() will be
    // 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 (engine *Engine) Use(middleware ...HandlerFunc) IRoutes {
    	engine.RouterGroup.Use(middleware...)
    	engine.rebuild404Handlers()
    	engine.rebuild405Handlers()
    	return engine
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    5.LoadHTMLGlob方法

    // LoadHTMLGlob loads HTML files identified by glob pattern
    // and associates the result with HTML renderer.
    func (engine *Engine) LoadHTMLGlob(pattern string) {
    	left := engine.delims.Left
    	right := engine.delims.Right
    	templ := template.Must(template.New("").Delims(left, right).Funcs(engine.FuncMap).ParseGlob(pattern))
    
    	if IsDebugging() {
    		debugPrintLoadTemplate(templ)
    		engine.HTMLRender = render.HTMLDebug{Glob: pattern, FuncMap: engine.FuncMap, Delims: engine.delims}
    		return
    	}
    
    	engine.SetHTMLTemplate(templ)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    6.LoadHTMLFiles方法

    // LoadHTMLFiles loads a slice of HTML files
    // and associates the result with HTML renderer.
    func (engine *Engine) LoadHTMLFiles(files ...string) {
       if IsDebugging() {
          engine.HTMLRender = render.HTMLDebug{Files: files, FuncMap: engine.FuncMap, Delims: engine.delims}
          return
       }
    
       templ := template.Must(template.New("").Delims(engine.delims.Left, engine.delims.Right).Funcs(engine.FuncMap).ParseFiles(files...))
       engine.SetHTMLTemplate(templ)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    7.Delims方法

    // Delims sets template left and right delims and returns a Engine instance.
    func (engine *Engine) Delims(left, right string) *Engine {
    	engine.delims = render.Delims{Left: left, Right: right}
    	return engine
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    8.SetFuncMap方法

    // SetFuncMap sets the FuncMap used for template.FuncMap.
    func (engine *Engine) SetFuncMap(funcMap template.FuncMap) {
    	engine.FuncMap = funcMap
    }
    
    • 1
    • 2
    • 3
    • 4
  • 相关阅读:
    Mysql判断某个数据库中是否包含某个表,与pymysql工具函数
    C++ Reference: Standard C++ Library reference: C Library: cstdio: FILE
    IP代理|一文看懂IPv4与IPv6
    vue3 封装使用 pinia (可直接使用,包含数据持久化)
    2022年9月12日-9月18日(ue4热更新视频教程+rhi模块源码抄写调剂)
    Google开源依赖注入框架-Guice指南
    用ts类型要描述 1到100范围的数字
    印度金融公司数据遭泄露,泄露数据超过3TB
    logrus简单使用教程
    【零基础学Java】第二十篇 包装类3(Math,Arrays,System,BigInteger,BigDecimal,日期)
  • 原文地址:https://blog.csdn.net/qq_53267860/article/details/126658280