Skip to content

Commit

Permalink
Replace Gin with Hertz (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
a76yyyy committed Dec 9, 2022
1 parent 920f558 commit bdb590b
Show file tree
Hide file tree
Showing 14 changed files with 366 additions and 135 deletions.
8 changes: 3 additions & 5 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# tiktok

基于 kitex RPC微服务 + Gin HTTP服务完成的第三届字节跳动青训营-极简抖音后端项目
基于 kitex RPC微服务 + Hertz HTTP服务完成的第三届字节跳动青训营-极简抖音后端项目

## 一、项目特点

Expand Down Expand Up @@ -45,7 +45,7 @@

![image.png](pic/%E6%9C%8D%E5%8A%A1%E8%B0%83%E7%94%A8%E5%85%B3%E7%B3%BB.png)

- HTTP 使用 GIN 开放 HTTP 端口, 通过封装的RPC客户端与微服务中的服务端通信;
- HTTP 使用 Hertz 开放 HTTP 端口, 通过封装的RPC客户端与微服务中的服务端通信;

- RPC 微服务, 接收客户端的请求, 在各自的 command 中实现与数据库交互的业务逻辑;

Expand Down Expand Up @@ -209,6 +209,4 @@

5. 采用 Redis 作为 NoSQL 缓存,优化 JWT 鉴权,结合消息队列和 Redis 实现对定时更新 Token、各种操作数据 的缓存和持久性存储

6. 使用 Hertz 替换 Gin, 提高 HTTP 层的性能和可扩展性

7. 实现分库分表
6. 实现分库分表
10 changes: 5 additions & 5 deletions cmd/api/handlers/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ import (
"github.com/a76yyyy/tiktok/dal/pack"
"github.com/a76yyyy/tiktok/kitex_gen/comment"
"github.com/a76yyyy/tiktok/pkg/errno"
"github.com/gin-gonic/gin"
"github.com/cloudwego/hertz/pkg/app"
)

// 传递 评论操作 的上下文至 Comment 服务的 RPC 客户端, 并获取相应的响应.
func CommentAction(c *gin.Context) {
func CommentAction(ctx context.Context, c *app.RequestContext) {
var paramVar CommentActionParam
token := c.Query("token")
video_id := c.Query("video_id")
Expand Down Expand Up @@ -76,7 +76,7 @@ func CommentAction(c *gin.Context) {
rpcReq.CommentId = &cid64
}

resp, err := rpc.CommentAction(context.Background(), &rpcReq)
resp, err := rpc.CommentAction(ctx, &rpcReq)
if err != nil {
SendResponse(c, pack.BuildCommentActionResp(errno.ConvertErr(err)))
return
Expand All @@ -85,7 +85,7 @@ func CommentAction(c *gin.Context) {
}

// 传递 获取评论列表操作 的上下文至 Comment 服务的 RPC 客户端, 并获取相应的响应.
func CommentList(c *gin.Context) {
func CommentList(ctx context.Context, c *app.RequestContext) {
var paramVar CommentListParam
videoid, err := strconv.Atoi(c.Query("video_id"))
if err != nil {
Expand All @@ -100,7 +100,7 @@ func CommentList(c *gin.Context) {
return
}

resp, err := rpc.CommentList(context.Background(), &comment.DouyinCommentListRequest{
resp, err := rpc.CommentList(ctx, &comment.DouyinCommentListRequest{
VideoId: paramVar.VideoId,
Token: paramVar.Token,
})
Expand Down
10 changes: 5 additions & 5 deletions cmd/api/handlers/favorite.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ import (
"github.com/a76yyyy/tiktok/dal/pack"
"github.com/a76yyyy/tiktok/kitex_gen/favorite"
"github.com/a76yyyy/tiktok/pkg/errno"
"github.com/gin-gonic/gin"
"github.com/cloudwego/hertz/pkg/app"
)

// 传递 点赞操作 的上下文至 Favorite 服务的 RPC 客户端, 并获取相应的响应.
func FavoriteAction(c *gin.Context) {
func FavoriteAction(ctx context.Context, c *app.RequestContext) {
var paramVar FavoriteActionParam
token := c.Query("token")
video_id := c.Query("video_id")
Expand All @@ -56,7 +56,7 @@ func FavoriteAction(c *gin.Context) {
paramVar.VideoId = int64(vid)
paramVar.ActionType = int32(act)

resp, err := rpc.FavoriteAction(context.Background(), &favorite.DouyinFavoriteActionRequest{
resp, err := rpc.FavoriteAction(ctx, &favorite.DouyinFavoriteActionRequest{
VideoId: paramVar.VideoId,
Token: paramVar.Token,
ActionType: paramVar.ActionType,
Expand All @@ -69,7 +69,7 @@ func FavoriteAction(c *gin.Context) {
}

// 传递 获取点赞列表操作 的上下文至 Favorite 服务的 RPC 客户端, 并获取相应的响应.
func FavoriteList(c *gin.Context) {
func FavoriteList(ctx context.Context, c *app.RequestContext) {
var paramVar UserParam
userid, err := strconv.Atoi(c.Query("user_id"))
if err != nil {
Expand All @@ -84,7 +84,7 @@ func FavoriteList(c *gin.Context) {
return
}

resp, err := rpc.FavoriteList(context.Background(), &favorite.DouyinFavoriteListRequest{
resp, err := rpc.FavoriteList(ctx, &favorite.DouyinFavoriteListRequest{
UserId: paramVar.UserId,
Token: paramVar.Token,
})
Expand Down
6 changes: 3 additions & 3 deletions cmd/api/handlers/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ import (
"github.com/a76yyyy/tiktok/dal/pack"
"github.com/a76yyyy/tiktok/kitex_gen/feed"
"github.com/a76yyyy/tiktok/pkg/errno"
"github.com/gin-gonic/gin"
"github.com/cloudwego/hertz/pkg/app"
)

// 传递 获取用户视频流操作 的上下文至 Feed 服务的 RPC 客户端, 并获取相应的响应.
func GetUserFeed(c *gin.Context) {
func GetUserFeed(ctx context.Context, c *app.RequestContext) {
var feedVar FeedParam
var laststTime int64
var token string
Expand All @@ -54,7 +54,7 @@ func GetUserFeed(c *gin.Context) {
token = c.Query("token")
feedVar.Token = &token

resp, err := rpc.GetUserFeed(context.Background(), &feed.DouyinFeedRequest{
resp, err := rpc.GetUserFeed(ctx, &feed.DouyinFeedRequest{
LatestTime: feedVar.LatestTime,
Token: feedVar.Token,
})
Expand Down
11 changes: 5 additions & 6 deletions cmd/api/handlers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@
* @Description: 定义 所有API handler 的 输入输出参数
*/

// 定义 Gin HTTP API 的 handler
// 定义 Hertz HTTP API 的 handler
package handlers

import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/protocol/consts"
)

// SendResponse pack response
func SendResponse(c *gin.Context, response interface{}) {
c.JSON(http.StatusOK, response)
func SendResponse(c *app.RequestContext, response interface{}) {
c.JSON(consts.StatusOK, response)
}

// 用户注册 handler 输入参数
Expand Down
18 changes: 12 additions & 6 deletions cmd/api/handlers/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,22 @@ import (
"github.com/a76yyyy/tiktok/dal/pack"
"github.com/a76yyyy/tiktok/kitex_gen/publish"
"github.com/a76yyyy/tiktok/pkg/errno"
"github.com/gin-gonic/gin"
"github.com/cloudwego/hertz/pkg/app"
)

// 传递 发布视频操作 的上下文至 Publish 服务的 RPC 客户端, 并获取相应的响应.
func PublishAction(c *gin.Context) {
func PublishAction(ctx context.Context, c *app.RequestContext) {
var paramVar PublishActionParam
token := c.PostForm("token")
title := c.PostForm("title")

file, _, err := c.Request.FormFile("data")
fileHeader, err := c.Request.FormFile("data")
if err != nil {
SendResponse(c, pack.BuildPublishResp(errno.ErrDecodingFailed))
return
}

file, err := fileHeader.Open()
if err != nil {
SendResponse(c, pack.BuildPublishResp(errno.ErrDecodingFailed))
return
Expand All @@ -58,7 +64,7 @@ func PublishAction(c *gin.Context) {
paramVar.Token = token
paramVar.Title = title

resp, err := rpc.PublishAction(context.Background(), &publish.DouyinPublishActionRequest{
resp, err := rpc.PublishAction(ctx, &publish.DouyinPublishActionRequest{
Title: paramVar.Title,
Token: paramVar.Token,
Data: buf.Bytes(),
Expand All @@ -71,7 +77,7 @@ func PublishAction(c *gin.Context) {
}

// 传递 获取视频列表操作 的上下文至 Publish 服务的 RPC 客户端, 并获取相应的响应.
func PublishList(c *gin.Context) {
func PublishList(ctx context.Context, c *app.RequestContext) {
var paramVar UserParam
userid, err := strconv.Atoi(c.Query("user_id"))
if err != nil {
Expand All @@ -86,7 +92,7 @@ func PublishList(c *gin.Context) {
return
}

resp, err := rpc.PublishList(context.Background(), &publish.DouyinPublishListRequest{
resp, err := rpc.PublishList(ctx, &publish.DouyinPublishListRequest{
UserId: paramVar.UserId,
Token: paramVar.Token,
})
Expand Down
14 changes: 7 additions & 7 deletions cmd/api/handlers/relation.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ import (
"github.com/a76yyyy/tiktok/dal/pack"
"github.com/a76yyyy/tiktok/kitex_gen/relation"
"github.com/a76yyyy/tiktok/pkg/errno"
"github.com/gin-gonic/gin"
"github.com/cloudwego/hertz/pkg/app"
)

// 传递 关注操作 的上下文至 Relation 服务的 RPC 客户端, 并获取相应的响应.
func RelationAction(c *gin.Context) {
func RelationAction(ctx context.Context, c *app.RequestContext) {
var paramVar RelationActionParam
token := c.Query("token")
to_user_id := c.Query("to_user_id")
Expand Down Expand Up @@ -63,7 +63,7 @@ func RelationAction(c *gin.Context) {
ActionType: paramVar.ActionType,
}

resp, err := rpc.RelationAction(context.Background(), &rpcReq)
resp, err := rpc.RelationAction(ctx, &rpcReq)
if err != nil {
SendResponse(c, pack.BuildRelationActionResp(errno.ConvertErr(err)))
return
Expand All @@ -72,7 +72,7 @@ func RelationAction(c *gin.Context) {
}

// 传递 获取正在关注列表操作 的上下文至 Relation 服务的 RPC 客户端, 并获取相应的响应.
func RelationFollowList(c *gin.Context) {
func RelationFollowList(ctx context.Context, c *app.RequestContext) {
var paramVar UserParam
uid, err := strconv.Atoi(c.Query("user_id"))
if err != nil {
Expand All @@ -87,7 +87,7 @@ func RelationFollowList(c *gin.Context) {
return
}

resp, err := rpc.RelationFollowList(context.Background(), &relation.DouyinRelationFollowListRequest{
resp, err := rpc.RelationFollowList(ctx, &relation.DouyinRelationFollowListRequest{
UserId: paramVar.UserId,
Token: paramVar.Token,
})
Expand All @@ -99,7 +99,7 @@ func RelationFollowList(c *gin.Context) {
}

// 传递 获取粉丝列表操作 的上下文至 Relation 服务的 RPC 客户端, 并获取相应的响应.
func RelationFollowerList(c *gin.Context) {
func RelationFollowerList(ctx context.Context, c *app.RequestContext) {
var paramVar UserParam
uid, err := strconv.Atoi(c.Query("user_id"))
if err != nil {
Expand All @@ -114,7 +114,7 @@ func RelationFollowerList(c *gin.Context) {
return
}

resp, err := rpc.RelationFollowerList(context.Background(), &relation.DouyinRelationFollowerListRequest{
resp, err := rpc.RelationFollowerList(ctx, &relation.DouyinRelationFollowerListRequest{
UserId: paramVar.UserId,
Token: paramVar.Token,
})
Expand Down
14 changes: 7 additions & 7 deletions cmd/api/handlers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ import (
"github.com/a76yyyy/tiktok/dal/pack"
"github.com/a76yyyy/tiktok/kitex_gen/user"
"github.com/a76yyyy/tiktok/pkg/errno"
"github.com/gin-gonic/gin"
"github.com/cloudwego/hertz/pkg/app"
)

// 传递 注册用户操作 的上下文至 User 服务的 RPC 客户端, 并获取相应的响应.
func Register(c *gin.Context) {
func Register(ctx context.Context, c *app.RequestContext) {
var registerVar UserRegisterParam
registerVar.UserName = c.Query("username")
registerVar.PassWord = c.Query("password")
Expand All @@ -45,7 +45,7 @@ func Register(c *gin.Context) {
return
}

resp, err := rpc.Register(context.Background(), &user.DouyinUserRegisterRequest{
resp, err := rpc.Register(ctx, &user.DouyinUserRegisterRequest{
Username: registerVar.UserName,
Password: registerVar.PassWord,
})
Expand All @@ -57,7 +57,7 @@ func Register(c *gin.Context) {
}

// 传递 注册用户登录操作 的上下文至 User 服务的 RPC 客户端, 并获取相应的响应.
func Login(c *gin.Context) {
func Login(ctx context.Context, c *app.RequestContext) {
var registerVar UserRegisterParam
registerVar.UserName = c.Query("username")
registerVar.PassWord = c.Query("password")
Expand All @@ -67,7 +67,7 @@ func Login(c *gin.Context) {
return
}

resp, err := rpc.Login(context.Background(), &user.DouyinUserRegisterRequest{
resp, err := rpc.Login(ctx, &user.DouyinUserRegisterRequest{
Username: registerVar.UserName,
Password: registerVar.PassWord,
})
Expand All @@ -79,7 +79,7 @@ func Login(c *gin.Context) {
}

// 传递 获取注册用户`UserID`操作 的上下文至 User 服务的 RPC 客户端, 并获取相应的响应.
func GetUserById(c *gin.Context) {
func GetUserById(ctx context.Context, c *app.RequestContext) {
var userVar UserParam
userid, err := strconv.Atoi(c.Query("user_id"))
if err != nil {
Expand All @@ -94,7 +94,7 @@ func GetUserById(c *gin.Context) {
return
}

resp, err := rpc.GetUserById(context.Background(), &user.DouyinUserRequest{
resp, err := rpc.GetUserById(ctx, &user.DouyinUserRequest{
UserId: userVar.UserId,
Token: userVar.Token,
})
Expand Down
Loading

0 comments on commit bdb590b

Please sign in to comment.