-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
181 lines (169 loc) · 4.35 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"context"
"log"
"net"
"os"
_ "github.com/CodyGuo/godaemon"
"github.com/chaossat/tiktak/middleware"
"github.com/chaossat/tiktak/oss"
"github.com/chaossat/tiktak/service/favoritelist/model"
"github.com/chaossat/tiktak/service/favoritelist/pb"
"github.com/spf13/viper"
"google.golang.org/grpc"
)
type FavoriteList struct {
}
//获取作者相关信息
func GetAuthor(userid, authorid int64) pb.User {
var user model.User
var pbauthor pb.User
if userid != 0 {
var err error
user, err = model.GetUser(userid)
if err != nil {
log.Println("查询当前用户错误")
return pbauthor
}
}
author, err := model.GetUser(authorid)
if err != nil {
log.Println("查询视频作者错误")
return pbauthor
}
Id := author.ID
Name := author.Username
followcount := model.GetFollowCount(author)
FollowCount := followcount
followercount := model.GetFollowerCount(author)
FollowerCount := followercount
var isfollow bool
if userid == 0 {
isfollow = false
} else {
isfollow, err = model.IsFollow(user, author)
if err != nil {
isfollow = false
}
}
IsFollow := isfollow
pbauthor = pb.User{
Id: &Id,
Name: &Name,
FollowCount: &FollowCount,
FollowerCount: &FollowerCount,
IsFollow: &IsFollow,
}
return pbauthor
}
func (f *FavoriteList) GetFavoriteList(ctx context.Context, req *pb.DouyinFavoriteListRequest) (*pb.DouyinFavoriteListResponse, error) {
var statuscode int32
var statusmsg string
user_id := *req.UserId
video_list, err := model.FavoriteList(user_id)
var curid int64
if len(*req.Token) > 0 {
claims, err := middleware.CheckToken(*req.Token)
if err != nil {
log.Println("解析用户token失败", err.Error())
statuscode = 1
statusmsg = "解析用户token失败"
return &pb.DouyinFavoriteListResponse{
StatusCode: &statuscode,
StatusMsg: &statusmsg,
}, nil
}
curid = claims.UserID
}
if err != nil {
statuscode = 1
statusmsg = "获取点赞过的视频列表失败"
return &pb.DouyinFavoriteListResponse{
StatusCode: &statuscode,
StatusMsg: &statusmsg,
}, nil
}
var pbvideo_list []*pb.Video
for _, video := range video_list {
Id := video.ID
Title := video.Title
Author := GetAuthor(curid, video.AuthorID)
PlayUrl := oss.GetURL(video.Location)
CoverUrl := oss.GetURL(video.Cover_location)
favoritecnt, err := model.GetFavoriteCount(Id)
if err != nil {
log.Println("获取视频的点赞个数错误", err.Error())
statuscode = 1
statusmsg = "获取视频点赞失败"
return &pb.DouyinFavoriteListResponse{
StatusCode: &statuscode,
StatusMsg: &statusmsg,
}, nil
}
FavoriteCount := favoritecnt
commentcnt := model.GetCommentCount(video)
CommentCount := commentcnt
var isfavorite bool
if curid != 0 {
isfavorite, err = model.IsFavorite(curid, video.ID)
if err != nil {
log.Println("判断用户是否点赞当前视频错误")
statuscode = 1
statusmsg = "判断用户是否点赞当前视频错误"
return &pb.DouyinFavoriteListResponse{
StatusCode: &statuscode,
StatusMsg: &statusmsg,
}, nil
}
} else {
isfavorite = false
}
IsFavorite := isfavorite
pbvideo := pb.Video{
Id: &Id,
Title: &Title,
Author: &Author,
PlayUrl: &PlayUrl,
CoverUrl: &CoverUrl,
FavoriteCount: &FavoriteCount,
CommentCount: &CommentCount,
IsFavorite: &IsFavorite,
}
pbvideo_list = append(pbvideo_list, &pbvideo)
}
statuscode = 0
statusmsg = "获取点赞过的视频成功"
return &pb.DouyinFavoriteListResponse{
StatusCode: &statuscode,
StatusMsg: &statusmsg,
VideoList: pbvideo_list,
}, 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("正在启动FavoriteList服务......")
InitConfig()
model.InitDB()
model.InitRedis()
//初始化grpc实例
grpcServer := grpc.NewServer()
//注册服务
pb.RegisterFavoriteListServer(grpcServer, new(FavoriteList))
//设置监听
listen, err := net.Listen("tcp", ":12350")
if err != nil {
log.Println("注册服务启动监听失败")
}
defer listen.Close()
//启动服务
grpcServer.Serve(listen)
}