name: Automated API Document

sort: 2

APIドキュメントの自動化

Automated document is a very cool feature that I wish to have. Now it became real in Beego. As I said Beego will not only boost the development of API but also make the API easy to use for the user. Ok, let’s try it out now. First create a new API application by bee api beeapi 自動ドキュメントは、欲しい非常にクールな機能です。beegoでは今となっては現実となりました。私が言ったようにboogoは、 APIの開発を後押しするだけでなく、ユーザーにとって使いAPIが容易になるわけではありません。大丈夫です。今それを試してみましょう。最初のbeego api beeapiによる新しいAPIアプリケーションを作成

API global settings

routers/router.goのトップにコメントを追加してください:

  1. // @APIVersion 1.0.0
  2. // @Title mobile API
  3. // @Description mobile has every tool to get any job done, so codename for the new mobile APIs.
  4. // @Contact astaxie@gmail.com
  5. package routers

The comments above set the global information. The available settings: 上のコメントはグローバル情報として設定しています。有用な設定:

  • @APIVersion
  • @Title
  • @Description
  • @Contact
  • @TermsOfServiceUrl
  • @License
  • @LicenseUrl

Router Parsing

Right now automated API document only supports namespace+Include and only supports two levels parsing. The first level is API version and the second level is the modules.

ルータの構文解析

今自動化されたAPIのドキュメントでは、名前空間をサポートしています`+含めるとのみ解析する2つのレベルがサポートされています。最初のレベルは、APIバージョンであり、第二のレベルは、モジュールです。

  1. func init() {
  2. ns :=
  3. beego.NewNamespace("/v1",
  4. beego.NSNamespace("/customer",
  5. beego.NSInclude(
  6. &controllers.CustomerController{},
  7. &controllers.CustomerCookieCheckerController{},
  8. ),
  9. ),
  10. beego.NSNamespace("/catalog",
  11. beego.NSInclude(
  12. &controllers.CatalogController{},
  13. ),
  14. ),
  15. beego.NSNamespace("/newsletter",
  16. beego.NSInclude(
  17. &controllers.NewsLetterController{},
  18. ),
  19. ),
  20. beego.NSNamespace("/cms",
  21. beego.NSInclude(
  22. &controllers.CMSController{},
  23. ),
  24. ),
  25. beego.NSNamespace("/suggest",
  26. beego.NSInclude(
  27. &controllers.SearchController{},
  28. ),
  29. ),
  30. )
  31. beego.AddNamespace(ns)
  32. }

# アプリケーションのコメント

コメントの最も重要な部分です。例えば:

  1. package controllers
  2. import "github.com/astaxie/beego"
  3. // CMS API
  4. type CMSController struct {
  5. beego.Controller
  6. }
  7. func (c *CMSController) URLMapping() {
  8. c.Mapping("StaticBlock", c.StaticBlock)
  9. c.Mapping("Product", c.Product)
  10. }
  11. // @Title getStaticBlock
  12. // @Description get all the staticblock by key
  13. // @Param key path string true "The email for login"
  14. // @Success 200 {object} models.ZDTCustomer.Customer
  15. // @Failure 400 Invalid email supplied
  16. // @Failure 404 User not found
  17. // @router /staticblock/:key [get]
  18. func (c *CMSController) StaticBlock() {
  19. }
  20. // @Title Get Product list
  21. // @Description Get Product list by some info
  22. // @Success 200 {object} models.ZDTProduct.ProductList
  23. // @Param category_id query int false "category id"
  24. // @Param brand_id query int false "brand id"
  25. // @Param query query string false "query of search"
  26. // @Param segment query string false "segment"
  27. // @Param sort query string false "sort option"
  28. // @Param dir query string false "direction asc or desc"
  29. // @Param offset query int false "offset"
  30. // @Param limit query int false "count limit"
  31. // @Param price query float false "price"
  32. // @Param special_price query bool false "whether this is special price"
  33. // @Param size query string false "size filter"
  34. // @Param color query string false "color filter"
  35. // @Param format query bool false "choose return format"
  36. // @Failure 400 no enough input
  37. // @Failure 500 get products common error
  38. // @router /products [get]
  39. func (c *CMSController) Product() {
  40. }

We defined the comment above for CMSController which will show for this module. Then we need to define the comment for every functions. Below is the supported comments: このモジュールが表示されるCMSControllerのため、上記のコメントを定義しました。その後、すべての機能のためのコメントを定義する必要があります。以下はサポートされているコメントです:

  • @Title

    The title for this API. it’s a string, all the content after the first space will be parsed as the title. このAPIのタイトルです。それは最初のスペースの後にすべてのコンテンツがタイトルとして解析され、文字列です。

  • @Description

  1. The description for this API. it's a string, all the content after the first space will be parsed as the description.
  • @Param

    @Param defines the parameters sent to the server. There are five columns for each @Param:

    1. parameter name;
    2. parameter sending type; It can be form, query, path, body or header. form means the parameter send by POST. query means the parameter in url send by GET. path means the parameter in the url path, such as key in the former example. body means the raw data send from request body. header means the parameter in request header.
    3. parameter data type
    4. required
    5. comment
  • @Success

    The success message returned to client. Three parameters.

    1. status code.
    2. return type; Must wrap with {}.
    3. returned object or string. For {object}, use path and the object name of your project here and bee tool will look up the object while generate the docs. For example models.ZDTProduct.ProductList represents ProductList object under /models/ZDTProduct

      Use space to separate these three parameters

  • @Failure

    The failure message returned to client. Two parameters separated by space.

    1. status code.
    2. Error message
  • @router

    Router information. Two parameters separated by space.

    1. The request’s router address.
    2. Supported request methods. Wrap in []. Use , to separate multiple methods.

Generate document automatically

以下が動作させるためのステップです:

  1. Enable docs by setting EnableDocs = true in conf/app.conf
  2. conf/app.confEnableDocs = trueを設定することにより、ドキュメントを有効にしてください
  3. main.goにて`_ “beeapi/docs”をインポートしてください。
  4. APIアプリケーションの実行と自動的に文書を再構築するために、bee run -downdoc=true -gendoc=trueを使用してください。
  5. Visit swagger document from API project's URL and port. (see item #1 below) 5.訪問 APIプロジェクトのURLとポートから闊歩ドキュメント。 (以下の項目# 1を参照)

APIドキュメントが利用可能になりました。ブラウザを開いて、それを確認してください。

APIドキュメントの自動化 - 图1

APIドキュメントの自動化 - 图2

Problems You May Have

  1. CORS Two solutions

    1. Integrate swagger into the application. Download swagger and put it into project folder. (bee run -downdoc=true will also download it and put it into project folder) And before beego.Run() in func main() of main.go

      if beego.RunMode == “dev” {

      1. beego.DirectoryIndex = true
      2. beego.StaticDir["/swagger"] = "swagger"

      }

      And then visit swagger document from API project’s URL and port.

    2. Make API support CORS

      1. ctx.Output.Header("Access-Control-Allow-Origin", "*")
  2. Other problems. This is a feature used in my own project. If you have some other problems please fire issues to us.

  3. 他の問題。これは私自身のプロジェクトで使用する機能です。いくつかの他の問題が発生した場合、我々に問題を報告してください。