From e0f150bb14295965a2af10342ec5b921f6af9548 Mon Sep 17 00:00:00 2001 From: Vyacheslav1557 Date: Sun, 13 Oct 2024 21:32:11 +0500 Subject: [PATCH] feat: add graceful shutdown --- main.go | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/main.go b/main.go index 40012c1..0c63e3a 100644 --- a/main.go +++ b/main.go @@ -3,17 +3,19 @@ package main import ( "fmt" "git.sch9.ru/new_gate/ms-tester/config" - "git.sch9.ru/new_gate/ms-tester/internal/services" - "git.sch9.ru/new_gate/ms-tester/internal/storage" - "git.sch9.ru/new_gate/ms-tester/internal/transport" + contestsDelivery "git.sch9.ru/new_gate/ms-tester/internal/contests/delivery/grpc" + contestsRepository "git.sch9.ru/new_gate/ms-tester/internal/contests/repository" + contestsUseCase "git.sch9.ru/new_gate/ms-tester/internal/contests/usecase" + problemsDelivery "git.sch9.ru/new_gate/ms-tester/internal/problems/delivery/grpc" + problemsRepository "git.sch9.ru/new_gate/ms-tester/internal/problems/repository" + problemsUseCase "git.sch9.ru/new_gate/ms-tester/internal/problems/usecase" "git.sch9.ru/new_gate/ms-tester/pkg/external/pandoc" - sessionv1 "git.sch9.ru/new_gate/ms-tester/pkg/go/gen/proto/session/v1" "github.com/ilyakaznacheev/cleanenv" _ "github.com/jackc/pgx/v5/stdlib" "github.com/jmoiron/sqlx" "go.uber.org/zap" "google.golang.org/grpc" - "google.golang.org/grpc/credentials/insecure" + "google.golang.org/grpc/reflection" "net" "net/http" "os" @@ -48,30 +50,27 @@ func main() { pandocClient := pandoc.NewPandocClient(&http.Client{}, cfg.Pandoc) - grpcSessionClient, err := grpc.NewClient(cfg.Auth, grpc.WithTransportCredentials(insecure.NewCredentials())) - if err != nil { - panic(err) - } - sessionClient := sessionv1.NewSessionServiceClient(grpcSessionClient) + problemRepo := problemsRepository.NewProblemRepository(db, logger) + problemUC := problemsUseCase.NewProblemUseCase(problemRepo, pandocClient) - permissionService := services.NewPermissionService() + contestRepo := contestsRepository.NewContestRepository(db, logger) + contestUC := contestsUseCase.NewContestUseCase(contestRepo) - problemStorage := storage.NewProblemStorage(db, logger) - problemService := services.NewProblemService(problemStorage, pandocClient, permissionService) + gserver := grpc.NewServer() + defer gserver.GracefulStop() - userStorage := storage.NewUserStorage(db) - userService := services.NewUserService(userStorage) + problemsDelivery.NewProblemHandlers(gserver, problemUC) + contestsDelivery.NewContestHandlers(gserver, contestUC) + reflection.Register(gserver) - server := transport.NewTesterServer(problemService, sessionClient, userService, logger) - - lis, err := net.Listen("tcp", cfg.Address) + ln, err := net.Listen("tcp", cfg.Address) if err != nil { panic(err) } go func() { - if err := server.Start(lis); err != nil { - logger.Fatal("error starting server", zap.Error(err)) + if err = gserver.Serve(ln); err != nil { + panic(err) } }()