bee工具的使用

2023-11-20 15:58 更新

bee 工具簡(jiǎn)介

bee 工具是一個(gè)為了協(xié)助快速開(kāi)發(fā) beego 項(xiàng)目而創(chuàng)建的項(xiàng)目,通過(guò) bee 您可以很容易的進(jìn)行 beego 項(xiàng)目的創(chuàng)建、熱編譯、開(kāi)發(fā)、測(cè)試、和部署。

bee 工具的安裝

您可以通過(guò)如下的方式安裝 bee 工具:

go get github.com/beego/bee

安裝完之后,bee 可執(zhí)行文件默認(rèn)存放在 $GOPATH/bin 里面,所以您需要把 $GOPATH/bin 添加到您的環(huán)境變量中,才可以進(jìn)行下一步。

如何添加環(huán)境變量,請(qǐng)自行搜索 如果你本機(jī)設(shè)置了 GOBIN,那么上面的命令就會(huì)安裝到 GOBIN 下,請(qǐng)?zhí)砑?GOBIN 到你的環(huán)境變量中

bee 工具命令詳解

我們?cè)诿钚休斎?nbsp;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
    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.

new 命令

new 命令是新建一個(gè) Web 項(xiàng)目,我們?cè)诿钚邢聢?zhí)行 bee new <項(xiàng)目名> 就可以創(chuàng)建一個(gè)新的項(xiàng)目。但是注意該命令必須在 $GOPATH/src 下執(zhí)行。最后會(huì)在 $GOPATH/src 相應(yīng)目錄下生成如下目錄結(jié)構(gòu)的項(xiàng)目:

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 命令是用來(lái)新建 Web 項(xiàng)目,不過(guò)很多用戶使用 beego 來(lái)開(kāi)發(fā) API 應(yīng)用。所以這個(gè) api 命令就是用來(lái)創(chuàng)建 API 應(yīng)用的,執(zhí)行命令之后如下所示:

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

這個(gè)項(xiàng)目的目錄結(jié)構(gòu)如下:

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 項(xiàng)目相比,少了 static 和 views 目錄,多了一個(gè) test 模塊,用來(lái)做單元測(cè)試的。

同時(shí),該命令還支持一些自定義參數(shù)自動(dòng)連接數(shù)據(jù)庫(kù)創(chuàng)建相關(guān) model 和 controller: bee api [appname] [-tables=""] [-driver=mysql] [-conn="root:<password>@tcp(127.0.0.1:3306)/test"] 如果 conn 參數(shù)為空則創(chuàng)建一個(gè)示例項(xiàng)目,否則將基于鏈接信息鏈接數(shù)據(jù)庫(kù)創(chuàng)建項(xiàng)目。

run 命令

我們?cè)陂_(kāi)發(fā) Go 項(xiàng)目的時(shí)候最大的問(wèn)題是經(jīng)常需要自己手動(dòng)去編譯再運(yùn)行,bee run 命令是監(jiān)控 beego 的項(xiàng)目,通過(guò) fsnotify監(jiān)控文件系統(tǒng)。但是注意該命令必須在 $GOPATH/src/appname 下執(zhí)行。 這樣我們?cè)陂_(kāi)發(fā)過(guò)程中就可以實(shí)時(shí)的看到項(xiàng)目修改之后的效果:

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...

我們打開(kāi)瀏覽器就可以看到效果 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...

刷新瀏覽器我們看到新的修改內(nèi)容已經(jīng)輸出。

pack 命令

pack 目錄用來(lái)發(fā)布應(yīng)用的時(shí)候打包,會(huì)把項(xiàng)目打包成 zip 包,這樣我們部署的時(shí)候直接把打包之后的項(xiàng)目上傳,解壓就可以部署了:

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 命令

這個(gè)命令目前僅限內(nèi)部使用,具體實(shí)現(xiàn)方案未完善,主要用來(lái)壓縮所有的靜態(tài)文件變成一個(gè)變量申明文件,全部編譯到二進(jìn)制文件里面,用戶發(fā)布的時(shí)候攜帶靜態(tài)文件,包括 js、css、img 和 views。最后在啟動(dòng)運(yùn)行時(shí)進(jìn)行非覆蓋式的自解壓。

version 命令

這個(gè)命令是動(dòng)態(tài)獲取 bee、beego 和 Go 的版本,這樣一旦用戶出現(xiàn)錯(cuò)誤,可以通過(guò)該命令來(lái)查看當(dāng)前的版本

$ bee version
bee   :1.2.2
beego :1.4.2
Go    :go version go1.3.3 darwin/amd64

generate 命令

這個(gè)命令是用來(lái)自動(dòng)化的生成代碼的,包含了從數(shù)據(jù)庫(kù)一鍵生成 model,還包含了 scaffold 的,通過(guò)這個(gè)命令,讓大家開(kāi)發(fā)代碼不再慢

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

migrate 命令

這個(gè)命令是應(yīng)用的數(shù)據(jù)庫(kù)遷移命令,主要是用來(lái)每次應(yīng)用升級(jí),降級(jí)的SQL管理。

bee migrate [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
    run all outstanding migrations
    -driver: [mysql | postgresql | sqlite], the default is mysql
    -conn:   the connection string used by the driver, the default is root:@tcp(127.0.0.1:3306)/test

bee migrate rollback [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
    rollback the last migration operation
    -driver: [mysql | postgresql | sqlite], the default is mysql
    -conn:   the connection string used by the driver, the default is root:@tcp(127.0.0.1:3306)/test

bee migrate reset [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
    rollback all migrations
    -driver: [mysql | postgresql | sqlite], the default is mysql
    -conn:   the connection string used by the driver, the default is root:@tcp(127.0.0.1:3306)/test

bee migrate refresh [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
    rollback all migrations and run them all again
    -driver: [mysql | postgresql | sqlite], the default is mysql
    -conn:   the connection string used by the driver, the default is root:@tcp(127.0.0.1:3306)/test

dockerize 命令

這個(gè)命令可以通過(guò)生成Dockerfile文件來(lái)實(shí)現(xiàn)docker化你的應(yīng)用。

例子:生成一個(gè)以1.6.4版本Go環(huán)境為基礎(chǔ)鏡像的Dockerfile,并暴露9000端口:

$ bee dockerize -image="library/golang:1.6.4" -expose=9000
______
| ___ \
| |_/ /  ___   ___
| ___ \ / _ \ / _ \
| |_/ /|  __/|  __/
\____/  \___| \___| v1.6.2
2016/12/26 22:34:54 INFO     ? 0001 Generating Dockerfile...
2016/12/26 22:34:54 SUCCESS  ? 0002 Dockerfile generated.

更多幫助信息可執(zhí)行bee help dockerize.

bee 工具配置文件

您可能已經(jīng)注意到,在 bee 工具的源碼目錄下有一個(gè) bee.json 文件,這個(gè)文件是針對(duì) bee 工具的一些行為進(jìn)行配置。該功能還未完全開(kāi)發(fā)完成,不過(guò)其中的一些選項(xiàng)已經(jīng)可以使用:

  • "version": 0:配置文件版本,用于對(duì)比是否發(fā)生不兼容的配置格式版本。
  • "go_install": false:如果您的包均使用完整的導(dǎo)入路徑(例如:github.com/user/repo/subpkg),則可以啟用該選項(xiàng)來(lái)進(jìn)行 go install 操作,加快構(gòu)建操作。
  • "watch_ext": []:用于監(jiān)控其它類(lèi)型的文件(默認(rèn)只監(jiān)控后綴為 .go 的文件)。
  • "dir_structure":{}:如果您的目錄名與默認(rèn)的 MVC 架構(gòu)的不同,則可以使用該選項(xiàng)進(jìn)行修改。
  • "cmd_args": []:如果您需要在每次啟動(dòng)時(shí)加入啟動(dòng)參數(shù),則可以使用該選項(xiàng)。
  • "envs": []:如果您需要在每次啟動(dòng)時(shí)設(shè)置臨時(shí)環(huán)境變量參數(shù),則可以使用該選項(xiàng)。
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)