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@latest

Update the bee tool with the following command:

go get -u github.com/beego/bee/v2@latest

bee is installed into GOPATH/bin by default. You need to add GOPATH/bin to your PATH, otherwise the bee command won’t work.

More details in bee installation

bee tool commands

Type bee in command line and the following messages with be displayed:

  1. bee is a tool for managing Beego framework.
  2. Usage:
  3. bee command [arguments]
  4. The commands are:
  5. new Create a Beego application
  6. run run the app and start a Web server for development
  7. pack Compress a Beego project into a single file
  8. api create an API Beego application
  9. bale packs non-Go files to Go source files
  10. version show the bee, Beego and Go version
  11. generate source code generator
  12. migrate 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:

  1. bee new myproject
  2. [INFO] Creating application...
  3. /gopath/src/myproject/
  4. /gopath/src/myproject/conf/
  5. /gopath/src/myproject/controllers/
  6. /gopath/src/myproject/models/
  7. /gopath/src/myproject/static/
  8. /gopath/src/myproject/static/js/
  9. /gopath/src/myproject/static/css/
  10. /gopath/src/myproject/static/img/
  11. /gopath/src/myproject/views/
  12. /gopath/src/myproject/conf/app.conf
  13. /gopath/src/myproject/controllers/default.go
  14. /gopath/src/myproject/views/index.tpl
  15. /gopath/src/myproject/main.go
  16. 13-11-25 09:50:39 [SUCC] New application successfully created!
  1. myproject
  2. ├── conf
  3. └── app.conf
  4. ├── controllers
  5. └── default.go
  6. ├── main.go
  7. ├── models
  8. ├── routers
  9. └── router.go
  10. ├── static
  11. ├── css
  12. ├── img
  13. └── js
  14. ├── tests
  15. └── default_test.go
  16. └── views
  17. └── index.tpl
  18. 8 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:

  1. bee api apiproject
  2. create app folder: /gopath/src/apiproject
  3. create conf: /gopath/src/apiproject/conf
  4. create controllers: /gopath/src/apiproject/controllers
  5. create models: /gopath/src/apiproject/models
  6. create tests: /gopath/src/apiproject/tests
  7. create conf app.conf: /gopath/src/apiproject/conf/app.conf
  8. create controllers default.go: /gopath/src/apiproject/controllers/default.go
  9. create tests default.go: /gopath/src/apiproject/tests/default_test.go
  10. create models object.go: /gopath/src/apiproject/models/object.go
  11. create main.go: /gopath/src/apiproject/main.go

Below is the generated project structure of a new API application:

  1. apiproject
  2. ├── conf
  3. └── app.conf
  4. ├── controllers
  5. └── object.go
  6. └── user.go
  7. ├── docs
  8. └── doc.go
  9. ├── main.go
  10. ├── models
  11. └── object.go
  12. └── user.go
  13. ├── routers
  14. └── router.go
  15. └── tests
  16. └── 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.

  1. 13-11-25 09:53:04 [INFO] Uses 'myproject' as 'appname'
  2. 13-11-25 09:53:04 [INFO] Initializing watcher...
  3. 13-11-25 09:53:04 [TRAC] Directory(/gopath/src/myproject/controllers)
  4. 13-11-25 09:53:04 [TRAC] Directory(/gopath/src/myproject/models)
  5. 13-11-25 09:53:04 [TRAC] Directory(/gopath/src/myproject)
  6. 13-11-25 09:53:04 [INFO] Start building...
  7. 13-11-25 09:53:16 [SUCC] Build was successful
  8. 13-11-25 09:53:16 [INFO] Restarting myproject ...
  9. 13-11-25 09:53:16 [INFO] ./myproject is running...

Visting http://localhost:8080/ with a web browser will display your app running:

Introduction to bee tool - 图1

After modifying the default.go file in the controllers folder, the following output will be displayed in the command line:

  1. 13-11-25 10:11:20 [EVEN] "/gopath/src/myproject/controllers/default.go": DELETE|MODIFY
  2. 13-11-25 10:11:20 [INFO] Start building...
  3. 13-11-25 10:11:20 [SKIP] "/gopath/src/myproject/controllers/default.go": CREATE
  4. 13-11-25 10:11:23 [SKIP] "/gopath/src/myproject/controllers/default.go": MODIFY
  5. 13-11-25 10:11:23 [SUCC] Build was successful
  6. 13-11-25 10:11:23 [INFO] Restarting myproject ...
  7. 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.

  1. bee pack
  2. app path: /gopath/src/apiproject
  3. GOOS darwin GOARCH amd64
  4. build apiproject
  5. build success
  6. exclude prefix:
  7. exclude suffix: .go:.DS_Store:.tmp
  8. file write to `/gopath/src/apiproject/apiproject.tar.gz`

The compressed file will be in the project folder:

  1. rwxr-xr-x 1 astaxie staff 8995376 11 25 22:46 apiproject
  2. -rw-r--r-- 1 astaxie staff 2240288 11 25 22:58 apiproject.tar.gz
  3. drwxr-xr-x 3 astaxie staff 102 11 25 22:31 conf
  4. drwxr-xr-x 3 astaxie staff 102 11 25 22:31 controllers
  5. -rw-r--r-- 1 astaxie staff 509 11 25 22:31 main.go
  6. drwxr-xr-x 3 astaxie staff 102 11 25 22:31 models
  7. drwxr-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.

  1. $ bee version
  2. bee :1.2.2
  3. Beego :1.4.2
  4. Go :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.

  1. bee generate scaffold [scaffoldname] [-fields=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
  2. The generate scaffold command will do a number of things for you.
  3. -fields: a list of table fields. Format: field:type, ...
  4. -driver: [mysql | postgres | sqlite], the default is mysql
  5. -conn: the connection string used by the driver, the default is root:@tcp(127.0.0.1:3306)/test
  6. example: bee generate scaffold post -fields="title:string,body:text"
  7. bee generate model [modelname] [-fields=""]
  8. generate RESTful model based on fields
  9. -fields: a list of table fields. Format: field:type, ...
  10. bee generate controller [controllerfile]
  11. generate RESTful controllers
  12. bee generate view [viewpath]
  13. generate CRUD view in viewpath
  14. bee generate migration [migrationfile] [-fields=""]
  15. generate migration file for making database schema update
  16. -fields: a list of table fields. Format: field:type, ...
  17. bee generate docs
  18. generate swagger doc file
  19. bee generate routers [-ctrlDir=/path/to/controller/directory] [-routersFile=/path/to/routers/file.go] [-routersPkg=myPackage]
  20. -ctrlDir: the directory contains controllers definition. Bee scans this directory and its subdirectory to generate routers info
  21. -routersFile: output file. All generated routers info will be output into this file.
  22. If file not found, Bee create new one, or Bee truncates it.
  23. The default value is "routers/commentRouters.go"
  24. -routersPkg: package declaration.The default value is "routers".
  25. When you pass routersFile parameter, you'd better pass this parameter
  26. bee generate test [routerfile]
  27. generate testcase
  28. bee generate appcode [-tables=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] [-level=3]
  29. generate appcode based on an existing database
  30. -tables: a list of table names separated by ',', default is empty, indicating all tables
  31. -driver: [mysql | postgres | sqlite], the default is mysql
  32. -conn: the connection string used by the driver.
  33. default for mysql: root:@tcp(127.0.0.1:3306)/test
  34. default for postgres: postgres://postgres:postgres@127.0.0.1:5432/postgres
  35. -level: [1 | 2 | 3], 1 = models; 2 = models,controllers; 3 = models,controllers,router

Command migrate

This command will run database migration scripts.

  1. bee migrate [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
  2. run all outstanding migrations
  3. -driver: [mysql | postgresql | sqlite], the default is mysql
  4. -conn: the connection string used by the driver, the default is root:@tcp(127.0.0.1:3306)/test
  5. bee migrate rollback [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
  6. rollback the last migration operation
  7. -driver: [mysql | postgresql | sqlite], the default is mysql
  8. -conn: the connection string used by the driver, the default is root:@tcp(127.0.0.1:3306)/test
  9. bee migrate reset [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
  10. rollback all migrations
  11. -driver: [mysql | postgresql | sqlite], the default is mysql
  12. -conn: the connection string used by the driver, the default is root:@tcp(127.0.0.1:3306)/test
  13. bee migrate refresh [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
  14. rollback all migrations and run them all again
  15. -driver: [mysql | postgresql | sqlite], the default is mysql
  16. -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 like github.com/user/repo/subpkg you can enable this option to run go install and speed up you build processes.
  • "watch_ext": []: add other file extensions to watch (only watch .go files 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.