beego源码解析之路由
2021/6/9 20:23:31
本文主要是介绍beego源码解析之路由,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
目录
简单的例子
路由涉及的重要组件
App
ControllerRegister
ControllerInfo
Tree
ControllerInterface
Controller
路由表注册流程:
路由匹配流程
简单的例子
func main() { beego.Router("/get", &MainController{}) beego.Run() } type MainController struct { beego.Controller } func (c *MainController) Get() { c.Data["Website"] = "beego.me" c.Data["Email"] = "astaxie@gmail.com" c.TplName = "index.tpl" }
1、继承beego.Controller
2、注册路由
3、beego框架启动
路由涉及的重要组件
App
beego框架框的开始 beego.Run()
结构如下:
type App struct { Handlers *ControllerRegister Server *http.Server }
1、该实体包含了一个go原生Handler的实现
2、go的原生Server对象
ControllerRegister
Handler的具体实现,该实现包含了路由的解析与转发,可以看做是一个go web的 gateway
结构如下:
type ControllerRegister struct { routers map[string]*Tree //路由表 enableFilter bool //标志位,是否有过滤器 filters map[int][]*FilterRouter //文件过滤,int为本文件37行定义的几个变量 pool sync.Pool //对象池 }
ControllerInfo
封装的具体路由信息
结构如下:
type controllerInfo struct { pattern string //调用模式 controllerType reflect.Type //类型 methods map[string]string //支持的方法集合 handler http.Handler //原生的handler接口 runFunction FilterFunc //过滤方法 routerType int //路由类型 }
Tree
路由树
type Tree struct { //前缀 prefix string //search fix route first //不带正则的路由 fixrouters []*Tree //if set, failure to match fixrouters search then search wildcard //通配符 如果设置的并且查找 fixrouters失败时会来查找 wildcard wildcard *Tree //if set, failure to match wildcard search //叶子 如果设置并且查找 wildcard失败后会查找 leaves,里面保存了一些正则需要的信息 leaves []*leafInfo }
ControllerInterface
所有 Controller的通用接口
type ControllerInterface interface { Init(ct *context.Context, controllerName, actionName string, app interface{}) Prepare() Get() Post() Delete() Put() Head() Patch() Options() Finish() Render() error XSRFToken() string CheckXSRFCookie() bool HandlerFunc(fn string) bool URLMapping() }
Controller
ControllerInterface的实现,业务层的controller,只需要继承此controller就可以加入路由树中
type Controller struct { // context data Ctx *context.Context Data map[interface{}]interface{} // route controller info controllerName string actionName string methodMapping map[string]func() //method:routertree gotofunc string AppController interface{} // template data TplName string Layout string LayoutSections map[string]string // the key is the section name and the value is the template name TplExt string EnableRender bool // xsrf data _xsrfToken string XSRFExpire int EnableXSRF bool // session CruSession session.Store }
路由表注册流程:
路由匹配流程
1、请求统一打到ControllerRegister.ServeHTTP(rw http.ResponseWriter , r http.Request)
2、该方法内负责查找路由,分发调用、渲染页面
这篇关于beego源码解析之路由的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-05MongoDB入门:快速掌握NoSQL数据库基础
- 2024-08-28go 项目中怎么打印调试-icode9专业技术文章分享
- 2024-08-21swoole未来的发展前景与golang对比哪个更好-icode9专业技术文章分享
- 2024-08-16goland 已经下了中文插件了, 怎么设置成中文-icode9专业技术文章分享
- 2024-07-26使用 SendGrid 的 Go 客户端库能同时给多个邮箱发吗-icode9专业技术文章分享
- 2024-07-26使用 SendGrid 的 Go 客户端库时怎么设置header 和 标签tag 呢-icode9专业技术文章分享
- 2024-07-26SendGrid 对邮件的类别(Categories)和标签的数量有限制吗?-icode9专业技术文章分享
- 2024-07-17课程推荐《高性能GO企业级APM监控系统实战》
- 2024-06-26解决google chrome helper 内存占用较高!
- 2024-04-01got an unexpected keyword argument