一、beego框架
1、beego框架簡介
beego 是一個快速開發 Go 應用的 HTTP 框架,他可以用來快速開發 API、Web 及后端服務等各種應用,是一個 RESTful 的框架,主要設計靈感來源於 tornado、sinatra 和 flask 這三個框架,但是結合了 Go 本身的一些特性(interface、struct 嵌入等)而設計的一個框架。
參考官網:https://beego.me/docs/intro/
2、beego 的執行邏輯
beego 是一個典型的 MVC 架構,它的執行邏輯如下圖所示:
3、beego 項目結構
一般的 beego 項目的目錄如下所示:
├── conf
│ └── app.conf
├── controllers
│ ├── admin
│ └── default.go
├── main.go
├── models
│ └── models.go
├── static
│ ├── css
│ ├── ico
│ ├── img
│ └── js
└── views
├── admin
└── index.tpl
從上面的目錄結構我們可以看出來 M(models 目錄)、V(views 目錄)和 C(controllers 目錄)的結構, main.go 是入口文件。
4、beego框架安裝
go get github.com/astaxie/beego
參考:https://github.com/astaxie/beego
設置GoPATH路徑:
5、bee工具
go get github.com/beego/bee
new命令是新建一個Web項目,我們在命令行下執行bee new<項目名>就可以創建一個新的項目。但是注意該命令必段在$GOPATH/src 下執行。
api 命令 是用來創建API應用的。
安裝步驟:
#絕對路徑,運行:bee
#安裝bee C:\Users\560140>go get github.com/beego/bee #絕對路徑,運行:bee C:\Users\560140>%GOPATH%/bin/bee Bee is a Fast and Flexible tool for managing your Beego Web Application. USAGE bee command [arguments] AVAILABLE COMMANDS version Prints the current Bee version migrate Runs database migrations api Creates a Beego API application bale Transforms non-Go files to Go source files fix Fixes your application by making it compatible with newer versio ns of Beego dlv Start a debugging session using Delve dockerize Generates a Dockerfile for your Beego application generate Source code generator hprose Creates an RPC application based on Hprose and Beego frameworks new Creates a Beego application pack Compresses a Beego application into a single file rs Run customized scripts run Run the application by starting a local development server server serving static content over HTTP on port Use bee help [command] for more information about a command. ADDITIONAL HELP TOPICS Use bee help [topic] for more information about that topic
# 設置環境變量臨時生效
#設置環境變量,臨時生效 C:\Users\560140>set PATH=%PATH%;%GOPATH%/bin #運行bee C:\Users\560140>bee Bee is a Fast and Flexible tool for managing your Beego Web Application. USAGE bee command [arguments] AVAILABLE COMMANDS version Prints the current Bee version migrate Runs database migrations api Creates a Beego API application bale Transforms non-Go files to Go source files fix Fixes your application by making it compatible with newer versio ns of Beego dlv Start a debugging session using Delve dockerize Generates a Dockerfile for your Beego application generate Source code generator hprose Creates an RPC application based on Hprose and Beego frameworks new Creates a Beego application pack Compresses a Beego application into a single file rs Run customized scripts run Run the application by starting a local development server server serving static content over HTTP on port Use bee help [command] for more information about a command. ADDITIONAL HELP TOPICS Use bee help [topic] for more information about that topic.
6、beego創建項目
創建web項目: bee new WEB
C:\Users\560140>bee new WEB ______ | ___ \ | |_/ / ___ ___ | ___ \ / _ \ / _ \ | |_/ /| __/| __/ \____/ \___| \___| v1.10.0 2019/01/29 14:25:14 WARN ▶ 0001 You current workdir is not inside $GOPATH/sr c. 2019/01/29 14:25:14 INFO ▶ 0002 Creating application... create D:\GoFiles\src\WEB\ create D:\GoFiles\src\WEB\conf\ create D:\GoFiles\src\WEB\controllers\ create D:\GoFiles\src\WEB\models\ create D:\GoFiles\src\WEB\routers\ create D:\GoFiles\src\WEB\tests\ create D:\GoFiles\src\WEB\static\ create D:\GoFiles\src\WEB\static\js\ create D:\GoFiles\src\WEB\static\css\ create D:\GoFiles\src\WEB\static\img\ create D:\GoFiles\src\WEB\views\ create D:\GoFiles\src\WEB\conf\app.conf create D:\GoFiles\src\WEB\controllers\default.go create D:\GoFiles\src\WEB\views\index.tpl create D:\GoFiles\src\WEB\routers\router.go create D:\GoFiles\src\WEB\tests\default_test.go create D:\GoFiles\src\WEB\main.go 2019/01/29 14:25:14 SUCCESS ▶ 0003 New application successfully created!
創建API項目: bee api API
C:\Users\560140>bee api API ______ | ___ \ | |_/ / ___ ___ | ___ \ / _ \ / _ \ | |_/ /| __/| __/ \____/ \___| \___| v1.10.0 2019/01/29 14:25:32 WARN ▶ 0001 You current workdir is not inside $GOPATH/sr c. 2019/01/29 14:25:32 INFO ▶ 0002 Creating API... create D:\GoFiles\src\API create D:\GoFiles\src\API\conf create D:\GoFiles\src\API\controllers create D:\GoFiles\src\API\tests create D:\GoFiles\src\API\conf\app.conf create D:\GoFiles\src\API\models create D:\GoFiles\src\API\routers\ create D:\GoFiles\src\API\controllers\object.go create D:\GoFiles\src\API\controllers\user.go create D:\GoFiles\src\API\tests\default_test.go create D:\GoFiles\src\API\routers\router.go create D:\GoFiles\src\API\models\object.go create D:\GoFiles\src\API\models\user.go create D:\GoFiles\src\API\main.go 2019/01/29 14:25:33 SUCCESS ▶ 0003 New API successfully created!
7、在LiteIDE中打開WEB項目
把WEB項目導入到LiteIDE中
執行項目:
D:\GoFiles\src\WEB>bee run ______ | ___ \ | |_/ / ___ ___ | ___ \ / _ \ / _ \ | |_/ /| __/| __/ \____/ \___| \___| v1.10.0 2019/01/29 14:33:35 INFO ▶ 0001 Using 'WEB' as 'appname' 2019/01/29 14:33:35 INFO ▶ 0002 Initializing watcher... WEB/controllers WEB/routers WEB 2019/01/29 14:33:41 SUCCESS ▶ 0003 Built Successfully! 2019/01/29 14:33:41 INFO ▶ 0004 Restarting 'WEB.exe'... 2019/01/29 14:33:41 SUCCESS ▶ 0005 './WEB.exe' is running... 2019/01/29 14:33:41.919 [I] [asm_amd64.s:1333] http server Running on http://:8 080