Beego框架使用

介绍

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

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

分析

Beego

  1. Beego设置路由的函数为
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的结构为:
// 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
// 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
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()
    }
    
  1. I18nControllerInterface是接口,所有组合I18nBaseController的类可以重载这些接口
type I18nControllerInterface interface {
    
    
   Setup()
   Process()
   Exec()
}

使用

  1. 创建类

    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. 路由

    var mappingMethods string = "*:Exec"
    beego.Router("/"+applocal+"/accessories", &accessories.IndexController{
          
          }, mappingMethods)
    
  • mappingMethods意味执行IndexController里的Exec函数,即

    扫描二维码关注公众号,回复: 12843052 查看本文章

    func (c *I18nBaseController) Exec() {
    defer c.recoverPanic()
    c.i18nC.Process()
    }

    最终执行的是IndexController中的Process

  1. Beego框架

在这里插入图片描述

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

总结

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

最后

大家如果喜欢我的文章,可以关注我的公众号(程序员麻辣烫)

往期文章回顾:

算法

  1. 算法学习计划
  2. 蛮力法
  3. 分治法
  4. 减治法

技术

  1. 浅谈微服务
  2. TCP性能优化
  3. 限流实现1
  4. Redis实现分布式锁
  5. Golang源码BUG追查
  6. 事务原子性、一致性、持久性的实现原理
  7. CDN请求过程详解
  8. 记博客服务被压垮的历程
  9. 常用缓存技巧
  10. 如何高效对接第三方支付
  11. Gin框架简洁版
  12. InnoDB锁与事务简析

读书笔记

  1. 敏捷革命
  2. 如何锻炼自己的记忆力
  3. 简单的逻辑学-读后感
  4. 热风-读后感
  5. 论语-读后感

思考

  1. 对项目管理的一些看法
  2. 对产品经理的一些思考
  3. 关于程序员职业发展的思考
  4. 关于代码review的思考
  5. Markdown编辑器推荐-typora

猜你喜欢

转载自blog.csdn.net/shida219/article/details/108739044