标签:信息 相关 compress arch 定义 empty base git win
beego我认为是go初学者比较容易上手的一门MVC Web框架。简单易懂,最重要的一点就是提供了中文文档,这对于我这种英语能力比较差的人来说就是福音。
beego的官网上是这么介绍beego的:“beego 是一个快速开发 Go 应用的 HTTP 框架,他可以用来快速开发 API、Web 及后端服务等各种应用,是一个 RESTful 的框架,主要设计灵感来源于 tornado、sinatra 和 flask 这三个框架,但是结合了 Go 本身的一些特性(interface、struct 嵌入等)而设计的一个框架”。从中可以看出beego可以快速开发API接口、可以实现服务端的各种应用
beego官网 : https://beego.me/
github : https://github.com/beego
beego 包含一些示例应用程序以帮您学习并使用 beego 应用框架。
您需要安装 Go 1.1+ 以确保所有功能的正常使用。
你需要安装 Beego 和 Bee 的开发工具:
$ go get github.com/astaxie/beego
$ go get github.com/beego/bee
注意:
安装完成Bee后,在控制台输入bee
Bee is a tool for managing beego framework. Usage: bee command [arguments] The commands are: new Create a Beego application run run the app and start a Web server for development pack Compress a beego project into a single file api create an API beego application hprose create an rpc application use hprose base on beego framework bale packs non-Go files to Go source files version prints the current Bee version generate source code generator migrate run database migrations fix fix the beego application to make it compatible with beego 1.6 Use "bee help [command]" for more information about a command. Additional help topics: Use "bee help [topic]" for more information about that topic.
主要有以上10个命令,下面说几个常用的命令
new
命令是新建一个 Web 项目,我们在命令行下执行 bee new <项目名>
就可以创建一个新的项目。但是注意该命令必须在 $GOPATH/src
下执行。最后会在 $GOPATH/src
相应目录下生成如下目录结构的项目:
bogon:src zuxingyu$ bee new cnblogs ______ | ___ | |_/ / ___ ___ | ___ \ / _ \ / _ | |_/ /| __/| __/ \____/ \___| \___| v1.5.2 2016/10/31 22:16:11 [INFO] Creating application... create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/ create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/conf/ create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/controllers/ create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/models/ create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/routers/ create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/tests/ create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/static/ create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/static/js/ create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/static/css/ create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/static/img/ create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/views/ create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/conf/app.conf create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/controllers/default.go create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/views/index.tpl create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/routers/router.go create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/tests/default_test.go create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/main.go 2016/10/31 22:16:11 [SUCC] New application successfully created!
这样一个beego框架的Go项目就生成了
上面的 new
命令是用来新建 Web 项目,不过很多用户使用 beego 来开发 API 应用。所以这个 api
命令就是用来创建 API 应用的,属于纯服务端业务。执行命令之后如下所示:
bogon:src zuxingyu$ bee api cnblogsApi ______ | ___ | |_/ / ___ ___ | ___ \ / _ \ / _ | |_/ /| __/| __/ \____/ \___| \___| v1.5.2 2016/10/31 22:19:51 [INFO] Creating API... create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/conf create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/controllers create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/tests create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/conf/app.conf create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/models create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/routers/ create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/controllers/object.go create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/controllers/user.go create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/tests/default_test.go create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/routers/router.go create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/models/object.go create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/models/user.go create /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/main.go 2016/10/31 22:19:51 [SUCC] New API successfully created!
从上面的目录我们可以看到和 Web 项目相比,少了 static 和 views 目录,多了一个 test 模块,用来做单元测试的。
同时,该命令还支持一些自定义参数自动连接数据库创建相关model和controller:
bee api [appname] [-tables=“”] [-driver=mysql] [-conn=root:@tcp(127.0.0.1:3306)/test]
如果conn参数为空则创建一个示例项目,否则将基于链接信息链接数据库创建项目。
bee run
命令是监控 beego 的项目,通过 fsnotify监控文件系统。但是注意该命令必须在$GOPATH/src/appname
下执行。
bogon:src zuxingyu$ cd cnblogs bogon:cnblogs zuxingyu$ bee run ______ | ___ | |_/ / ___ ___ | ___ \ / _ \ / _ | |_/ /| __/| __/ \____/ \___| \___| v1.5.2 2016/10/31 22:22:30 [INFO] Using ‘cnblogs‘ as ‘appname‘ 2016/10/31 22:22:30 [INFO] Initializing watcher... 2016/10/31 22:22:30 [TRAC] Directory(/Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/controllers) 2016/10/31 22:22:30 [TRAC] Directory(/Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs) 2016/10/31 22:22:30 [TRAC] Directory(/Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/routers) 2016/10/31 22:22:30 [TRAC] Directory(/Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/tests) 2016/10/31 22:22:30 [INFO] Start building... 2016/10/31 22:22:34 [SUCC] Build was successful 2016/10/31 22:22:34 [INFO] Restarting cnblogs ... 2016/10/31 22:22:34 [INFO] ./cnblogs is running... 2016/10/31 22:22:34 [I] [asm_amd64.s:2086] http server Running on http://:8080
注意:Linux/Mac Os 在命令行运行时,结束直接用Ctrl + C 结束
pack
目录用来发布应用的时候打包,会把项目打包成 zip 包,这样我们部署的时候直接把打包之后的项目上传,解压就可以部署了。可能在后面工作中会使用Docker去做容器。具体的在后面使用后告诉大家。
bogon:cnblogs zuxingyu$ bee pack ______ | ___ | |_/ / ___ ___ | ___ \ / _ \ / _ | |_/ /| __/| __/ \____/ \___| \___| v1.5.2 2016/10/31 22:28:03 Packaging application: /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs 2016/10/31 22:28:03 Building application... 2016/10/31 22:28:03 Env: GOOS=darwin GOARCH=amd64 2016/10/31 22:28:06 Build successful 2016/10/31 22:28:06 Excluding relpath prefix: . 2016/10/31 22:28:06 Excluding relpath suffix: .go:.DS_Store:.tmp 2016/10/31 22:28:07 Writing to output: `/Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/cnblogs.tar.gz`
使用命令后会打出一个tar.gz包。放入Linux/Mac OS 解压即可。
这个命令是动态获取bee、beego和Go的版本,这样一旦用户出现错误,可以通过该命令来查看当前的版本,没啥需要解释的。只是查看下当前版本。
bogon:cnblogs zuxingyu$ bee version ______ | ___ | |_/ / ___ ___ | ___ \ / _ \ / _ | |_/ /| __/| __/ \____/ \___| \___| v1.5.2 ├── Beego : 1.7.1 ├── GoVersion : go1.7.1 ├── GOOS : darwin ├── GOARCH : amd64 ├── NumCPU : 4 ├── GOPATH : /Users/zuxingyu/Documents/GoWorkSpace ├── GOROOT : /usr/local/go ├── Compiler : gc └── Date : Monday, 31 Oct 2016
这是一个重要的命令,尤其是生成api项目时,generate docs可以生成swagger的文档。
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
这些都是我目前常用的命令。如果有其他命令以后可以开个专题专门实验这些命令。
标签:信息 相关 compress arch 定义 empty base git win
原文地址:http://www.cnblogs.com/zuxingyu/p/6016816.html