-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* message 和 comment 功能代码提交 * message 和 comment 功能代码提交 * db调用代码修改 * db调用代码修改 etcd修改 * 代码修改优化 --------- Co-authored-by: Shiyuan Ji <[email protected]>
- Loading branch information
1 parent
ba66a9e
commit d9a7cd7
Showing
39 changed files
with
4,484 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package handler | ||
|
||
import ( | ||
"context" | ||
"github.com/Tiktok-Lite/kotkit/cmd/api/rpc" | ||
"github.com/Tiktok-Lite/kotkit/internal/response" | ||
"github.com/Tiktok-Lite/kotkit/kitex_gen/comment" | ||
"github.com/Tiktok-Lite/kotkit/pkg/helper/constant" | ||
"github.com/Tiktok-Lite/kotkit/pkg/log" | ||
"github.com/cloudwego/hertz/pkg/app" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func CommentList(ctx context.Context, c *app.RequestContext) { | ||
logger := log.Logger() | ||
|
||
videoId := c.Query("video_id") | ||
if videoId == "" { | ||
logger.Errorf("Illegal input: empty video_id.") | ||
ResponseError(c, http.StatusBadRequest, response.PackCommentListError("video_id不能为空")) | ||
return | ||
} | ||
id, err := strconv.ParseInt(videoId, 10, 64) | ||
if err != nil { | ||
logger.Errorf("failed to parse video_id: %v", err) | ||
ResponseError(c, http.StatusBadRequest, response.PackCommentActionError("请检查您的输入是否合法")) | ||
return | ||
} | ||
|
||
token := c.Query("token") | ||
if token == "" { | ||
logger.Errorf("Illegal input: empty token.") | ||
ResponseError(c, http.StatusBadRequest, response.PackCommentListError("token不能为空")) | ||
return | ||
} | ||
req := &comment.DouyinCommentListRequest{ | ||
Token: token, | ||
VideoId: id, | ||
} | ||
resp, err := rpc.CommentList(ctx, req) | ||
if err != nil { | ||
logger.Errorf("error occurs when calling rpc: %v", err) | ||
ResponseError(c, http.StatusInternalServerError, response.PackCommentListError(resp.StatusMsg)) | ||
return | ||
} | ||
ResponseSuccess(c, response.PackCommentListSuccess(resp.CommentList, "评论列表获取成功")) | ||
} | ||
|
||
func CommentAction(ctx context.Context, c *app.RequestContext) { | ||
logger := log.Logger() | ||
|
||
videoId := c.Query("video_id") | ||
if videoId == "" { | ||
logger.Errorf("Illegal input: empty video_id.") | ||
ResponseError(c, http.StatusBadRequest, response.PackCommentActionError("video_id不能为空")) | ||
return | ||
} | ||
id, err := strconv.ParseInt(videoId, 10, 64) | ||
if err != nil { | ||
logger.Errorf("failed to parse video_id: %v", err) | ||
ResponseError(c, http.StatusBadRequest, response.PackCommentActionError("请检查video_id是否合法")) | ||
return | ||
} | ||
|
||
token := c.Query("token") | ||
if token == "" { | ||
logger.Errorf("Illegal input: empty token.") | ||
ResponseError(c, http.StatusBadRequest, response.PackCommentActionError("token不能为空")) | ||
return | ||
} | ||
|
||
actionType := c.Query("action_type") | ||
if actionType == "" { | ||
logger.Errorf("Illegal input: empty action_type.") | ||
ResponseError(c, http.StatusBadRequest, response.PackCommentActionError("action_type不能为空")) | ||
return | ||
} | ||
act, err := strconv.Atoi(actionType) | ||
if err != nil { | ||
logger.Errorf("failed to parse action_type: %v", err) | ||
ResponseError(c, http.StatusBadRequest, response.PackCommentActionError("请检查您的输入是否合法")) | ||
return | ||
} | ||
|
||
req := &comment.DouyinCommentActionRequest{ | ||
Token: token, | ||
VideoId: id, | ||
ActionType: int32(act), | ||
} | ||
if act == constant.PostCommentCode { | ||
content := c.Query("comment_text") | ||
s := strings.TrimSpace(content) | ||
if len(s) == 0 { | ||
ResponseError(c, http.StatusInternalServerError, response.PackCommentActionError("评论不能为空")) | ||
return | ||
} | ||
req.CommentText = s | ||
} | ||
if act == constant.DeleteCommentCode { | ||
commentId := c.Query("comment_id") | ||
if commentId == "" { | ||
logger.Errorf("Illegal input: empty comment_id.") | ||
ResponseError(c, http.StatusBadRequest, response.PackCommentActionError("comment_id不能为空")) | ||
return | ||
} | ||
cid, err := strconv.ParseInt(commentId, 10, 64) | ||
if err != nil { | ||
ResponseError(c, http.StatusInternalServerError, response.PackCommentActionError("请检查comment_id是否合法")) | ||
return | ||
} | ||
req.CommentId = cid | ||
} | ||
|
||
resp, err := rpc.CommentAction(ctx, req) | ||
if err != nil { | ||
logger.Errorf("failed to call rpc: %v", err) | ||
ResponseError(c, http.StatusInternalServerError, | ||
response.PackCommentActionError(resp.StatusMsg)) | ||
return | ||
} | ||
|
||
ResponseSuccess(c, response.PackCommentActionSuccess(resp.Comment, resp.StatusMsg)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package handler | ||
|
||
import ( | ||
"context" | ||
"github.com/Tiktok-Lite/kotkit/cmd/api/rpc" | ||
"github.com/Tiktok-Lite/kotkit/internal/response" | ||
"github.com/Tiktok-Lite/kotkit/kitex_gen/message" | ||
"github.com/Tiktok-Lite/kotkit/pkg/helper/constant" | ||
"github.com/Tiktok-Lite/kotkit/pkg/log" | ||
"github.com/cloudwego/hertz/pkg/app" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func Chat(ctx context.Context, c *app.RequestContext) { | ||
logger := log.Logger() | ||
|
||
token := c.Query("token") | ||
if token == "" { | ||
logger.Errorf("Illegal input: empty token.") | ||
ResponseError(c, http.StatusBadRequest, response.PackMessageListError("token不能为空")) | ||
return | ||
} | ||
|
||
toUserId := c.Query("to_user_id") | ||
if toUserId == "" { | ||
logger.Errorf("Illegal input: empty to_user_id.") | ||
ResponseError(c, http.StatusBadRequest, response.PackMessageListError("to_user_id不能为空")) | ||
return | ||
} | ||
id, err := strconv.ParseInt(toUserId, 10, 64) | ||
if err != nil { | ||
logger.Errorf("failed to parse to_user_id: %v", err) | ||
ResponseError(c, http.StatusBadRequest, | ||
response.PackMessageListError("请检查您的输入是否合法")) | ||
return | ||
} | ||
|
||
req := &message.DouyinMessageChatRequest{ | ||
Token: token, | ||
ToUserId: id, | ||
} | ||
resp, err := rpc.MessageList(ctx, req) | ||
if err != nil { | ||
logger.Errorf("failed to call rpc: %v", err) | ||
ResponseError(c, http.StatusInternalServerError, | ||
response.PackMessageListError(resp.StatusMsg)) | ||
return | ||
} | ||
|
||
ResponseSuccess(c, response.PackMessageListSuccess(resp.MessageList, "聊天列表获取成功")) | ||
} | ||
|
||
func MessageAction(ctx context.Context, c *app.RequestContext) { | ||
logger := log.Logger() | ||
|
||
token := c.Query("token") | ||
if token == "" { | ||
logger.Errorf("Illegal input: empty token.") | ||
ResponseError(c, http.StatusBadRequest, response.PackMessageListError("token不能为空")) | ||
return | ||
} | ||
|
||
toUserId := c.Query("to_user_id") | ||
if toUserId == "" { | ||
logger.Errorf("Illegal input: empty to_user_id.") | ||
ResponseError(c, http.StatusBadRequest, response.PackMessageListError("to_user_id不能为空")) | ||
return | ||
} | ||
id, err := strconv.ParseInt(toUserId, 10, 64) | ||
if err != nil { | ||
logger.Errorf("failed to parse to_user_id: %v", err) | ||
ResponseError(c, http.StatusBadRequest, | ||
response.PackMessageListError("请检查您的输入是否合法")) | ||
return | ||
} | ||
|
||
actionType := c.Query("action_type") | ||
if actionType == "" { | ||
logger.Errorf("Illegal input: empty action_type.") | ||
ResponseError(c, http.StatusBadRequest, response.PackMessageListError("action_type不能为空")) | ||
return | ||
} | ||
act, err := strconv.Atoi(actionType) | ||
if err != nil { | ||
logger.Errorf("failed to parse action_type: %v", err) | ||
ResponseError(c, http.StatusBadRequest, response.PackMessageListError("请检查您的输入是否合法")) | ||
return | ||
} | ||
|
||
req := &message.DouyinMessageActionRequest{ | ||
Token: token, | ||
ToUserId: id, | ||
ActionType: int32(act), | ||
} | ||
if act == constant.PostMessageCode { | ||
content := c.Query("content") | ||
s := strings.TrimSpace(content) | ||
if len(s) == 0 { | ||
ResponseError(c, http.StatusInternalServerError, | ||
response.PackMessageListError("消息不能为空")) | ||
return | ||
} | ||
req.Content = s | ||
} | ||
resp, err := rpc.MessageAction(ctx, req) | ||
if err != nil { | ||
logger.Errorf("failed to call rpc: %v", err) | ||
ResponseError(c, http.StatusInternalServerError, | ||
response.PackMessageListError(resp.StatusMsg)) | ||
return | ||
} | ||
|
||
ResponseSuccess(c, response.PackMessageActionSuccess(resp.StatusMsg)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package rpc | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/Tiktok-Lite/kotkit/kitex_gen/comment" | ||
"github.com/Tiktok-Lite/kotkit/kitex_gen/comment/commentservice" | ||
"github.com/Tiktok-Lite/kotkit/pkg/etcd" | ||
"github.com/Tiktok-Lite/kotkit/pkg/helper/constant" | ||
"github.com/cloudwego/kitex/client" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var ( | ||
commentClient commentservice.Client | ||
) | ||
|
||
func InitComment(config *viper.Viper) { | ||
r, err := etcd.Resolver() | ||
if err != nil { | ||
logger.Errorf("Error occurs when creating etcd resolver: %v", err) | ||
panic(err) | ||
} | ||
|
||
commentServiceName := config.GetString("server.name") | ||
|
||
c, err := commentservice.NewClient(commentServiceName, client.WithResolver(r)) | ||
|
||
if err != nil { | ||
logger.Errorf("Error occurs when creating login client: %v", err) | ||
panic(err) | ||
} | ||
commentClient = c | ||
} | ||
|
||
func CommentList(ctx context.Context, req *comment.DouyinCommentListRequest) (*comment.DouyinCommentListResponse, error) { | ||
resp, err := commentClient.CommentList(ctx, req) | ||
if err != nil { | ||
return resp, err | ||
} | ||
if resp.StatusCode == constant.StatusErrorCode { | ||
return resp, fmt.Errorf(resp.StatusMsg) | ||
} | ||
return resp, nil | ||
} | ||
func CommentAction(ctx context.Context, req *comment.DouyinCommentActionRequest) (*comment.DouyinCommentActionResponse, error) { | ||
resp, err := commentClient.CommentAction(ctx, req) | ||
if err != nil { | ||
return resp, err | ||
} | ||
if resp.StatusCode == constant.StatusErrorCode { | ||
return resp, fmt.Errorf(resp.StatusMsg) | ||
} | ||
|
||
return resp, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package rpc | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/Tiktok-Lite/kotkit/kitex_gen/message" | ||
"github.com/Tiktok-Lite/kotkit/kitex_gen/message/messageservice" | ||
"github.com/Tiktok-Lite/kotkit/pkg/etcd" | ||
"github.com/Tiktok-Lite/kotkit/pkg/helper/constant" | ||
"github.com/cloudwego/kitex/client" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var ( | ||
messageClient messageservice.Client | ||
) | ||
|
||
func InitMessage(config *viper.Viper) { | ||
r, err := etcd.Resolver() | ||
if err != nil { | ||
logger.Errorf("Error occurs when creating etcd resolver: %v", err) | ||
panic(err) | ||
} | ||
|
||
messageServiceName := config.GetString("server.name") | ||
|
||
c, err := messageservice.NewClient(messageServiceName, client.WithResolver(r)) | ||
|
||
if err != nil { | ||
logger.Errorf("Error occurs when creating login client: %v", err) | ||
panic(err) | ||
} | ||
messageClient = c | ||
} | ||
|
||
func MessageList(ctx context.Context, req *message.DouyinMessageChatRequest) (*message.DouyinMessageChatResponse, error) { | ||
resp, err := messageClient.MessageChat(ctx, req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if resp.StatusCode == constant.StatusErrorCode { | ||
return resp, fmt.Errorf(resp.StatusMsg) | ||
} | ||
return resp, nil | ||
} | ||
func MessageAction(ctx context.Context, req *message.DouyinMessageActionRequest) (*message.DouyinMessageActionResponse, error) { | ||
resp, err := messageClient.MessageAction(ctx, req) | ||
if err != nil { | ||
return resp, err | ||
} | ||
if resp.StatusCode == constant.StatusErrorCode { | ||
return resp, fmt.Errorf(resp.StatusMsg) | ||
} | ||
|
||
return resp, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/usr/bin/env bash | ||
RUN_NAME="comment" | ||
|
||
mkdir -p output/bin | ||
cp script/* output/ | ||
chmod +x output/bootstrap.sh | ||
|
||
if [ "$IS_SYSTEM_TEST_ENV" != "1" ]; then | ||
go build -o output/bin/${RUN_NAME} | ||
else | ||
go test -c -covermode=set -o output/bin/${RUN_NAME} -coverpkg=./... | ||
fi | ||
|
Oops, something went wrong.