bee 工具簡介
bee 工具是一個為了協助快速開發 beego 項目而創建的項目,通過 bee 您可以很容易的進行 beego 項目的創建、熱編譯、開發、測試、和部署。
bee 工具的安裝
您可以通過如下的方式安裝 bee 工具:
go get github.com/beego/bee
安裝完之后,bee
可執行文件默認存放在 $GOPATH/bin
里面,所以您需要把 $GOPATH/bin
添加到您的環境變量中,才可以進行下一步。
如何添加環境變量,請自行搜索
如果你本機設置了GOBIN
,那么上面的命令就會安裝到GOBIN
下,請添加 GOBIN 到你的環境變量中
bee 工具命令詳解
我們在命令行輸入 bee
,可以看到如下的信息:
Bee is a Fast and Flexible tool for managing your Beego Web Application. Usage: bee command [arguments] The commands are: version show the bee & beego version migrate run database migrations api create an api application base on beego framework bale packs non-Go files to Go source files new create an application base on beego framework run run the app which can hot compile pack compress an beego project fix Fixes your application by making it compatible with newer versions 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 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.
new
命令
new
命令是新建一個 Web 項目,我們在命令行下執行 bee new <項目名>
就可以創建一個新的項目。但是注意該命令必須在 $GOPATH/src
下執行。最后會在 $GOPATH/src
相應目錄下生成如下目錄結構的項目:
bee new myproject [INFO] Creating application... /gopath/src/myproject/ /gopath/src/myproject/conf/ /gopath/src/myproject/controllers/ /gopath/src/myproject/models/ /gopath/src/myproject/static/ /gopath/src/myproject/static/js/ /gopath/src/myproject/static/css/ /gopath/src/myproject/static/img/ /gopath/src/myproject/views/ /gopath/src/myproject/conf/app.conf /gopath/src/myproject/controllers/default.go /gopath/src/myproject/views/index.tpl /gopath/src/myproject/main.go 13-11-25 09:50:39 [SUCC] New application successfully created!
myproject ├── conf │ └── app.conf ├── controllers │ └── default.go ├── main.go ├── models ├── routers │ └── router.go ├── static │ ├── css │ ├── img │ └── js ├── tests │ └── default_test.go └── views └── index.tpl 8 directories, 4 files
api
命令
上面的 new
命令是用來新建 Web 項目,不過很多用戶使用 beego 來開發 API 應用。所以這個 api
命令就是用來創建 API 應用的,執行命令之后如下所示:
bee api apiproject create app folder: /gopath/src/apiproject create conf: /gopath/src/apiproject/conf create controllers: /gopath/src/apiproject/controllers create models: /gopath/src/apiproject/models create tests: /gopath/src/apiproject/tests create conf app.conf: /gopath/src/apiproject/conf/app.conf create controllers default.go: /gopath/src/apiproject/controllers/default.go create tests default.go: /gopath/src/apiproject/tests/default_test.go create models object.go: /gopath/src/apiproject/models/object.go create main.go: /gopath/src/apiproject/main.go
這個項目的目錄結構如下:
apiproject ├── conf │ └── app.conf ├── controllers │ └── object.go │ └── user.go ├── docs │ └── doc.go ├── main.go ├── models │ └── object.go │ └── user.go ├── routers │ └── router.go └── tests └── default_test.go
從上面的目錄我們可以看到和 Web 項目相比,少了 static 和 views 目錄,多了一個 test 模塊,用來做單元測試的。
同時,該命令還支持一些自定義參數自動連接數據庫創建相關 model 和 controller:bee api [appname] [-tables=""] [-driver=mysql] [-conn="root:<password>@tcp(127.0.0.1:3306)/test"]
如果 conn 參數為空則創建一個示例項目,否則將基於鏈接信息鏈接數據庫創建項目。
run
命令
我們在開發 Go 項目的時候最大的問題是經常需要自己手動去編譯再運行,bee run
命令是監控 beego 的項目,通過 fsnotify監控文件系統。但是注意該命令必須在 $GOPATH/src/appname
下執行。
這樣我們在開發過程中就可以實時的看到項目修改之后的效果:
bee run 13-11-25 09:53:04 [INFO] Uses 'myproject' as 'appname' 13-11-25 09:53:04 [INFO] Initializing watcher... 13-11-25 09:53:04 [TRAC] Directory(/gopath/src/myproject/controllers) 13-11-25 09:53:04 [TRAC] Directory(/gopath/src/myproject/models) 13-11-25 09:53:04 [TRAC] Directory(/gopath/src/myproject) 13-11-25 09:53:04 [INFO] Start building... 13-11-25 09:53:16 [SUCC] Build was successful 13-11-25 09:53:16 [INFO] Restarting myproject ... 13-11-25 09:53:16 [INFO] ./myproject is running...
我們打開瀏覽器就可以看到效果 http://localhost:8080/
:
如果我們修改了 Controller
下面的 default.go
文件,我們就可以看到命令行輸出:
13-11-25 10:11:20 [EVEN] "/gopath/src/myproject/controllers/default.go": DELETE|MODIFY 13-11-25 10:11:20 [INFO] Start building... 13-11-25 10:11:20 [SKIP] "/gopath/src/myproject/controllers/default.go": CREATE 13-11-25 10:11:23 [SKIP] "/gopath/src/myproject/controllers/default.go": MODIFY 13-11-25 10:11:23 [SUCC] Build was successful 13-11-25 10:11:23 [INFO] Restarting myproject ... 13-11-25 10:11:23 [INFO] ./myproject is running...
刷新瀏覽器我們看到新的修改內容已經輸出。
pack
命令
pack
目錄用來發布應用的時候打包,會把項目打包成 zip 包,這樣我們部署的時候直接把打包之后的項目上傳,解壓就可以部署了:
bee pack app path: /gopath/src/apiproject GOOS darwin GOARCH amd64 build apiproject build success exclude prefix: exclude suffix: .go:.DS_Store:.tmp file write to `/gopath/src/apiproject/apiproject.tar.gz`
我們可以看到目錄下有如下的壓縮文件:
rwxr-xr-x 1 astaxie staff 8995376 11 25 22:46 apiproject -rw-r--r-- 1 astaxie staff 2240288 11 25 22:58 apiproject.tar.gz drwxr-xr-x 3 astaxie staff 102 11 25 22:31 conf drwxr-xr-x 3 astaxie staff 102 11 25 22:31 controllers -rw-r--r-- 1 astaxie staff 509 11 25 22:31 main.go drwxr-xr-x 3 astaxie staff 102 11 25 22:31 models drwxr-xr-x 3 astaxie staff 102 11 25 22:31 tests
bale
命令
這個命令目前僅限內部使用,具體實現方案未完善,主要用來壓縮所有的靜態文件變成一個變量申明文件,全部編譯到二進制文件里面,用戶發布的時候攜帶靜態文件,包括 js、css、img 和 views。最后在啟動運行時進行非覆蓋式的自解壓。
version
命令
這個命令是動態獲取 bee、beego 和 Go 的版本,這樣一旦用戶出現錯誤,可以通過該命令來查看當前的版本
$ bee version bee :1.2.2 beego :1.4.2 Go :go version go1.3.3 darwin/amd64
generate
命令
這個命令是用來自動化的生成代碼的,包含了從數據庫一鍵生成 model,還包含了 scaffold 的,通過這個命令,讓大家開發代碼不再慢
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