gin 源码解析
2022-08-03 09:21:38 0 举报
AI智能生成
gin 源码解析, 关注我,后续有更多的源码解析
作者其他创作
大纲/内容
1. r := gin.Default()
1. debugPrintWARNINGDefault()
由环境变量`GIN_MODE` 来指定
2. engine := New()
1. 初始化 RouterGroup
2. engine.pool.New = func() interface{} {
return engine.allocateContext()
}
return engine.allocateContext()
}
3. engine.Use(Logger(), Recovery())
1. 打印请求日志
2. 错误处理
2. r.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong")
})
c.String(http.StatusOK, "pong")
})
1. handle(httpMethod, relativePath string, handlers HandlersChain)
1. group.calculateAbsolutePath(relativePath)
2. handlers = group.combineHandlers(handlers)
3. group.engine.addRoute(httpMethod, absolutePath, handlers)
3. r.Run(":8080")
1. engine.prepareTrustedCIDRs()
2. resolveAddress(addr)
3. http.ListenAndServe(address, engine)
4. ServeHTTP(w http.ResponseWriter, req *http.Request)
1. engine.pool.Get().(*Context)
2. c.reset()
3. engine.handleHTTPRequest(c)
1. 遍历所有的 engine.trees 找到对应的 handleFunc
2. value := root.getValue(rPath, c.params, unescape)
3. if value.params != nil {
c.Params = *value.params
}
if value.handlers != nil {
c.handlers = value.handlers
c.fullPath = value.fullPath
c.Next()
c.writermem.WriteHeaderNow()
return
}
c.Params = *value.params
}
if value.handlers != nil {
c.handlers = value.handlers
c.fullPath = value.fullPath
c.Next()
c.writermem.WriteHeaderNow()
return
}
4. 没有匹配的路由, 错误处理 或者 重定向
4. engine.pool.Put(c)
0 条评论
下一页