Skip to content

Commit 64eab89

Browse files
committed
Added gRPC thread-comment connection when deleting threads
1 parent 286ec5e commit 64eab89

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

code/docker-compose.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ services:
109109
DB_SERVICE_PORT: ${DB_SERVICE_PORT}
110110
COMMUNITY_SERVICE_HOST: community-service
111111
COMMUNITY_SERVICE_PORT: ${COMMUNITY_SERVICE_PORT}
112+
COMMENT_SERVICE_HOST: comment-service
113+
COMMENT_SERVICE_PORT: ${COMMENT_SERVICE_PORT}
112114
ports:
113115
- "${THREAD_SERVICE_PORT}:${THREAD_SERVICE_PORT}"
114116
networks:

code/kubernetes/services/thread-service/deployment.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ spec:
4545
configMapKeyRef:
4646
name: threadit-config
4747
key: COMMUNITY_SERVICE_PORT
48+
- name: COMMENT_SERVICE_HOST
49+
value: "comment-service"
50+
- name: COMMENT_SERVICE_PORT
51+
valueFrom:
52+
configMapKeyRef:
53+
name: threadit-config
54+
key: COMMENT_SERVICE_PORT
4855
readinessProbe:
4956
tcpSocket:
5057
port: 50053

code/services/thread-service/main.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package main
22

33
import (
4-
"context"
54
"fmt"
5+
commentpb "gen/comment-service/pb"
66
communitypb "gen/community-service/pb"
77
dbpb "gen/db-service/pb"
88
threadpb "gen/thread-service/pb"
@@ -47,10 +47,15 @@ func main() {
4747
communityConn := connectGrpcClient("COMMUNITY_SERVICE_HOST", "COMMUNITY_SERVICE_PORT")
4848
defer communityConn.Close()
4949

50+
// connect to comment service
51+
commentConn := connectGrpcClient("COMMENT_SERVICE_HOST", "COMMENT_SERVICE_PORT")
52+
defer commentConn.Close()
53+
5054
// create thread service with database service
5155
threadService := &server.ThreadServer{
5256
DBClient: dbpb.NewDBServiceClient(dbConn),
5357
CommunityClient: communitypb.NewCommunityServiceClient(communityConn),
58+
CommentClient: commentpb.NewCommentServiceClient(commentConn),
5459
}
5560

5661
// get env port
@@ -71,13 +76,6 @@ func main() {
7176
)
7277
threadpb.RegisterThreadServiceServer(grpcServer, threadService)
7378

74-
// hardcode a thread creation
75-
threadService.CreateThread(context.Background(), &threadpb.CreateThreadRequest{
76-
CommunityId: "1",
77-
Title: "Hello World",
78-
Content: "This is a test thread",
79-
})
80-
8179
log.Printf("gRPC server is listening on :%s", port)
8280
if err := grpcServer.Serve(lis); err != nil {
8381
log.Fatalf("failed to serve: %v", err)

code/services/thread-service/src/server.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package server
22

33
import (
44
"context"
5+
commentpb "gen/comment-service/pb"
56
communitypb "gen/community-service/pb"
67
dbpb "gen/db-service/pb"
78
models "gen/models/pb"
@@ -17,6 +18,7 @@ type ThreadServer struct {
1718
threadpb.UnimplementedThreadServiceServer
1819
CommunityClient communitypb.CommunityServiceClient
1920
DBClient dbpb.DBServiceClient
21+
CommentClient commentpb.CommentServiceClient
2022
}
2123

2224
const (
@@ -172,23 +174,23 @@ func (s *ThreadServer) DeleteThread(ctx context.Context, req *threadpb.DeleteThr
172174
}
173175

174176
// find all comments from thread
175-
comments, err := s.DBClient.ListComments(ctx, &dbpb.ListCommentsRequest{
177+
comments, err := s.CommentClient.ListComments(ctx, &commentpb.ListCommentsRequest{
176178
ThreadId: &req.Id,
177179
})
178180
if err != nil {
179181
return nil, err
180182
}
181-
183+
182184
// delete comments
183185
for _, comment := range comments.Comments {
184-
_, err = s.DBClient.DeleteComment(ctx, &dbpb.DeleteCommentRequest{
186+
_, err = s.CommentClient.DeleteComment(ctx, &commentpb.DeleteCommentRequest{
185187
Id: comment.Id,
186188
})
187189
if err != nil {
188190
return nil, err
189191
}
190192
}
191-
193+
192194
// delete thread
193195
_, err = s.DBClient.DeleteThread(ctx, &dbpb.DeleteThreadRequest{
194196
Id: req.Id,

0 commit comments

Comments
 (0)