ms-tester/main.go

82 lines
2.2 KiB
Go
Raw Permalink Normal View History

2024-07-27 07:31:04 +00:00
package main
2024-08-20 22:54:46 +00:00
import (
"fmt"
2024-10-09 18:55:16 +00:00
"git.sch9.ru/new_gate/ms-tester/config"
2024-10-13 16:32:11 +00:00
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"
2024-10-09 18:55:16 +00:00
"git.sch9.ru/new_gate/ms-tester/pkg/external/pandoc"
2024-08-20 22:54:46 +00:00
"github.com/ilyakaznacheev/cleanenv"
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/jmoiron/sqlx"
"go.uber.org/zap"
"google.golang.org/grpc"
2024-10-13 16:32:11 +00:00
"google.golang.org/grpc/reflection"
2024-08-20 22:54:46 +00:00
"net"
"net/http"
"os"
"os/signal"
"syscall"
)
2024-07-27 07:31:04 +00:00
func main() {
2024-10-09 18:55:16 +00:00
var cfg config.Config
2024-08-20 22:54:46 +00:00
err := cleanenv.ReadConfig(".env", &cfg)
if err != nil {
panic(fmt.Sprintf("error reading config: %s", err.Error()))
}
var logger *zap.Logger
if cfg.Env == "prod" {
logger = zap.Must(zap.NewProduction())
} else if cfg.Env == "dev" {
logger = zap.Must(zap.NewDevelopment())
} else {
panic(fmt.Sprintf(`error reading config: env expected "prod" or "dev", got "%s"`, cfg.Env))
}
db, err := sqlx.Connect("pgx", cfg.PostgresDSN)
if err != nil {
panic(err)
}
defer db.Close()
//contestStorage := storage.NewContestStorage(db, logger)
//contestService := services.NewContestService(contestStorage)
2024-10-09 18:55:16 +00:00
pandocClient := pandoc.NewPandocClient(&http.Client{}, cfg.Pandoc)
2024-08-20 22:54:46 +00:00
2024-10-13 16:32:11 +00:00
problemRepo := problemsRepository.NewProblemRepository(db, logger)
problemUC := problemsUseCase.NewProblemUseCase(problemRepo, pandocClient)
2024-08-25 10:03:00 +00:00
2024-10-13 16:32:11 +00:00
contestRepo := contestsRepository.NewContestRepository(db, logger)
contestUC := contestsUseCase.NewContestUseCase(contestRepo)
2024-08-20 22:54:46 +00:00
2024-10-13 16:32:11 +00:00
gserver := grpc.NewServer()
defer gserver.GracefulStop()
2024-08-20 22:54:46 +00:00
2024-10-13 16:32:11 +00:00
problemsDelivery.NewProblemHandlers(gserver, problemUC)
contestsDelivery.NewContestHandlers(gserver, contestUC)
reflection.Register(gserver)
2024-08-20 22:54:46 +00:00
2024-10-13 16:32:11 +00:00
ln, err := net.Listen("tcp", cfg.Address)
2024-08-20 22:54:46 +00:00
if err != nil {
panic(err)
}
go func() {
2024-10-13 16:32:11 +00:00
if err = gserver.Serve(ln); err != nil {
panic(err)
2024-08-20 22:54:46 +00:00
}
}()
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGTERM, syscall.SIGINT)
<-stop
2024-07-27 07:31:04 +00:00
}