-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
103 lines (94 loc) · 2.45 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"context"
"log"
"net"
"os"
_ "github.com/CodyGuo/godaemon"
"github.com/chaossat/tiktak/middleware"
"github.com/chaossat/tiktak/service/favoriteaction/model"
"github.com/chaossat/tiktak/service/favoriteaction/pb"
"github.com/spf13/viper"
"google.golang.org/grpc"
)
type FavoriteAction struct {
}
func (f *FavoriteAction) GetFavoriteAction(ctx context.Context, req *pb.DouyinFavoriteActionRequest) (*pb.DouyinFavoriteActionResponse, error) {
var statuscode int32
var statusmsg string
token := *req.Token
_, err := middleware.CheckToken(token)
if err != nil {
if err != nil {
log.Println("解析用户token失败", err.Error())
statuscode = 1
statusmsg = "解析用户token失败"
return &pb.DouyinFavoriteActionResponse{
StatusCode: &statuscode,
StatusMsg: &statusmsg,
}, nil
}
}
if *req.ActionType == 1 {
_, err := model.GiveLike(*req.UserId, *req.VideoId)
if err != nil {
log.Println("点赞操作失败", err.Error())
statuscode = 1
statusmsg = "点赞操作失败"
return &pb.DouyinFavoriteActionResponse{
StatusCode: &statuscode,
StatusMsg: &statusmsg,
}, nil
}
statuscode = 0
statusmsg = "点赞成功"
return &pb.DouyinFavoriteActionResponse{
StatusCode: &statuscode,
StatusMsg: &statusmsg,
}, nil
}
_, err = model.CancelLike(*req.UserId, *req.VideoId)
if err != nil {
log.Println("取消点赞操作失败", err.Error())
statuscode = 1
statusmsg = "取消点赞操作失败"
return &pb.DouyinFavoriteActionResponse{
StatusCode: &statuscode,
StatusMsg: &statusmsg,
}, nil
}
statuscode = 0
statusmsg = "取消点赞成功"
return &pb.DouyinFavoriteActionResponse{
StatusCode: &statuscode,
StatusMsg: &statusmsg,
}, nil
}
func InitConfig() {
workDir, _ := os.Getwd() //获取当前工作路径,非文件路径,以终端显示路径为准
viper.SetConfigName("config")
viper.SetConfigType("yml")
viper.AddConfigPath(workDir + "/config")
err := viper.ReadInConfig()
if err != nil {
panic(err)
}
}
func main() {
log.Println("正在启动FavoriteAction服务......")
InitConfig()
model.InitDB()
model.InitRedis()
//初始化grpc实例
grpcServer := grpc.NewServer()
//注册服务
pb.RegisterFavoriteActionServer(grpcServer, new(FavoriteAction))
//设置监听
listen, err := net.Listen("tcp", ":12355")
if err != nil {
log.Println("注册服务启动监听失败")
}
defer listen.Close()
//启动服务
grpcServer.Serve(listen)
}