bee工具的使用及创建第一个项目

前提文章:beego的安装及配置参数说明-CSDN博客

提示:beego框架下项目需要再GOPATH/src下进行开发,我的GOPATH是C:\Users\leell\go

一、web项目创建

通过 bee new 创建web项目

C:\Users\leell\go\src>bee new beego-web
2024/01/15 21:40:04.553 [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.
2024/01/15 21:40:04 INFO     ▶ 0001 Generate new project support go modules.
2024/01/15 21:40:04 INFO     ▶ 0002 Creating application...
        create   C:\Users\leell\go\src\beego-web\go.mod
        create   C:\Users\leell\go\src\beego-web\
        create   C:\Users\leell\go\src\beego-web\conf\
        create   C:\Users\leell\go\src\beego-web\controllers\
        create   C:\Users\leell\go\src\beego-web\models\
        create   C:\Users\leell\go\src\beego-web\routers\
        create   C:\Users\leell\go\src\beego-web\tests\
        create   C:\Users\leell\go\src\beego-web\static\
        create   C:\Users\leell\go\src\beego-web\static\js\
        create   C:\Users\leell\go\src\beego-web\static\css\
        create   C:\Users\leell\go\src\beego-web\static\img\
        create   C:\Users\leell\go\src\beego-web\views\
        create   C:\Users\leell\go\src\beego-web\conf\app.conf
        create   C:\Users\leell\go\src\beego-web\controllers\default.go
        create   C:\Users\leell\go\src\beego-web\views\index.tpl
        create   C:\Users\leell\go\src\beego-web\routers\router.go
        create   C:\Users\leell\go\src\beego-web\tests\default_test.go
        create   C:\Users\leell\go\src\beego-web\main.go
2024/01/15 21:40:04 SUCCESS  ▶ 0003 New application successfully created!

beego-web 文件结构:

beego-web
├── conf
│   └── app.conf
├── controllers
│   └── default.go
├── main.go
├── models
├── routers
│   └── router.go
├── static
│   ├── css
│   ├── img
│   └── js
├── tests
│   └── default_test.go
└── views
    └── index.tpl

app.conf : 配置文件

controllers:请求分发控制器

routers: 路由

static: css、img、js等配置文件

tests: 测试程序

views:页面

启动beego-web web服务

C:\Users\leell\go\src\beego-web>go mod init beego-web
go: C:\Users\leell\go\src\beego-web\go.mod already exists

C:\Users\leell\go\src\beego-web>go mod tidy
go: finding module for package github.com/kr/text
go: found github.com/kr/text in github.com/kr/text v0.2.0

C:\Users\leell\go\src\beego-web>bee run
______
| ___ \
| |_/ /  ___   ___
| ___ \ / _ \ / _ \
| |_/ /|  __/|  __/
\____/  \___| \___| v2.1.0
2024/01/15 21:50:11 INFO     ▶ 0001 Using 'beego-web' as 'appname'
2024/01/15 21:50:11 INFO     ▶ 0002 Initializing watcher...
cannot install, GOBIN must be an absolute path
2024/01/15 21:50:28 SUCCESS  ▶ 0003 Built Successfully!
2024/01/15 21:50:28 INFO     ▶ 0004 Restarting 'beego-web.exe'...
2024/01/15 21:50:28 SUCCESS  ▶ 0005 './beego-web.exe' is running...
2024/01/15 21:50:29.819 [I] [server.go:281]  http server Running on http://:8080

访问localhost:8080

bee工具的使用及创建第一个项目_第1张图片

二、api项目创建

通过 bee api 创建api项目

C:\Users\leell\go\src>bee api beego-api
2024/01/15 21:54:15.539 [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.
______
| ___ \
| |_/ /  ___   ___
| ___ \ / _ \ / _ \
| |_/ /|  __/|  __/
\____/  \___| \___| v2.1.0
2024/01/15 21:54:15 INFO     ▶ 0001 Generate api project support go modules.
2024/01/15 21:54:15 INFO     ▶ 0002 Creating API...
        create   C:\Users\leell\go\src\beego-api\go.mod
        create   C:\Users\leell\go\src\beego-api
        create   C:\Users\leell\go\src\beego-api\conf
        create   C:\Users\leell\go\src\beego-api\controllers
        create   C:\Users\leell\go\src\beego-api\tests
        create   C:\Users\leell\go\src\beego-api\conf\app.conf
        create   C:\Users\leell\go\src\beego-api\models
        create   C:\Users\leell\go\src\beego-api\routers\
        create   C:\Users\leell\go\src\beego-api\controllers\object.go
        create   C:\Users\leell\go\src\beego-api\controllers\user.go
        create   C:\Users\leell\go\src\beego-api\tests\default_test.go
        create   C:\Users\leell\go\src\beego-api\routers\router.go
        create   C:\Users\leell\go\src\beego-api\models\object.go
        create   C:\Users\leell\go\src\beego-api\models\user.go
        create   C:\Users\leell\go\src\beego-api\main.go
2024/01/15 21:54:15 SUCCESS  ▶ 0003 New API successfully created!

启动beego-web web服务

C:\Users\leell\go\src\beego-api>go mod tidy
go: finding module for package github.com/kr/text
go: found github.com/kr/text in github.com/kr/text v0.2.0

C:\Users\leell\go\src\beego-api>bee run
______
| ___ \
| |_/ /  ___   ___
| ___ \ / _ \ / _ \
| |_/ /|  __/|  __/
\____/  \___| \___| v2.1.0
2024/01/15 21:56:20 INFO     ▶ 0001 Using 'beego-api' as 'appname'
2024/01/15 21:56:20 INFO     ▶ 0002 Initializing watcher...
cannot install, GOBIN must be an absolute path
2024/01/15 21:56:26 SUCCESS  ▶ 0003 Built Successfully!
2024/01/15 21:56:26 INFO     ▶ 0004 Restarting 'beego-api.exe'...
2024/01/15 21:56:26 SUCCESS  ▶ 0005 './beego-api.exe' is running...
2024/01/15 21:56:28.138 [I] [server.go:281]  http server Running on http://:8080

生成的routers.go文件,更多路由设置参考文章:beego 路由设置 - 汇思班

func init() {
	ns := beego.NewNamespace("/v1",
		beego.NSNamespace("/object",
			beego.NSInclude(
				&controllers.ObjectController{},
			),
		),
		beego.NSNamespace("/user",
			beego.NSInclude(
				&controllers.UserController{},
			),
		),
	)
	beego.AddNamespace(ns)
}

当访问接口:

[root@k8s-master01 fastcms]# curl -X 'GET'  'http://192.168.10.101:8080/v1/user'   -H 'accept: application/json'
{
  "user_11111": {
    "Id": "user_11111",
    "Username": "astaxie",
    "Password": "11111",
    "Profile": {
      "Gender": "male",
      "Age": 20,
      "Address": "Singapore",
      "Email": "[email protected]"
    }
  }
}

三、bee项目打包

pack 目录用来发布应用的时候打包,会把项目打包成 zip 包,这样我们部署的时候直接把打包之后的项目上传,解压就可以部署了

C:\Users\leell\go\src\beego-web>bee pack
______
| ___ \
| |_/ /  ___   ___
| ___ \ / _ \ / _ \
| |_/ /|  __/|  __/
\____/  \___| \___| v2.1.0
2024/01/16 00:14:00 INFO     ▶ 0001 Packaging application on 'C:\Users\leell\go\src\beego-web'...
2024/01/16 00:14:00 INFO     ▶ 0002 Building application (beego-web)...
2024/01/16 00:14:00 INFO     ▶ 0003 Using: GOOS=windows GOARCH=amd64
2024/01/16 00:14:05 SUCCESS  ▶ 0004 Build Successful!
2024/01/16 00:14:05 INFO     ▶ 0005 Writing to output: C:\Users\leell\go\src\beego-web\beego-web.tar.gz
2024/01/16 00:14:05 INFO     ▶ 0006 Excluding relpath prefix: .
2024/01/16 00:14:05 INFO     ▶ 0007 Excluding relpath suffix: .go:.DS_Store:.tmp:go.mod:go.sum
2024/01/16 00:14:06 SUCCESS  ▶ 0008 Application packed!

会在beego-web目录下生成一个beego-web.tar.gz文件,文件列表:

views
static
conf
beego-web.exe

四、异常问题

刚开始访问http://192.168.10.101:8080/v1/user 出现下面的问题,排查了好久,都开始怀疑beego是否是个稳定的框架了。

bee工具的使用及创建第一个项目_第2张图片

​​结果发现可能是beego是v1版本,升级了v2就正常了

PS C:\Users\leell\go\src\beego-api> go get github.com/astaxie/beego/v2
go: module github.com/astaxie/beego@upgrade found (v1.12.3), but does not contain package github.com/astaxie/beego/v2

这样设计合理多了,里面的接口地址通过接口注解就能访问了,做过Java来写go没有注解确实还是希望有这种便捷的方式开发,看看下面接口注解方式是不是很nice了,完全很轻松实现restful api的请求方式。通过bee api生成的项目还有put、post、delete的请求方式,自行观看。

// @Title GetAll
// @Description get all Users
// @Success 200 {object} models.User
// @router / [get]
func (u *UserController) GetAll() {
	users := models.GetAllUsers()
	u.Data["json"] = users
	u.ServeJSON()
}

// @Title Get
// @Description get user by uid
// @Param	uid		path 	string	true		"The key for staticblock"
// @Success 200 {object} models.User
// @Failure 403 :uid is empty
// @router /:uid [get]
func (u *UserController) Get() {
	uid := u.GetString(":uid")
	if uid != "" {
		user, err := models.GetUser(uid)
		if err != nil {
			u.Data["json"] = err.Error()
		} else {
			u.Data["json"] = user
		}
	}
	u.ServeJSON()
}

参考文章:https://www.fansimao.com/785059.html

https://www.huisiban.com/703561.html

你可能感兴趣的:(beego从入门到实践,tensorflow,人工智能,python)