• 主页
  • 架构
  • 编程语言
  • 数据存储
  • 网络
  • VMware
  • 服务器
  • 组网
  • AI
  • 算法系列
  • 设计模式
  • 读书笔记
  • 思考
  • 工具
  • 其它技术

  • 主页
  • 架构
  • 编程语言
  • 数据存储
  • 网络
  • VMware
  • 服务器
  • 组网
  • AI
  • 算法系列
  • 设计模式
  • 读书笔记
  • 思考
  • 工具
  • 其它技术

Beego框架使用

2024-08-18

介绍

我们组服务端使用了Beego框架,使用的相对合理,本篇文章简单聊一下我们是如何使用框架的。

大家如果对Beego框架如果不熟悉,可以先看一下这篇文章 https://beego.me/ ,了解如何使用。

分析

Beego

  1. Beego设置路由的函数为
1
2
3
4
func Router(rootpath string, c ControllerInterface, mappingMethods ...string) *App {
BeeApp.Handlers.Add(rootpath, c, mappingMethods...)
return BeeApp
}
  1. mappingMethods这个参数用来设置对应 method 到函数名,定义如下
  • *表示任意的 method 都执行该函数
  • 使用 httpmethod:funcname 格式来展示
  • 多个不同的格式使用 ; 分割
  • 多个 method 对应同一个 funcname,method 之间通过 , 来分割

以下是一个 RESTful 的设计示例:

beego.Router(“/api/list”,&RestController{},”*:ListFood”)
beego.Router(“/api/create”,&RestController{},”post:CreateFood”)

  1. 其中ControllerInterface的结构为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 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()
}
  1. 同时Beego的Controller实现了ControllerInterface
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
29
30
31
// 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
}

服务端

  1. 创建I18nBaseController,组合Beego的Controller,这么做可以导致I18nBaseController实现了Beego的ControllerInterface
1
2
3
4
5
6
7
8
9
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

    1
    2
    3
    4
    func (c *I18nBaseController) Exec() {
    defer c.recoverPanic()
    c.i18nC.Process()
    }
  1. I18nControllerInterface是接口,所有组合I18nBaseController的类可以重载这些接口
1
2
3
4
5
type I18nControllerInterface interface {
Setup()
Process()
Exec()
}

使用

  1. 创建类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    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里对应的函数实现了重载
  1. 路由

    1
    2
    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

  1. Beego框架
  • 以Beego中ServeHTTP为例,会先执行IndexController的Init,然后执行IndexController,最后执行Exec,如此完成了一次请求

总结

本文给大家演示了团队内部是怎样使用Beego框架的,这套使用方案给研发提供了很多灵活性,希望对大家有所帮助。

扫一扫,分享到微信

微信分享二维码
微服务之服务框架和注册中心
Effective-Go
© 2025 John Doe
Hexo Theme Yilia by Litten