介绍
我们组服务端使用了Beego框架,使用的相对合理,本篇文章简单聊一下我们是如何使用框架的。
大家如果对Beego框架如果不熟悉,可以先看一下这篇文章 https://beego.me/ ,了解如何使用。
分析
Beego
- Beego设置路由的函数为
func Router(rootpath string, c ControllerInterface, mappingMethods ...string) *App {
BeeApp.Handlers.Add(rootpath, c, mappingMethods...)
return BeeApp
}
- mappingMethods这个参数用来设置对应 method 到函数名,定义如下
*
表示任意的 method 都执行该函数- 使用 httpmethod:funcname 格式来展示
- 多个不同的格式使用
;
分割 - 多个 method 对应同一个 funcname,method 之间通过
,
来分割
以下是一个 RESTful 的设计示例:
beego.Router(“/api/list”,&RestController{},“*:ListFood”)
beego.Router(“/api/create”,&RestController{},“post:CreateFood”)
- 其中ControllerInterface的结构为:
// ControllerInterface is an interface to uniform all controller handler.
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()
}
- 同时Beego的Controller实现了ControllerInterface
// Controller defines some basic http request handler operations, such as
// http context, template and view, session and xsrf.
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
ViewPath string
Layout string
LayoutSections map[string]string // the key is the section name and the value is the template name
TplPrefix string
TplExt string
EnableRender bool
// xsrf data
_xsrfToken string
XSRFExpire int
EnableXSRF bool
// session
CruSession session.Store
}
服务端
- 创建I18nBaseController,组合Beego的Controller,这么做可以导致I18nBaseController实现了Beego的ControllerInterface
type I18nBaseController struct {
beego.Controller
// 根据输入解析出来的参数数据,子类主动设置的控制参数
InputData *i18nhelper.XmInputData
//I18nController interface
i18nC I18nControllerInterface
}
I18nBaseController中重点实现了如下函数:
-
Init:初始化数据,并生成i18nC(i18nC, ok := app.(I18nControllerInterface))
-
Prepare:主要处理登录、access/reffer检查等,也会调用i18nC.Setup
-
Exec:用于调用Process
func (c *I18nBaseController) Exec() { defer c.recoverPanic() c.i18nC.Process() }
- I18nControllerInterface是接口,所有组合I18nBaseController的类可以重载这些接口
type I18nControllerInterface interface {
Setup()
Process()
Exec()
}
使用
-
创建类
type IndexController struct { base.I18nBaseController } func (c *IndexController) Setup() { c.InputData.IsNeedLogin = true //默认不需要登录 } func (c *IndexController) Process() { c.Data["json"] = "rt" c.ServeJSON(true) }
- 这个类里有I18nBaseController,所以也实现了Beego的ControllerInterface
- 实现函数Setup和Process,对I18nBaseController里对应的函数实现了重载
-
路由
var mappingMethods string = "*:Exec" beego.Router("/"+applocal+"/accessories", &accessories.IndexController{}, mappingMethods)
-
mappingMethods意味执行IndexController里的Exec函数,即
func (c *I18nBaseController) Exec() {
defer c.recoverPanic()
c.i18nC.Process()
}最终执行的是IndexController中的Process
- Beego框架
- 以Beego中ServeHTTP为例,会先执行IndexController的Init,然后执行IndexController,最后执行Exec,如此完成了一次请求
总结
本文给大家演示了团队内部是怎样使用Beego框架的,这套使用方案给研发提供了很多灵活性,希望对大家有所帮助。
最后
大家如果喜欢我的文章,可以关注我的公众号(程序员麻辣烫)
往期文章回顾:
算法
技术
- 浅谈微服务
- TCP性能优化
- 限流实现1
- Redis实现分布式锁
- Golang源码BUG追查
- 事务原子性、一致性、持久性的实现原理
- CDN请求过程详解
- 记博客服务被压垮的历程
- 常用缓存技巧
- 如何高效对接第三方支付
- Gin框架简洁版
- InnoDB锁与事务简析
读书笔记
思考