版本、IDE
- Go: 1.19
- beego:2.0.4
- Goland:2021
创建bee-blog
通过 bee命令来创建beego 的项目,所以在创建项目之前确保你已经安装了 bee 工具和 beego。如果你还没有安装,那么请查阅beego 的安装 和 bee工具的安装。
打开终端,进入 E:\maizi80
目录运行命令:
$ bee new bee-blog
2022/10/24 14:36:03.680 [D] init global config instance failed. If you do not use this, just ignore it. open conf/app.conf: The system cannot find the path specified.
2022/10/24 14:36:03 INFO ▶ 0001 Generate new project support go modules.
2022/10/24 14:36:03 INFO ▶ 0002 Creating application...
create E:\maizi80\bee-blog\go.mod
create E:\maizi80\bee-blog\
create E:\maizi80\bee-blog\conf\
create E:\maizi80\bee-blog\controllers\
create E:\maizi80\bee-blog\models\
create E:\maizi80\bee-blog\routers\
create E:\maizi80\bee-blog\tests\
create E:\maizi80\bee-blog\static\
create E:\maizi80\bee-blog\static\js\
create E:\maizi80\bee-blog\static\css\
create E:\maizi80\bee-blog\static\img\
create E:\maizi80\bee-blog\views\
create E:\maizi80\bee-blog\conf\app.conf
create E:\maizi80\bee-blog\controllers\default.go
create E:\maizi80\bee-blog\views\index.tpl
create E:\maizi80\bee-blog\routers\router.go
create E:\maizi80\bee-blog\tests\default_test.go
create E:\maizi80\bee-blog\main.go
2022/10/24 14:36:03 SUCCESS ▶ 0003 New application successfully created!
看到SUCCESS字样则表示项目创建成功,beego是典型的MVC架构,main.go
是框架的入口文件,项目的目录结构如下:
bee-blog
|-- conf
| `-- app.conf
|-- controllers
| `-- default.go
|-- main.go
|-- models
|-- routers
| `-- router.go
|-- static
| |-- css
| |-- img
| `-- js
|-- tests
| `-- default_test.go
`-- views
`-- index.tpl
初始化项目
运行命令go mod tidy
下载框架的各个依赖包。
$ cd bee-blog
$ go mod tidy
运行
使用Goland打开上面的项目,运行命令bee run
运行项目,该命令运行是热编译的效果,修改文件之后自动更新,而不需要重新运行命令。
______
| ___ \
| |_/ / ___ ___
| ___ \ / _ \ / _ \
| |_/ /| __/| __/
\____/ \___| \___| v2.0.4
2022/10/24 14:41:51 WARN ▶ 0001 Running application outside of GOPATH
2022/10/24 14:41:51 INFO ▶ 0002 Using 'bee-blog' as 'appname'
2022/10/24 14:41:51 INFO ▶ 0003 Initializing watcher...
.
.
.
bee-blog/controllers
bee-blog/routers
bee-blog
2022/10/24 14:42:14 SUCCESS ▶ 0004 Built Successfully!
2022/10/24 14:42:14 INFO ▶ 0005 Restarting 'bee-blog.exe'...
2022/10/24 14:42:14 SUCCESS ▶ 0006 './bee-blog.exe' is running...
2022/10/24 14:42:18.116 [I] [parser.go:413] generate router from comments
2022/10/24 14:42:18.152 [I] [server.go:241] http server Running on http://:8080
看到以上输出即表示应用项目已经在8080端口运行了,Go 已经做了网络层的东西,beego 封装了一下,可以做到不需要 nginx 和 apache即可运行网站项目。 在浏览器输入网址http://localhost:8080
即可看到应用项目的欢迎页[插入图片]
修改配置文件
打开框架配置文件conf/app.conf
,用以下的内容覆盖原来的
appname = bee-blog
runmode = dev
[dev]
httpport = 8080
[prod]
httpport = 8080
[test]
httpport = 8888
appname
应用名称,默认是beego通过bee new
创建的是创建的项目名。runmode
应用的运行模式,可选值为prod
,dev
或者test
. 默认是dev
, 为开发模式,在开发模式下出错会提示友好的出错页面,如前面错误描述中所述。- 配置文件在不同的
runmode
下解析不同的配置,例如在dev
模式下,httpport
是 8080,在prod
模式下是 80,在test
模式下是 8888。其他配置文件同理。解析的时候优先解析runmode
下的配置,然后解析默认的配置。
加入bee-blog-tpl模板
由于教程的篇幅限制,本教程不涉及view
层的知识,所以直接使用已写好的tpl
模板文件,感兴趣的同学可以打开文件学习。已经把tpl模板放在GitHub上,可直接打开bee-blog-tpl项目链接下载或者克隆模板项目到本地,把里面的文件夹static
和views
拷贝到项目根目录,替换原来的文件和文件夹,再刷新浏览器访问http://localhost:8080
就可以看到全新的首页
提交Git代码控制
如有不需要GitHub管理可以忽略,我们把项目提交到自己的GitHub上,便于版本管理。如果还没有安装git,先下载和安装git。先在GitHub上新建一个名字为bee-blog
的项目点击创建项目,然后将代码推送上去。
新建gitignore文件
有部分文件不需要上传到Git的,可以新建一个屏蔽文件屏蔽部分文件不加入版本管理,不上传到Git,在项目文件夹新建文件.gitignore
,输入信息:
bee-blog.exe*
go.sum
.idea/*
env.yaml
static/upload/*
lastupdate.tmp
在终端执行以下命令:
# 后面的命令如果没有特殊说明,都是在GoLand里的Terminal终端运行。先确保是在bee-blog项目目录里,再执行以下命令
$ git init
$ git add -A
$ git commit -m 初始化项目
$ git remote add origin git@github.com:<username>/bee-blog.git
$ git push -u origin master