feat: add graceful shutdown

This commit is contained in:
Vyacheslav1557 2024-10-13 21:32:11 +05:00
parent 07f89b5ab1
commit e0f150bb14

39
main.go
View file

@ -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)
}
}()