-
Notifications
You must be signed in to change notification settings - Fork 1
/
publistService.go
182 lines (173 loc) · 4.63 KB
/
publistService.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
package main
import (
"context"
"fmt"
"log"
"net"
"os"
_ "github.com/CodyGuo/godaemon"
"github.com/chaossat/tiktak/common"
"github.com/chaossat/tiktak/db"
"github.com/chaossat/tiktak/middleware"
"github.com/chaossat/tiktak/oss"
feedmodel "github.com/chaossat/tiktak/service/feed/model"
"github.com/chaossat/tiktak/service/publist/pb"
"github.com/spf13/viper"
"google.golang.org/grpc"
)
type PublistServer struct {
pb.UnimplementedPublishServer
}
// 获取发布的video列表
func (p *PublistServer) PublishVideo(ctx context.Context, req *pb.DouyinPublishListRequest) (*pb.DouyinPublishListResponse, error) {
// token验证
_, err := middleware.CheckToken(*req.Token)
if err != nil {
var code int32 = -1
var msg string = "验证失败!"
return &pb.DouyinPublishListResponse{
StatusCode: &code,
StatusMsg: &msg,
VideoList: nil,
}, nil
}
userinf, err := db.UserInfoById(int(*req.UserId))
if err != nil {
var code int32 = -2
var msg string = "查询用户信息失败!"
return &pb.DouyinPublishListResponse{
StatusCode: &code,
StatusMsg: &msg,
VideoList: nil,
}, nil
}
if userinf.ID == 0 {
var code int32 = -3
var msg string = "对应id的用户不存在!"
return &pb.DouyinPublishListResponse{
StatusCode: &code,
StatusMsg: &msg,
VideoList: nil,
}, nil
}
videos, err := db.VideoedByID(int(*req.UserId))
if err != nil {
var code int32 = -4
var msg string = "查询用户视频失败!"
return &pb.DouyinPublishListResponse{
StatusCode: &code,
StatusMsg: &msg,
VideoList: nil,
}, nil
}
isfollow := false
followCount, err := db.FollowCountByID(int(userinf.ID))
if err != nil {
var code int32 = -5
var msg string = "查询关注数量失败!"
return &pb.DouyinPublishListResponse{
StatusCode: &code,
StatusMsg: &msg,
VideoList: nil,
}, nil
}
followerCount, err := db.FollowerCountByID(int(userinf.ID))
if err != nil {
var code int32 = -6
var msg string = "查询粉丝数量失败!"
return &pb.DouyinPublishListResponse{
StatusCode: &code,
StatusMsg: &msg,
VideoList: nil,
}, nil
}
var user = pb.User{
Id: &userinf.ID,
Name: &userinf.Username,
FollowCount: &followCount,
FollowerCount: &followerCount,
IsFollow: &isfollow,
}
var vide_ans = make([]*pb.Video, len(videos))
for i := 0; i < len(videos); i++ {
var isfabvorite = false
playURL := oss.GetURL(videos[i].Location)
coverURL := oss.GetURL(videos[i].Cover_location)
favoritecnt, err := feedmodel.GetFavoriteCount(videos[i].ID)
if err != nil {
log.Println("获取视频的点赞个数错误", err.Error())
var code int32 = -7
var msg string = "获取视频的点赞个数错误!"
return &pb.DouyinPublishListResponse{
StatusCode: &code,
StatusMsg: &msg,
VideoList: nil,
}, nil
}
cCount := int64(0)
vide_ans[i] = &pb.Video{
Id: &videos[i].ID,
Author: &user,
PlayUrl: &playURL,
CoverUrl: &coverURL,
FavoriteCount: &favoritecnt,
CommentCount: &cCount,
IsFavorite: &isfabvorite,
Title: &videos[i].Title,
}
}
if err != nil {
var code int32 = -8
var msg string = "视频查找失败!"
return &pb.DouyinPublishListResponse{
StatusCode: &code,
StatusMsg: &msg,
VideoList: nil,
}, nil
}
var code int32 = 0
var msg string = "验证成功!"
res := &pb.DouyinPublishListResponse{
StatusCode: &code,
StatusMsg: &msg,
VideoList: vide_ans,
}
return res, nil
}
func main() {
log.Println("正在启动Publist服务......")
InitConfig()
common.InitDB()
feedmodel.InitRedis()
//初始化grpc实例
grpcServer := grpc.NewServer()
//注册服务
pb.RegisterPublishServer(grpcServer, new(PublistServer))
//设置监听
listen, err := net.Listen("tcp", ":10002")
if err != nil {
log.Println("注册服务启动监听失败")
}
defer listen.Close()
//启动服务
grpcServer.Serve(listen)
}
func InitConfig() {
workDir, _ := os.Getwd() //获取当前工作路径,非文件路径,以终端显示路径为准
viper.SetConfigName("config.yml")
viper.SetConfigType("yml")
viper.AddConfigPath(workDir + "/config")
fmt.Println("configPath:", workDir+"/config")
err := viper.ReadInConfig()
fmt.Println(
"host:", viper.GetString("datasource.mysql.host"), "\r\n",
"port:", viper.GetString("datasource.mysql.port"), "\r\n",
"database:", viper.GetString("datasource.mysql.database"), "\r\n",
"username:", viper.GetString("datasource.mysql.username"), "\r\n",
"passowrd:", viper.GetString("datasource.mysql.password"), "\r\n",
"charset:", viper.GetString("datasource.mysql.charset"),
)
if err != nil {
panic(err)
}
}