Beego framework usage

Introduction

Our group server uses the Beego framework, which is relatively reasonable. This article briefly talks about how we use the framework.

If you are not familiar with the Beego framework, you can read this article https://beego.me/ to learn how to use it.

analysis

Beego

  1. The function of Beego to set the route is
func Router(rootpath string, c ControllerInterface, mappingMethods ...string) *App {
    
    
   BeeApp.Handlers.Add(rootpath, c, mappingMethods...)
   return BeeApp
}
  1. The mappingMethods parameter is used to set the corresponding method to the function name, defined as follows
  • *Indicates that any method executes the function
  • Use httpmethod:funcname format to display
  • A plurality of different format ;segmentation
  • Method corresponding to a plurality of the same funcname, between the method by ,segmenting

The following is a RESTful design example:

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

  1. The structure of ControllerInterface is:
// 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. At the same time, Beego's Controller implements 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
}

Server

  1. Create I18nBaseController and combine Beego's Controller. Doing so can cause I18nBaseController to implement Beego's ControllerInterface
type I18nBaseController struct {
    
    
   beego.Controller

   // 根据输入解析出来的参数数据,子类主动设置的控制参数
   InputData *i18nhelper.XmInputData

   //I18nController interface
   i18nC I18nControllerInterface
}

The following functions are implemented in I18nBaseController:

  • Init: Initialize data and generate i18nC(i18nC, ok := app.(I18nControllerInterface))

  • Prepare: Mainly handle login, access/reffer check, etc., also call i18nC.Setup

  • Exec: used to call Process

    func (c *I18nBaseController) Exec() {
          
          
       defer c.recoverPanic()
       c.i18nC.Process()
    }
    
  1. I18nControllerInterface is an interface, all classes that combine I18nBaseController can override these interfaces
type I18nControllerInterface interface {
    
    
   Setup()
   Process()
   Exec()
}

use

  1. Create class

    type IndexController struct {
          
          
       base.I18nBaseController
    }
    
    func (c *IndexController) Setup() {
          
          
    	c.InputData.IsNeedLogin = true //默认不需要登录
    }
    
    func (c *IndexController) Process() {
          
          
       c.Data["json"] = "rt"
       c.ServeJSON(true)
    }
    
  • There is I18nBaseController in this class, so Beego's ControllerInterface is also implemented
  • Implement the functions Setup and Process, and overload the corresponding functions in I18nBaseController
  1. routing

    var mappingMethods string = "*:Exec"
    beego.Router("/"+applocal+"/accessories", &accessories.IndexController{
          
          }, mappingMethods)
    
  • mappingMethods means to execute the Exec function in IndexController, that is

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

    The final execution is the Process in IndexController

  1. Beego framework

Insert picture description here

  • Taking ServeHTTP in Beego as an example, the Init of IndexController will be executed first, then IndexController will be executed, and Exec will be executed last, thus completing a request

to sum up

This article shows you how the Beego framework is used inside the team. This use plan provides a lot of flexibility for research and development, and I hope it will be helpful to everyone.

At last

If you like my article, you can follow my official account (Programmer Mala Tang)

Review of previous articles:

algorithm

  1. Algorithm learning plan
  2. Brute force
  3. Divide and conquer
  4. Reduction method

technology

  1. Talking about microservices
  2. TCP performance optimization
  3. Current limit realization 1
  4. Redis implements distributed locks
  5. Golang source code bug tracking
  6. The realization principle of transaction atomicity, consistency and durability
  7. Detailed CDN request process
  8. The history of blog service being crushed
  9. Common caching techniques
  10. How to efficiently connect with third-party payment
  11. Gin framework concise version
  12. A brief analysis of InnoDB locks and transactions

study notes

  1. Agile revolution
  2. How to exercise your memory
  3. Simple logic-after reading
  4. Hot air-after reading
  5. The Analects-Thoughts after Reading

Thinking

  1. Some views on project management
  2. Some thoughts on product managers
  3. Thoughts on the career development of programmers
  4. Thinking about code review
  5. Markdown editor recommendation-typora

Guess you like

Origin blog.csdn.net/shida219/article/details/108739044