(二)Beego框架之bee工具

一、bee工具的简介

bee工具是一个为了协助快速开发beego项目而创建的项目,通过bee您可以很容易的进行beego项目的创建、热编译、开发、测试和部署。

1.1 工具安装

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

安装完之后, bee 可执行文件默认存放在 $GOPATH/bin 里面,所以您需要把$GOPATH/bin 添加到您的环境变量中,才可以进行下一步。

如何添加环境变量,请自行搜索 如果你本机设置了 GOBIN ,那么上面的命令就会安装到GOBIN 下,请添加 GOBIN 到你的环境变量中

二、bee工具命令详解

我们在命令行输入bee,可以看到如下的信息:

Bee is a Fast and Flexible tool for managing your Beego Web Application.

You are using bee for beego v2.x. If you are working on beego v1.x, please downgrade version to bee v1.12.0

USAGE
    bee command [arguments]

AVAILABLE COMMANDS

    version     Prints the current Bee version
    migrate     Runs database migrations
    api         Creates a Beego API application
    bale        Transforms non-Go files to Go source files
    fix         Fixes your application by making it compatible with newer versions of Beego
    pro         Source code generator
    dev         Commands which used to help to develop beego and bee
    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
    update      Update Bee

Use bee help [command] for more information about a command.

2.1 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!
tizi365
├── conf            - 配置文件存放目录
│   └── app.conf    - beego应用配置文件,里面包含一些默认的配置包括启动端口、运行模式等等
├── controllers     - 控制器目录
│   └── default.go
├── main.go         - 入口文件
├── models          - model目录,存放我们的业务逻辑和数据库相关的操作
├── routers         - 路由配置目录,主要存放我们各个业务模块的路由设置
│   └── router.go
├── static          - 静态资源目录,默认静态资源访问url为 "http://域名/static/资源路径"
│   ├── css
│   ├── img
│   └── js
├── tests           - 单元测试脚本目录
│   └── default_test.go
└── views           - 视图模板目录
    └── index.tpl

2.2 api命令

上面的 new 命令是用来新建 Web 项目,不过很多用户使用 beego 来开发 API 应用。所以这个 api 命令就是用来创建 API 应用的,执行命令之后如下所示:

# bee api apiProject
| ___ \
| |_/ /  ___   ___
| ___ \ / _ \ / _ \
| |_/ /|  __/|  __/
\____/  \___| \___| v2.0.2
2022/05/07 16:12:14 INFO     ▶ 0001 generate api project support go modules.
2022/05/07 16:12:14 INFO     ▶ 0002 Creating API...
        create   /home/ubuntu/go/src/githud.com/infodriven/apiProject/go.mod
        create   /home/ubuntu/go/src/githud.com/infodriven/apiProject
        create   /home/ubuntu/go/src/githud.com/infodriven/apiProject/conf
        create   /home/ubuntu/go/src/githud.com/infodriven/apiProject/controllers
        create   /home/ubuntu/go/src/githud.com/infodriven/apiProject/tests
        create   /home/ubuntu/go/src/githud.com/infodriven/apiProject/conf/app.conf
        create   /home/ubuntu/go/src/githud.com/infodriven/apiProject/models
        create   /home/ubuntu/go/src/githud.com/infodriven/apiProject/routers/
        create   /home/ubuntu/go/src/githud.com/infodriven/apiProject/controllers/object.go
        create   /home/ubuntu/go/src/githud.com/infodriven/apiProject/controllers/user.go
        create   /home/ubuntu/go/src/githud.com/infodriven/apiProject/tests/default_test.go
        create   /home/ubuntu/go/src/githud.com/infodriven/apiProject/routers/router.go
        create   /home/ubuntu/go/src/githud.com/infodriven/apiProject/models/object.go
        create   /home/ubuntu/go/src/githud.com/infodriven/apiProject/models/user.go
        create   /home/ubuntu/go/src/githud.com/infodriven/apiProject/main.go
2022/05/07 16:12:14 SUCCESS  ▶ 0003 New API successfully created!
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:@tcp(127.0.0.1:3306)/test"] 如果conn 参数为空则创建一个示例项目,否则将基于链接信息链接数据库创建项目。

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

刷新浏览器我们看到新的修改内容已经输出。

2.4 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`

我们可以看到目录下有如下的压缩文件:

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

2.5 bale 命令

这个命令目前仅限内部使用,具体实现方案未完善,主要用来压缩所有的静态文件变成一个变量申明文
件,全部编译到二进制文件里面,用户发布的时候携带静态文件,包括 js、css、img 和 views。
最后在启动运行时进行非覆盖式的自解压。

2.6 version 命令

这个命令是动态获取 bee、beego 和 Go 的版本,这样一旦用户出现错误,可以通过该命令来查看当前的版本

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

2.7 generate 命令

这个命令是用来自动化的生成代码的,包含了从数据库一键生成 model,还包含了 scaffold 的,通过这个命令,让大家开发代码不再慢

bee generate scaffold [scaffoldname] [-fields=""] [-driver=mysql] [-
1.  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
-conn: the connection string used by the driver, the default is
5.  root:@tcp(127.0.0.1:3306)/test
6.  example: bee generate scaffold post -fields="title:string,body:text"
7.
8.  bee generate model [modelname] [-fields=""]
9.  generate RESTful model based on fields
10.  -fields: a list of table fields. Format: field:type, ...
11.
12.  bee generate controller [controllerfile]
13.  generate RESTful controllers
14.
15.  bee generate view [viewpath]
16.  generate CRUD view in viewpath
17.
18.  bee generate migration [migrationfile] [-fields=""]
19.  generate migration file for making database schema update
20.  -fields: a list of table fields. Format: field:type, ...
21.
22.  bee generate docs
23.  generate swagger doc file
24.
25.  bee generate test [routerfile]
26.  generate testcase
27.
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

2.8 migrate 命令

这个命令是应用的数据库迁移命令,主要是用来每次应用升级,降级的SQL管理。

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.
6.  bee migrate rollback [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
7.  rollback the last migration operation
8.  -driver: [mysql | postgresql | sqlite], the default is mysql
9.
-conn: the connection string used by the driver, the default is
root:@tcp(127.0.0.1:3306)/test
10.
11.  bee migrate reset [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
12.  rollback all migrations
13.  -driver: [mysql | postgresql | sqlite], the default is mysql
14.
-conn: the connection string used by the driver, the default is
root:@tcp(127.0.0.1:3306)/test
15.
16.  bee migrate refresh [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
17.  rollback all migrations and run them all again
18.  -driver: [mysql | postgresql | sqlite], the default is mysql
19.
-conn: the connection string used by the driver, the default is
root:@tcp(127.0.0.1:3306)/test

2.9 dockerize 命令

这个命令可以通过生成Dockerfile文件来实现docker化你的应用。
例子:
生成一个以1.6.4版本Go环境为基础镜像的Dockerfile,并暴露9000端口:

1.  $ bee dockerize -image="library/golang:1.6.4" -expose=9000
2.  ______
3.  | ___ \
4.  | |_/ / ___ ___
5.  | ___ \ / _ \ / _ \
6.  | |_/ /| __/| __/
7.  \____/ \___| \___| v1.6.2
8.  2016/12/26 22:34:54 INFO ▶ 0001 Generating Dockerfile...
9.  2016/12/26 22:34:54 SUCCESS ▶ 0002 Dockerfile generated.

更多帮助信息可执行 bee help dockerize .

三、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": [] :如果您需要在每次启动时设置临时环境变量参数,则可以使用该选项。
posted @ 2022-05-07 16:29  比特边界  阅读(714)  评论(0编辑  收藏  举报