bee 工具简介
bee 工具是一个为了协助快速开发 beego 项目而创建的项目,通过 bee 您可以很容易的进行 beego 项目的创建、热编译、开发、测试、和部署。
bee 工具安装
您可以通过如下的方式安装 bee 工具:
go get github.com/beego/bee
安装完之后,bee 可执行文件默认存放在 $GOPATH/bin 里面,所以您需要把 $GOPATH/bin 添加到您的环境变量中,才可以进行下一步。
bee 工具命令详解
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.go13-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.tpl8 directories, 4 files
api 命令
api 命令用来创建 API 应用的,执行命令之后如下所示:
bee api apiprojectcreate app folder: /gopath/src/apiprojectcreate conf: /gopath/src/apiproject/confcreate controllers: /gopath/src/apiproject/controllerscreate models: /gopath/src/apiproject/modelscreate tests: /gopath/src/apiproject/testscreate conf app.conf: /gopath/src/apiproject/conf/app.confcreate controllers default.go: /gopath/src/apiproject/controllers/default.gocreate tests default.go: /gopath/src/apiproject/tests/default_test.gocreate models object.go: /gopath/src/apiproject/models/object.gocreate 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
run 命令
bee run 命令是监控 beego 的项目,通过 fsnotify监控文件系统。但是注意该命令必须在 $GOPATH/src/appname 下执行。
bee run13-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 successful13-11-25 09:53:16 [INFO] Restarting myproject ...13-11-25 09:53:16 [INFO] ./myproject is running...
打开浏览器就可以看到效果 http://localhost:8080/
version 命令
动态获取 bee、beego 和 Go 的版本。
$ bee versionbee :1.2.2beego :1.4.2Go :go version go1.3.3 darwin/amd64
generate 命令
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)/testexample: 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 docsgenerate 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)/testdefault for postgres: postgres://postgres:postgres@127.0.0.1:5432/postgres-level: [1 | 2 | 3], 1 = models; 2 = models,controllers; 3 = models,controllers,router
pack 命令
pack 目录用来发布应用的时候打包,会把项目打包成 zip 包,这样我们部署的时候直接把打包之后的项目上传,解压就可以部署了:
Windows 服务器
bee pack -be GOOS=windows
Linux 服务器
bee pack -be GOOS=linux
bee 工具配置文件
在 bee 工具的源码目录下有一个 bee.json 文件,这个文件是针对 bee 工具的一些行为进行配置。该功能还未完全开发完成,不过其中的一些选项已经可以使用:
"version": 0:配置文件版本,用于对比是否发生不兼容的配置格式版本。"go_install": false:如果您的包均使用完整的导入路径(例如:github.com/user/repo/subpkg),则可以启用该选项来进行go install操作,加快构建操作。"watch_ext": []:用于监控其它类型的文件(默认只监控后缀为.go的文件)。"dir_structure":{}:如果您的目录名与默认的 MVC 架构的不同,则可以使用该选项进行修改。"cmd_args": []:如果您需要在每次启动时加入启动参数,则可以使用该选项。"envs": []:如果您需要在每次启动时设置临时环境变量参数,则可以使用该选项。
