name: bee tool usage
sort: 2
Introduction to bee tool
Bee tool is a project for rapid Beego development. With bee tool developers can create, auto compile and reload, develop, test, and deploy Beego applications quickly and easily.
Installing bee tool
Install bee tool with the following command:
go get github.com/beego/bee/v2
Update the bee tool with the following command:
go get -u github.com/beego/bee/v2
bee is installed into GOPATH/bin by default. You need to add GOPATH/bin to your PATH, otherwise the bee command won’t work.
bee tool commands
Type bee in command line and the following messages with be displayed:
bee is a tool for managing Beego framework.Usage:bee command [arguments]The commands are:new Create a Beego applicationrun run the app and start a Web server for developmentpack Compress a Beego project into a single fileapi create an API Beego applicationbale packs non-Go files to Go source filesversion show the bee, Beego and Go versiongenerate source code generatormigrate run database migrations
Command new
The new command can create a new web project. You can create a new Beego project by typing bee new <project name> under $GOPATH/src. This will generate all the default project folders and files:
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
Command api
The new command is used for crafting new web applications. The api command is used to create new API applications.
Here is the result of running bee api project_name:
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
Below is the generated project structure of a new API application:
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
Compare this to the bee new myproject command seen earlier.
Note that the new API application doesn’t have a static and views folder.
You can also create a model and controller based on the database schema by providing database conn:
bee api [appname] [-tables=""] [-driver=mysql] [-conn=root:@tcp(127.0.0.1:3306)/test]
Command run
The bee run command will supervise the file system of any Beego project using inotify. The results will autocompile and display immediately after any modification in the Beego project folders.
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 successful13-11-25 09:53:16 [INFO] Restarting myproject ...13-11-25 09:53:16 [INFO] ./myproject is running...
Visting http://localhost:8080/ with a web browser will display your app running:

After modifying the default.go file in the controllers folder, the following output will be displayed in the command line:
13-11-25 10:11:20 [EVEN] "/gopath/src/myproject/controllers/default.go": DELETE|MODIFY13-11-25 10:11:20 [INFO] Start building...13-11-25 10:11:20 [SKIP] "/gopath/src/myproject/controllers/default.go": CREATE13-11-25 10:11:23 [SKIP] "/gopath/src/myproject/controllers/default.go": MODIFY13-11-25 10:11:23 [SUCC] Build was successful13-11-25 10:11:23 [INFO] Restarting myproject ...13-11-25 10:11:23 [INFO] ./myproject is running...
Refresh the browser to show the results of the new modifications.
Command pack
The pack command is used to compress the project into a single file. The compressed file can be deployed by uploading and extracting the zip file to the server.
bee packapp path: /gopath/src/apiprojectGOOS darwin GOARCH amd64build apiprojectbuild successexclude prefix:exclude suffix: .go:.DS_Store:.tmpfile write to `/gopath/src/apiproject/apiproject.tar.gz`
The compressed file will be in the project folder:
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.gzdrwxr-xr-x 3 astaxie staff 102 11 25 22:31 confdrwxr-xr-x 3 astaxie staff 102 11 25 22:31 controllers-rw-r--r-- 1 astaxie staff 509 11 25 22:31 main.godrwxr-xr-x 3 astaxie staff 102 11 25 22:31 modelsdrwxr-xr-x 3 astaxie staff 102 11 25 22:31 tests
Command bale
This command is currently only available to the developer team. It is used to compress all static files in to a single binary file so that they do not need to carry static files including js, css, images and views when publishing the project. Those files will be self-extracting with non-overwrite when the program starts.
Command version
This command displays the version of bee, beego, and go.
$ bee versionbee :1.2.2Beego :1.4.2Go :go version go1.3.3 darwin/amd64
This command try to output beego’s version. It works well for GOPATH mode. Bee finds beego’s version from $GOPATH/src/astaxie/beego directory.
So when we use GOMOD mode, and we don’t download beego’s source code, Bee could not find the version’s information.
Command generate
This command will generate the routers by analyzing the functions in controllers.
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 routers [-ctrlDir=/path/to/controller/directory] [-routersFile=/path/to/routers/file.go] [-routersPkg=myPackage]-ctrlDir: the directory contains controllers definition. Bee scans this directory and its subdirectory to generate routers info-routersFile: output file. All generated routers info will be output into this file.If file not found, Bee create new one, or Bee truncates it.The default value is "routers/commentRouters.go"-routersPkg: package declaration.The default value is "routers".When you pass routersFile parameter, you'd better pass this parameterbee 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
Command migrate
This command will run database migration scripts.
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)/testbee 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)/testbee 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)/testbee 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
bee tool configuration
The file bee.json in the bee tool source code folder is the Beego configuration file. This file is still under development, but some options are already available to use:
"version": 0: version of file, for checking incompatible format version."go_install": false: if you use a full import path likegithub.com/user/repo/subpkgyou can enable this option to rungo installand speed up you build processes."watch_ext": []: add other file extensions to watch (only watch.gofiles by default). For example,.ini,.conf, etc."dir_structure":{}: if your folder names are not the same as MVC classic names you can use this option to change them."cmd_args": []: add command arguments for every start."envs": []: set environment variables for every start.
