Go beego架構使用筆記(一)

來源:互聯網
上載者:User

標籤:資訊   相關   compress   arch   定義   empty   base   git   win   

Beego介紹

  beego我認為是go初學者比較容易上手的一門MVC Web架構。簡單易懂,最重要的一點就是提供了中文文檔,這對於我這種英語能力比較差的人來說就是福音。

  beego的官網上是這麼介紹beego的:“beego 是一個快速開發 Go 應用的 HTTP 架構,他可以用來快速開發 API、Web 及後端服務等各種應用,是一個 RESTful 的架構,主要設計構想來源於 tornado、sinatra 和 flask 這三個架構,但是結合了 Go 本身的一些特性(interface、struct 嵌入等)而設計的一個架構”。從中可以看出beego可以快速開發API介面、可以實現服務端的各種應用

Beego官網

  beego官網 : https://beego.me/

  github : https://github.com/beego

Beego安裝

  beego 包含一些應用程式範例以幫您學習並使用 beego 應用程式框架。

  您需要安裝 Go 1.1+ 以確保所有功能的正常使用。

  你需要安裝 Beego 和 Bee 的開發工具:

$ go get github.com/astaxie/beego$ go get github.com/beego/bee

  注意:

  1. beege和bee是兩個概念。beego是架構,bee是工具,是命令。
  2. 在安裝Beego前,先確認是否將$GOPATH/bin寫入GO環境中。
Bee工具的使用

 安裝完成Bee後,在控制台輸入bee

Bee is a tool for managing beego framework.Usage:    bee command [arguments]The commands are:    new         Create a Beego application    run         run the app and start a Web server for development    pack        Compress a beego project into a single file    api         create an API beego application    hprose      create an rpc application use hprose base on beego framework    bale        packs non-Go files to Go source files    version     prints the current Bee version    generate    source code generator    migrate     run database migrations    fix         fix the beego application to make it compatible with beego 1.6Use "bee help [command]" for more information about a command.Additional help topics:Use "bee help [topic]" for more information about that topic.

主要有以上10個命令,下面說幾個常用的命令

 new命令

  new 命令是建立一個 Web 項目,我們在命令列下執行 bee new <項目名> 就可以建立一個新的項目。但是注意該命令必須在 $GOPATH/src 下執行。最後會在 $GOPATH/src 相應目錄下產生如下目錄結構的項目:

bogon:src zuxingyu$ bee new cnblogs______| ___ | |_/ /  ___   ___| ___ \ / _ \ / _ | |_/ /|  __/|  __/\____/  \___| \___| v1.5.22016/10/31 22:16:11 [INFO] Creating application...    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/conf/    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/controllers/    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/models/    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/routers/    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/tests/    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/static/    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/static/js/    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/static/css/    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/static/img/    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/views/    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/conf/app.conf    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/controllers/default.go    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/views/index.tpl    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/routers/router.go    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/tests/default_test.go    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/main.go2016/10/31 22:16:11 [SUCC] New application successfully created!

  這樣一個beego架構的Go項目就產生了

 api命令

  上面的 new 命令是用來建立 Web 項目,不過很多使用者使用 beego 來開發 API 應用程式。所以這個 api 命令就是用來建立 API 應用程式的,屬於純服務端業務。執行命令之後如下所示:

bogon:src zuxingyu$ bee api cnblogsApi______| ___ | |_/ /  ___   ___| ___ \ / _ \ / _ | |_/ /|  __/|  __/\____/  \___| \___| v1.5.22016/10/31 22:19:51 [INFO] Creating API...    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/conf    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/controllers    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/tests    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/conf/app.conf    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/models    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/routers/    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/controllers/object.go    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/controllers/user.go    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/tests/default_test.go    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/routers/router.go    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/models/object.go    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/models/user.go    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/main.go2016/10/31 22:19:51 [SUCC] New API successfully created!

  從上面的目錄我們可以看到和 Web 項目相比,少了 static 和 views 目錄,多了一個 test 模組,用來做單元測試的。

  同時,該命令還支援一些自訂參數自動連接資料庫建立相關model和controller:
  bee api [appname] [-tables=“”] [-driver=mysql] [-conn=root:@tcp(127.0.0.1:3306)/test]
  如果conn參數為空白則建立一個樣本項目,否則將基於連結資訊連結資料庫建立項目。

 run命令  bee run 命令是監控 beego 的項目,通過 fsnotify監控檔案系統。但是注意該命令必須在$GOPATH/src/appname下執行。 

 

bogon:src zuxingyu$ cd cnblogsbogon:cnblogs zuxingyu$ bee run______| ___ | |_/ /  ___   ___| ___ \ / _ \ / _ | |_/ /|  __/|  __/\____/  \___| \___| v1.5.22016/10/31 22:22:30 [INFO] Using ‘cnblogs‘ as ‘appname‘2016/10/31 22:22:30 [INFO] Initializing watcher...2016/10/31 22:22:30 [TRAC] Directory(/Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/controllers)2016/10/31 22:22:30 [TRAC] Directory(/Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs)2016/10/31 22:22:30 [TRAC] Directory(/Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/routers)2016/10/31 22:22:30 [TRAC] Directory(/Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/tests)2016/10/31 22:22:30 [INFO] Start building...2016/10/31 22:22:34 [SUCC] Build was successful2016/10/31 22:22:34 [INFO] Restarting cnblogs ...2016/10/31 22:22:34 [INFO] ./cnblogs is running...2016/10/31 22:22:34 [I] [asm_amd64.s:2086] http server Running on http://:8080

  注意:Linux/Mac Os 在命令列運行時,結束直接用Ctrl + C 結束

 pack命令    

    pack 目錄用來發布應用的時候打包,會把項目打包成 zip 包,這樣我們部署的時候直接把打包之後的項目上傳,解壓就可以部署了。可能在後面工作中會使用Docker去做容器。具體的在後面使用後告訴大家。

bogon:cnblogs zuxingyu$ bee pack______| ___ | |_/ /  ___   ___| ___ \ / _ \ / _ | |_/ /|  __/|  __/\____/  \___| \___| v1.5.22016/10/31 22:28:03 Packaging application: /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs2016/10/31 22:28:03 Building application...2016/10/31 22:28:03 Env: GOOS=darwin GOARCH=amd642016/10/31 22:28:06 Build successful2016/10/31 22:28:06 Excluding relpath prefix: .2016/10/31 22:28:06 Excluding relpath suffix: .go:.DS_Store:.tmp2016/10/31 22:28:07 Writing to output: `/Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/cnblogs.tar.gz`

  使用命令後會打出一個tar.gz包。放入Linux/Mac OS 解壓即可。

 version 命令

  這個命令是動態擷取bee、beego和Go的版本,這樣一旦使用者出現錯誤,可以通過該命令來查看當前的版本,沒啥需要解釋的。只是查看下目前的版本。

bogon:cnblogs zuxingyu$ bee version______| ___ | |_/ /  ___   ___| ___ \ / _ \ / _ | |_/ /|  __/|  __/\____/  \___| \___| v1.5.2├── Beego     : 1.7.1├── GoVersion : go1.7.1├── GOOS      : darwin├── GOARCH    : amd64├── NumCPU    : 4├── GOPATH    : /Users/zuxingyu/Documents/GoWorkSpace├── GOROOT    : /usr/local/go├── Compiler  : gc└── Date      : Monday, 31 Oct 2016
 generate 命令

   這是一個重要的命令,尤其是產生api項目時,generate docs可以產生swagger的文檔。

bee generate scaffold [scaffoldname] [-fields=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]    The generate scaffold command will do a number of things for you.    -fields: a list of table fields. Format: field:type, ...    -driver: [mysql | postgres | sqlite], the default is mysql    -conn:   the connection string used by the driver, the default is root:@tcp(127.0.0.1:3306)/test    example: bee generate scaffold post -fields="title:string,body:text"bee generate model [modelname] [-fields=""]    generate RESTful model based on fields    -fields: a list of table fields. Format: field:type, ...bee generate controller [controllerfile]    generate RESTful controllersbee generate view [viewpath]    generate CRUD view in viewpathbee generate migration [migrationfile] [-fields=""]    generate migration file for making database schema update    -fields: a list of table fields. Format: field:type, ...bee generate docs    generate swagger doc filebee generate test [routerfile]    generate testcasebee generate appcode [-tables=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] [-level=3]    generate appcode based on an existing database    -tables: a list of table names separated by ‘,‘, default is empty, indicating all tables    -driver: [mysql | postgres | sqlite], the default is mysql    -conn:   the connection string used by the driver.             default for mysql:    root:@tcp(127.0.0.1:3306)/test             default for postgres: postgres://postgres:[email protected]:5432/postgres    -level:  [1 | 2 | 3], 1 = models; 2 = models,controllers; 3 = models,controllers,router

 這些都是我目前常用的命令。如果有其他命令以後可以開個專題專門實驗這些命令。

 

Go beego架構使用筆記(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.