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 ( import (
"fmt" "fmt"
"git.sch9.ru/new_gate/ms-tester/config" "git.sch9.ru/new_gate/ms-tester/config"
"git.sch9.ru/new_gate/ms-tester/internal/services" contestsDelivery "git.sch9.ru/new_gate/ms-tester/internal/contests/delivery/grpc"
"git.sch9.ru/new_gate/ms-tester/internal/storage" contestsRepository "git.sch9.ru/new_gate/ms-tester/internal/contests/repository"
"git.sch9.ru/new_gate/ms-tester/internal/transport" 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" "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/ilyakaznacheev/cleanenv"
_ "github.com/jackc/pgx/v5/stdlib" _ "github.com/jackc/pgx/v5/stdlib"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"go.uber.org/zap" "go.uber.org/zap"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/reflection"
"net" "net"
"net/http" "net/http"
"os" "os"
@ -48,30 +50,27 @@ func main() {
pandocClient := pandoc.NewPandocClient(&http.Client{}, cfg.Pandoc) pandocClient := pandoc.NewPandocClient(&http.Client{}, cfg.Pandoc)
grpcSessionClient, err := grpc.NewClient(cfg.Auth, grpc.WithTransportCredentials(insecure.NewCredentials())) problemRepo := problemsRepository.NewProblemRepository(db, logger)
if err != nil { problemUC := problemsUseCase.NewProblemUseCase(problemRepo, pandocClient)
panic(err)
}
sessionClient := sessionv1.NewSessionServiceClient(grpcSessionClient)
permissionService := services.NewPermissionService() contestRepo := contestsRepository.NewContestRepository(db, logger)
contestUC := contestsUseCase.NewContestUseCase(contestRepo)
problemStorage := storage.NewProblemStorage(db, logger) gserver := grpc.NewServer()
problemService := services.NewProblemService(problemStorage, pandocClient, permissionService) defer gserver.GracefulStop()
userStorage := storage.NewUserStorage(db) problemsDelivery.NewProblemHandlers(gserver, problemUC)
userService := services.NewUserService(userStorage) contestsDelivery.NewContestHandlers(gserver, contestUC)
reflection.Register(gserver)
server := transport.NewTesterServer(problemService, sessionClient, userService, logger) ln, err := net.Listen("tcp", cfg.Address)
lis, err := net.Listen("tcp", cfg.Address)
if err != nil { if err != nil {
panic(err) panic(err)
} }
go func() { go func() {
if err := server.Start(lis); err != nil { if err = gserver.Serve(ln); err != nil {
logger.Fatal("error starting server", zap.Error(err)) panic(err)
} }
}() }()