Beegoフレームワークの使用法

前書き

私たちのグループサーバーは、比較的合理的なBeegoフレームワークを使用しています。この記事では、フレームワークの使用方法について簡単に説明します。

Beegoフレームワークに慣れていない場合は、この記事https://beego.me/を読んで、その使用方法を学ぶことができます。

分析

ビーゴ

  1. ルートを設定するBeegoの機能は次のとおりです。
func Router(rootpath string, c ControllerInterface, mappingMethods ...string) *App {
    
    
   BeeApp.Handlers.Add(rootpath, c, mappingMethods...)
   return BeeApp
}
  1. MappingMethodsパラメーターは、対応するメソッドを次のように定義された関数名に設定するために使用されます
  • *任意のメソッドが関数を実行することを示します
  • httpmethod:funcname形式を使用して表示します
  • 複数の異なるフォーマットの;セグメンテーション
  • ,セグメント化による方法間で、複数の同じ機能名に対応する方法

以下は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のコントローラーは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に実装されています。

  • 初期化:データを初期化し、i18nC(i18nC、ok:= app。(I18nControllerInterface))を生成します

  • 準備:主にログイン、アクセス/参照チェックなどを処理します。i18nC.Setupも呼び出します。

  • Exec:プロセスの呼び出しに使用

    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関数を実行することを意味します。

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


    最終的な実行は、IndexControllerのプロセスです。

  1. Beegoフレームワーク

ここに画像の説明を挿入します

  • BeegoのServeHTTPを例にとると、IndexControllerのInitが最初に実行され、次にIndexControllerが実行され、Execが最後に実行され、リクエストが完了します。

総括する

この記事では、チーム内でBeegoフレームワークがどのように使用されているかを示します。この使用計画は、研究開発に多くの柔軟性を提供し、すべての人に役立つことを願っています。

やっと

私の記事が気に入ったら、私の公式アカウント(プログラマーMala Tang)をフォローしてください。

以前の記事のレビュー:

アルゴリズム

  1. アルゴリズム学習計画
  2. 強引な
  3. 分割統治
  4. 削減方法

技術

  1. マイクロサービスについて話す
  2. TCPパフォーマンスの最適化
  3. 電流制限の実現1
  4. Redisは分散ロックを実装しています
  5. Golangソースコードのバグ追跡
  6. トランザクションの原子性、一貫性、耐久性の実現原理
  7. 詳細なCDNリクエストプロセス
  8. 押しつぶされたブログサービスの歴史
  9. 一般的なキャッシュ手法
  10. サードパーティの支払いに効率的に接続する方法
  11. ジンフレームワークの簡潔なバージョン
  12. InnoDBのロックとトランザクションの簡単な分析

研究ノート

  1. アジャイル革命
  2. あなたの記憶を行使する方法
  3. 単純なロジック-読んだ後
  4. 熱風-読んだ後
  5. 論語-読んだ後の考え

考え

  1. プロジェクト管理に関するいくつかの見解
  2. プロダクトマネージャーに関するいくつかの考え
  3. プログラマーのキャリア開発についての考え
  4. コードレビューについて考える
  5. マークダウンエディターの推奨事項-typora

おすすめ

転載: blog.csdn.net/shida219/article/details/108739044
おすすめ