Skip to content

Commit 922f336

Browse files
committed
feat: grpc client-server connection
1 parent 8ba4883 commit 922f336

File tree

6 files changed

+58
-37
lines changed

6 files changed

+58
-37
lines changed

apps/matching-service/handlers/responses.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func sendRejectionResponse(ws *websocket.Conn) {
2828
}
2929

3030
// Send message to matched user
31-
func sendMatchFoundResponse(ws *websocket.Conn, username string, result models.MatchFound) {
31+
func sendMatchFoundResponse(ws *websocket.Conn, username string, result models.MatchQuestionFound) {
3232
if err := ws.WriteJSON(result); err != nil {
3333
log.Printf("Error sending message to user %s: %v\n", username, err)
3434
}

apps/matching-service/handlers/websocket.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func waitForResult(ws *websocket.Conn, userCtx, timeoutCtx context.Context, matc
139139
if !ok {
140140
return
141141
}
142-
var result models.MatchFound
142+
var result models.MatchQuestionFound
143143
// Unmarshal the JSON message into the struct
144144
err := json.Unmarshal([]byte(msg.Payload), &result)
145145
if err != nil {

apps/matching-service/processes/findmatches.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func foundMatch(tx *redis.Tx, ctx context.Context, currentUser *models.MatchRequ
8585
for _, topic := range currentUser.Difficulties {
8686
for _, otherTopic := range matchedUser.Difficulties {
8787
if topic == otherTopic {
88-
matchedTopics = append(matchedTopics, topic)
88+
matchedDifficulties = append(matchedDifficulties, topic)
8989
}
9090
}
9191
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package handlers
2+
3+
import (
4+
"context"
5+
pb "proto/questionmatching"
6+
)
7+
8+
func (s *GrpcServer) FindMatchingQuestion(ctx context.Context, req *pb.MatchQuestionRequest) (*pb.QuestionFound, error) {
9+
// STUB
10+
return &pb.QuestionFound{
11+
QuestionId: 1,
12+
QuestionName: "abc",
13+
QuestionDifficulty: "Easy",
14+
QuestionTopics: []string{"Algorithms", "Arrays"},
15+
}, nil
16+
}
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
package handlers
22

3-
import "cloud.google.com/go/firestore"
3+
import (
4+
pb "proto/questionmatching"
5+
6+
"cloud.google.com/go/firestore"
7+
)
48

59
type Service struct {
610
Client *firestore.Client
711
}
12+
13+
type GrpcServer struct {
14+
pb.UnimplementedQuestionMatchingServiceServer // Embed the unimplemented service
15+
}

apps/question-service/main.go

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,6 @@ import (
2424
"google.golang.org/grpc"
2525
)
2626

27-
// initFirestore initializes the Firestore client
28-
func initFirestore(ctx context.Context, credentialsPath string) (*firestore.Client, error) {
29-
opt := option.WithCredentialsFile(credentialsPath)
30-
app, err := firebase.NewApp(ctx, nil, opt)
31-
if err != nil {
32-
return nil, fmt.Errorf("failed to initialize Firebase App: %v", err)
33-
}
34-
35-
client, err := app.Firestore(ctx)
36-
if err != nil {
37-
return nil, fmt.Errorf("failed to get Firestore client: %v", err)
38-
}
39-
return client, nil
40-
}
41-
4227
func main() {
4328
// Load .env file
4429
err := godotenv.Load()
@@ -67,7 +52,26 @@ func main() {
6752

6853
go initGrpcServer()
6954

70-
// Set up chi router
55+
r := initChiRouter(service)
56+
initRestServer(r)
57+
}
58+
59+
// initFirestore initializes the Firestore client
60+
func initFirestore(ctx context.Context, credentialsPath string) (*firestore.Client, error) {
61+
opt := option.WithCredentialsFile(credentialsPath)
62+
app, err := firebase.NewApp(ctx, nil, opt)
63+
if err != nil {
64+
return nil, fmt.Errorf("failed to initialize Firebase App: %v", err)
65+
}
66+
67+
client, err := app.Firestore(ctx)
68+
if err != nil {
69+
return nil, fmt.Errorf("failed to get Firestore client: %v", err)
70+
}
71+
return client, nil
72+
}
73+
74+
func initChiRouter(service *handlers.Service) *chi.Mux {
7175
r := chi.NewRouter()
7276
r.Use(middleware.Logger)
7377
r.Use(middleware.Timeout(60 * time.Second))
@@ -84,7 +88,12 @@ func main() {
8488
MaxAge: 300, // Maximum value not ignored by any of major browsers
8589
}))
8690

87-
// Register routes
91+
registerRoutes(r, service)
92+
93+
return r
94+
}
95+
96+
func registerRoutes(r *chi.Mux, service *handlers.Service) {
8897
r.Route("/questions", func(r chi.Router) {
8998
r.Get("/", service.ListQuestions)
9099
r.Post("/", service.CreateQuestion)
@@ -95,7 +104,9 @@ func main() {
95104
r.Delete("/", service.DeleteQuestion)
96105
})
97106
})
107+
}
98108

109+
func initRestServer(r *chi.Mux) {
99110
// Serve on port 8080
100111
port := os.Getenv("PORT")
101112
if port == "" {
@@ -104,36 +115,22 @@ func main() {
104115

105116
// Start the server
106117
log.Printf("Starting REST server on http://localhost:%s", port)
107-
err = http.ListenAndServe(fmt.Sprintf(":%s", port), r)
118+
err := http.ListenAndServe(fmt.Sprintf(":%s", port), r)
108119
if err != nil {
109120
log.Fatalf("Failed to start server: %v", err)
110121
}
111122
}
112123

113-
type server struct {
114-
pb.UnimplementedQuestionMatchingServiceServer // Embed the unimplemented service
115-
}
116-
117124
func initGrpcServer() {
118125
lis, err := net.Listen("tcp", ":50051")
119126
if err != nil {
120127
log.Fatalf("failed to listen: %v", err)
121128
}
122129
s := grpc.NewServer()
123-
pb.RegisterQuestionMatchingServiceServer(s, &server{})
130+
pb.RegisterQuestionMatchingServiceServer(s, &handlers.GrpcServer{})
124131

125132
log.Printf("gRPC Server is listening on port 50051...")
126133
if err := s.Serve(lis); err != nil {
127134
log.Fatalf("failed to serve: %v", err)
128135
}
129136
}
130-
131-
func (s *server) FindMatchingQuestion(ctx context.Context, req *pb.MatchQuestionRequest) (*pb.QuestionFound, error) {
132-
// STUB
133-
return &pb.QuestionFound{
134-
QuestionId: 1,
135-
QuestionName: "abc",
136-
QuestionDifficulty: "Easy",
137-
QuestionTopics: []string{"Algorithms", "Arrays"},
138-
}, nil
139-
}

0 commit comments

Comments
 (0)