From 0cd12f8948c5cb2a25ee5912bc0febacab45828a Mon Sep 17 00:00:00 2001 From: tituschewxj Date: Fri, 25 Oct 2024 04:26:50 +0800 Subject: [PATCH] refactor: move to main.go --- apps/question-service/grpc.go | 37 ----------------------------------- apps/question-service/main.go | 32 +++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 38 deletions(-) delete mode 100644 apps/question-service/grpc.go diff --git a/apps/question-service/grpc.go b/apps/question-service/grpc.go deleted file mode 100644 index e9b0166423..0000000000 --- a/apps/question-service/grpc.go +++ /dev/null @@ -1,37 +0,0 @@ -package main - -import ( - "context" - "log" - "net" - pb "proto/questionmatching" - - "google.golang.org/grpc" -) - -type server struct { - pb.UnimplementedQuestionMatchingServiceServer // Embed the unimplemented service -} - -func initGrpcServer() { - lis, err := net.Listen("tcp", ":50051") - if err != nil { - log.Fatalf("failed to listen: %v", err) - } - s := grpc.NewServer() - pb.RegisterQuestionMatchingServiceServer(s, &server{}) - - log.Printf("gRPC Server is listening on port 50051...") - if err := s.Serve(lis); err != nil { - log.Fatalf("failed to serve: %v", err) - } -} - -func (s *server) FindMatchingQuestion(ctx context.Context, req *pb.MatchQuestionRequest) (*pb.QuestionFound, error) { - return &pb.QuestionFound{ - QuestionId: 1, - QuestionName: "abc", - QuestionDifficulty: "Easy", - QuestionTopics: []string{"Algorithms", "Arrays"}, - }, nil -} diff --git a/apps/question-service/main.go b/apps/question-service/main.go index 9d17025895..55ffb25c8c 100644 --- a/apps/question-service/main.go +++ b/apps/question-service/main.go @@ -5,8 +5,10 @@ import ( "flag" "fmt" "log" + "net" "net/http" "os" + pb "proto/questionmatching" "question-service/handlers" mymiddleware "question-service/middleware" "question-service/utils" @@ -19,6 +21,7 @@ import ( "github.com/go-chi/cors" "github.com/joho/godotenv" "google.golang.org/api/option" + "google.golang.org/grpc" ) // initFirestore initializes the Firestore client @@ -100,9 +103,36 @@ func main() { } // Start the server - log.Printf("Starting server on http://localhost:%s", port) + log.Printf("Starting REST server on http://localhost:%s", port) err = http.ListenAndServe(fmt.Sprintf(":%s", port), r) if err != nil { log.Fatalf("Failed to start server: %v", err) } } + +type server struct { + pb.UnimplementedQuestionMatchingServiceServer // Embed the unimplemented service +} + +func initGrpcServer() { + lis, err := net.Listen("tcp", ":50051") + if err != nil { + log.Fatalf("failed to listen: %v", err) + } + s := grpc.NewServer() + pb.RegisterQuestionMatchingServiceServer(s, &server{}) + + log.Printf("gRPC Server is listening on port 50051...") + if err := s.Serve(lis); err != nil { + log.Fatalf("failed to serve: %v", err) + } +} + +func (s *server) FindMatchingQuestion(ctx context.Context, req *pb.MatchQuestionRequest) (*pb.QuestionFound, error) { + return &pb.QuestionFound{ + QuestionId: 1, + QuestionName: "abc", + QuestionDifficulty: "Easy", + QuestionTopics: []string{"Algorithms", "Arrays"}, + }, nil +}