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
注意:
- beege和bee是兩個概念。beego是框架,bee是工具,是命令。
- 在安裝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.6 Use "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.2 2016/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.go 2016/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.2 2016/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.go 2016/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 cnblogs bogon:cnblogs zuxingyu$ bee run ______ | ___ \ | |_/ / ___ ___ | ___ \ / _ \ / _ \ | |_/ /| __/| __/ \____/ \___| \___| v1.5.2 2016/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 successful 2016/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.2 2016/10/31 22:28:03 Packaging application: /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs 2016/10/31 22:28:03 Building application... 2016/10/31 22:28:03 Env: GOOS=darwin GOARCH=amd64 2016/10/31 22:28:06 Build successful 2016/10/31 22:28:06 Excluding relpath prefix: . 2016/10/31 22:28:06 Excluding relpath suffix: .go:.DS_Store:.tmp 2016/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 controllers bee generate view [viewpath] generate CRUD view in viewpath bee 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 file bee generate test [routerfile] generate testcase bee 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:postgres@127.0.0.1:5432/postgres -level: [1 | 2 | 3], 1 = models; 2 = models,controllers; 3 = models,controllers,router
這些都是我目前常用的命令。如果有其他命令以后可以開個專題專門實驗這些命令。