-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
216 lines (203 loc) · 5.14 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"context"
"fmt"
"log"
"net"
"os"
"time"
_ "github.com/CodyGuo/godaemon"
"github.com/chaossat/tiktak/common"
"github.com/chaossat/tiktak/db"
"github.com/chaossat/tiktak/middleware"
"github.com/chaossat/tiktak/oss"
"github.com/chaossat/tiktak/service/feed/model"
"github.com/chaossat/tiktak/service/feed/pb"
"github.com/spf13/viper"
"google.golang.org/grpc"
)
type Feed struct {
}
//本次返回的视频中发布最早的时间,作为下次请求时的latest_time
func GetNextTime(videos []model.Video) int64 {
if len(videos) == 0 {
return time.Now().Unix()
}
ans := videos[0].UpdateTime
for _, video := range videos {
if ans > video.UpdateTime {
ans = video.UpdateTime
}
}
return ans
}
//获取作者相关信息
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("查询视频作者错误", authorid, err.Error())
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 (this *Feed) GetFeed(ctx context.Context, req *pb.DouyinFeedRequest) (*pb.DouyinFeedResponse, error) {
var statuscode int32
var statusmsg string
var userid int64
fmt.Println("LatestTime:", *req.LatestTime)
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.DouyinFeedResponse{
StatusCode: &statuscode,
StatusMsg: &statusmsg,
}, nil
}
userid = claims.UserID
} else {
userid = 0
}
video_list, err := model.GetVideoList(*req.LatestTime)
if err != nil {
statuscode = 1
statusmsg = "获取视频列表失败"
return &pb.DouyinFeedResponse{
StatusCode: &statuscode,
StatusMsg: &statusmsg,
}, nil
}
log.Println("获取到的视频数:", len(video_list))
var pbvideo_list []*pb.Video
for _, video := range video_list {
Id := video.ID
Title := video.Title
Author := GetAuthor(userid, video.AuthorID)
PlayUrl := oss.GetURL(video.Location)
CoverUrl := oss.GetURL(video.Cover_location)
//favoritecnt := int64(0)
favoritecnt, err := model.GetFavoriteCount(Id)
if err != nil {
log.Println("获取视频的点赞个数错误", err.Error())
statuscode = 1
statusmsg = "获取视频点赞失败"
return &pb.DouyinFeedResponse{
StatusCode: &statuscode,
StatusMsg: &statusmsg,
}, nil
}
FavoriteCount := favoritecnt
// commentcnt := 0
commentcnt, err := db.CommentCountByVID(int(Id))
if err != nil {
log.Println("获取评论数错误:", err.Error())
statuscode = 1
statusmsg = "判断用户是否点赞当前视频错误"
return &pb.DouyinFeedResponse{
StatusCode: &statuscode,
StatusMsg: &statusmsg,
}, nil
}
CommentCount := int64(commentcnt)
var isfavorite bool
if userid != 0 {
isfavorite, err = model.IsFavorite(userid, video.ID)
if err != nil {
log.Println("判断用户是否点赞当前视频错误")
statuscode = 1
statusmsg = "判断用户是否点赞当前视频错误"
return &pb.DouyinFeedResponse{
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 = "获取视频流成功"
nexttime := GetNextTime(video_list)
return &pb.DouyinFeedResponse{
StatusCode: &statuscode,
StatusMsg: &statusmsg,
VideoList: pbvideo_list,
NextTime: &nexttime,
}, 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("正在启动Feed服务......")
InitConfig()
common.InitDB()
model.InitDB()
model.InitRedis()
//初始化grpc实例
grpcServer := grpc.NewServer()
//注册服务
pb.RegisterFeedServer(grpcServer, new(Feed))
//设置监听
listen, err := net.Listen("tcp", ":12346")
if err != nil {
log.Println("注册服务启动监听失败")
}
defer listen.Close()
//启动服务
grpcServer.Serve(listen)
}