go-zero 单体应用实践(一)

环境搭建

官方文档

  • Golang 环境安装 👉 golang
  • Go Module设置 go env -w GO111MODULE="on"
  • goctl安装 👉 goctl
  • protoc & protoc-gen-go安装 goctl env check -i -f --verbose

etcdredismysql 等开发工具可以通过Docker 快速搭建;可参考👉 gonivinck

创建单体应用

1、创建目录

mkdir user-login
cd user-login
go mod init user-login

2、编辑api文件 userlogin.api

api语法介绍

type (
	RegisterRequest {
		Name     string `json:"name"`
		Email    string `json:"email"`
		Password string `json:"password"`
	}

	RegisterResponse {
		ID    int    `json:"id"`
		Name  string `json:"name"`
		Email string `json:"email"`
	}

	LoginRequest {
		Email    string `json:"email"`
		Password string `json:"password"`
	}

	LoginResponse {
		Token  string `json:"token"`
		Expire int64  `json:"expire"`
	}
)

service userlogin-api {
	@handler RegisterHandler
	post /api/register(RegisterRequest) returns (RegisterResponse);
	
	@handler LoginHandler
	post /api/login(LoginRequest) returns (LoginResponse);
}

3、执行生成代码命令

goctl api go -api userlogin.api -dir .
go mod tidy

生成文件目录

userlogin
├─etc // // 配置文件
├─internal
│  ├─config  // 配置声明type
│  ├─handler  // 路由及handler转发
│  ├─logic  // 业务逻辑
│  ├─svc // logic所依赖的资源池
│  └─types  // request、response的struct,根据api自动生成

4、编辑配置文件 userlogin/etc/userlogin-api.yaml

Name: userlogin-api
Host: 0.0.0.0
Port: 8888
Mysql:
  DataSource: root:123456@tcp(mysql:3306)/user?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai

CacheRedis:
  - Host: redis:6379
    Type: node

Salt: DWe7OZf6KPlnv7yy
Auth:
  AccessSecret: uOvKLmVfztaXGpNYd4Z0I1SiT7MweJhl
  AccessExpire: 86400

添加config声明 userlogin/internal/config/config.go
其中Salt 用来密码加密存储,Auth 用于 jwt鉴权

type Config struct {
	rest.RestConf
	Mysql struct {
		DataSource string
	}

	CacheRedis cache.CacheConf
	Salt       string
	Auth       struct {
		AccessSecret string
		AccessExpire int64
	}
}

4、生成Model文件

添加用户表 userlogin/model/user.sql

CREATE TABLE `user` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255)  NOT NULL DEFAULT '' COMMENT '用户姓名',
  `email` varchar(255)  NOT NULL DEFAULT '' COMMENT '用户邮箱',
  `password` varchar(255)  NOT NULL DEFAULT '' COMMENT '用户密码',
  `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_email_unique` (`email`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4;

通过命令生成model文件 model 指令

goctl model mysql ddl -src=“userlogin/model/user.sql” -dir=“userlogin/model/” -c

-c 参数代表 在model层使用缓存 需要配置对应的 CacheRedis
生成文件如下:

user.sql
usermodel.go
usermodel_gen.go
vars.go

可以看到 goctl 已经生成了 UserModel interface 并实现了基本的增删改查询代码;

UserModel

5、完善服务依赖

编辑 userlogin/internal/svc/servicecontext.go 添加 logic所依赖的 UserModel

type ServiceContext struct {
	Config config.Config

	UserModel model.UserModel
}

func NewServiceContext(c config.Config) *ServiceContext {
	conn := sqlx.NewMysql(c.Mysql.DataSource)

	return &ServiceContext{
		Config:    c,
		UserModel: model.NewUserModel(conn, c.CacheRedis),
	}
}

6、业务逻辑代码

1、注册逻辑代码

type RegisterLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RegisterLogic) Register(req *types.RegisterRequest) (resp *types.RegisterResponse, err error) {

	_, err = l.svcCtx.UserModel.FindOneByEmail(l.ctx, req.Email)
	if err == nil {
		return nil, status.Error(100, "该用户已存在")
	}
	if err != model.ErrNotFound {
		return nil, status.Error(100, err.Error())
	}
	newUser := model.User{
		Name:     req.Name,
		Email:    req.Email,
		Password: cryptx.PasswordEncrypt(l.svcCtx.Config.Salt, req.Password),
	}

	res, err := l.svcCtx.UserModel.Insert(l.ctx, &newUser)
	if err != nil {
		return nil, status.Error(500, err.Error())
	}

	newUser.Id, err = res.LastInsertId()
	if err != nil {
		return nil, status.Error(500, err.Error())
	}

	return &types.RegisterResponse{
		ID:    int(newUser.Id),
		Name:  newUser.Name,
		Email: newUser.Email,
	}, nil
}

2、添加 JWT 工具 userlogin/common/jwtx/jwt.go

package jwtx

import "github.com/golang-jwt/jwt"

func GetToken(secretKey string, iat, seconds, uid int64) (string, error) {
	claims := make(jwt.MapClaims)
	claims["exp"] = iat + seconds
	claims["iat"] = iat
	claims["uid"] = uid
	token := jwt.New(jwt.SigningMethodHS256)
	token.Claims = claims
	return token.SignedString([]byte(secretKey))
}

3、用户登录逻辑 userlogin/internal/logic/loginlogic.go

type LoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LoginLogic) Login(req *types.LoginRequest) (resp *types.LoginResponse, err error) {
	res, err := l.svcCtx.UserModel.FindOneByEmail(l.ctx, req.Email)
	if err != nil {
		if err == model.ErrNotFound {
			return nil, status.Error(100, "用户不存在")
		}
		return nil, status.Error(500, err.Error())
	}
	// 判断密码是否正确
	password := cryptx.PasswordEncrypt(l.svcCtx.Config.Salt, req.Password)
	if password != res.Password {
		return nil, status.Error(100, "密码错误")
	}
	now := time.Now().Unix()
	accessExpire := l.svcCtx.Config.Auth.AccessExpire

	accessToken, err := jwtx.GetToken(l.svcCtx.Config.Auth.AccessSecret, now, accessExpire, res.Id)
	if err != nil {
		return nil, err
	}
	return &types.LoginResponse{
		Token:  accessToken,
		Expire: now + accessExpire,
	}, nil
}

7、启动测试

go run userlogin.go -f etc/userlogin-api.yaml
# Starting server at 0.0.0.0:8888...



注册测试

curl -i -X POST http://127.0.0.1:8888/api/register -H 'Content-Type: application/json' -d '{"name":"test","email":"test@gmail.com","password":"****"}'

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Traceparent: 00-91ee4162c2a42c7df3111ab73cdb242f-1b886700e9099cc8-00
Date: Tue, 19 Jul 2022 16:19:33 GMT
Content-Length: 47

{"id":1,"name":"test","email":"test@gmail.com"}

登录测试

curl -i -X POST http://127.0.0.1:8888/api/login -H 'Content-Type: application/json' -d '{"email":"test@gmail.com","password":"****"}'

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Traceparent: 00-4cc2b11a99a2216590c50a2a7215e832-d7af5dc45fa6a65b-00
Date: Tue, 19 Jul 2022 16:22:28 GMT
Content-Length: 171

{"token":"eyJhbGciOiJIUzI1NiI………………………………","expire":1658334148}

jwt鉴权应用

上面已经完成了登录注册功能;并且登录返回了Token;接下来就用一下Token

1、开启jwt鉴权

编写 api 文件 在 service 上方声明使用jwt鉴权
修改 userlogin/userlogin.api 重新执行 goctl api 命令

type UserInfoResponse {
		ID    int64    `json:"id"`
		Name  string `json:"name"`
		Email string `json:"email"`
	}

@server(
	jwt: Auth // Auth 与 userlogin/internal/config/config.go 中配置的 jwt 参数对应
)
service userlogin-api {
	@handler UserInfo
	get /api/userinfo returns (UserInfoResponse)
}

可以看到路由文件中 userlogin/internal/handler/routes.go 新增的路由中通 WithJwt 声明了使用jwt鉴权

server.AddRoutes(
		[]rest.Route{
			{
				Method:  http.MethodGet,
				Path:    "/api/userinfo",
				Handler: UserInfoHandler(serverCtx),
			},
		},
		rest.WithJwt(serverCtx.Config.Auth.AccessSecret),
	)

2、编写logic 代码

userlogin/internal/logic/userinfologic.go

func NewUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserInfoLogic {
	return &UserInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserInfoLogic) UserInfo() (resp *types.UserInfoResponse, err error) {
	uid, _ := l.ctx.Value("uid").(json.Number).Int64()
	one, err := l.svcCtx.UserModel.FindOne(l.ctx, uid)
	if err != nil {
		return nil, err
	}
	return &types.UserInfoResponse{
		ID:    one.Id,
		Name:  one.Name,
		Email: one.Email,
	}, nil
}

3、运行测试

# curl -i http://127.0.0.1:8000/api/userinfo

HTTP/1.1 401 Unauthorized
Traceparent: 00-a418d68212d9e2c29664a7e9aeefbd14-7282c005bbcd5f24-00
Date: Thu, 21 Jul 2022 17:01:21 GMT
Content-Length: 0

#curl -i -X GET  'http://127.0.0.1:8000/api/userinfo'  -H 'Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NTg1MDkzNDAsImlhdCI6MTY1ODQyMjk0MCwidWlkIjoxfQ.j6K0CjJ9jM5a3a7-DRUGq6b3uvMFcelvR7vQkZAQXWw'

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Traceparent: 00-e547b466d3e7da54d932838bfdee17c4-4ec7aaedb056ef03-00
Date: Thu, 21 Jul 2022 17:07:42 GMT
Content-Length: 47

{"id":1,"name":"test","email":"test@gmail.com"}

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
go-zero是一个开源的Go语言框架,它在构建微服务和高并发应用方面具有突破性的优势。其中一个突出的特点就是它整合了masterminds/squirrel,从而实现了优雅的多数据库支持。 masterminds/squirrel是一个流行的SQL查询构建器,它以非常直观和灵活的方式提供了编写SQL查询的功能。而go-zero在此基础上做了进一步的封装和优化,使得使用者能够更加方便地编写和执行SQL查询。 首先,go-zero提供了一组简洁而强大的API,使得构建SQL查询非常容易。开发者只需要按照一定的约定来创建查询参数和条件,然后使用go-zero提供的API来构建查询语句,即可完成复杂的SQL查询。 其次,go-zero还增加了一些高级功能,进一步提升了多数据库查询的灵活性和性能。例如,它支持数据库连接池管理,可以动态调整数据库连接数以适应并发请求;还支持分表分库功能,可以按照一定的规则将数据分散存储在不同的数据库或表中,从而提高查询效率。 最重要的是,go-zero通过内置的代码生成工具,提供了自动化生成数据库访问代码的能力。开发者只需要定义数据表的结构,然后运行代码生成工具,就能够自动生成包含增删改查等一系列数据库操作的代码。这极大地提高了开发效率,减少了出错的机会。 综上所述,go-zero整合了masterminds/squirrel,通过提供简洁强大的API、高级功能和自动化代码生成工具,实现了优雅的多数据库支持。它在微服务和高并发应用场景下的表现突出,为开发者提供了极大的便利和效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Devin_S

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值