ms-tester/main.go
Vyacheslav1557 c2076338fa feat:
2024-11-01 23:22:43 +05:00

110 lines
3.1 KiB
Go

package main
import (
"fmt"
"git.sch9.ru/new_gate/ms-tester/config"
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"
problemsSub "git.sch9.ru/new_gate/ms-tester/internal/problems/delivery/rabbitmq"
problemsRepository "git.sch9.ru/new_gate/ms-tester/internal/problems/repository"
problemsUseCase "git.sch9.ru/new_gate/ms-tester/internal/problems/usecase"
testerDelivery "git.sch9.ru/new_gate/ms-tester/internal/tester/delivery/grpc"
testerPubSub "git.sch9.ru/new_gate/ms-tester/internal/tester/delivery/rabbitmq"
testerUseCase "git.sch9.ru/new_gate/ms-tester/internal/tester/usecase"
"git.sch9.ru/new_gate/ms-tester/pkg/external/pandoc"
"git.sch9.ru/new_gate/ms-tester/pkg/external/postgres"
"git.sch9.ru/new_gate/ms-tester/pkg/external/rabbitmq"
"github.com/ilyakaznacheev/cleanenv"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"net"
"net/http"
"os"
"os/signal"
"syscall"
)
func main() {
var cfg config.Config
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 := postgres.NewPostgresDB(cfg.PostgresDSN)
if err != nil {
panic(err)
}
defer db.Close()
pandocClient := pandoc.NewPandocClient(&http.Client{}, cfg.Pandoc)
problemRepo := problemsRepository.NewProblemRepository(db, logger)
problemUC := problemsUseCase.NewProblemUseCase(problemRepo, pandocClient)
contestRepo := contestsRepository.NewContestRepository(db, logger)
contestUC := contestsUseCase.NewContestUseCase(contestRepo)
testerUC := testerUseCase.NewTesterUseCase()
conn, err := rabbitmq.NewRabbitClient(cfg.RabbitDSN)
if err != nil {
panic(err)
}
defer conn.Close()
ch, err := conn.Channel()
if err != nil {
panic(err)
}
defer ch.Close()
err = ch.Qos(
1,
0,
true,
)
if err != nil {
panic(err)
}
problemsSub.NewNotificationSubscriber(ch, cfg.NQueueName, cfg.InstanceName, problemUC)
testerPubSub.NewTesterProducer(ch, cfg.TQueueName, cfg.RQueueName, testerUC)
testerPubSub.NewTesterConsumer(ch, cfg.RQueueName, cfg.InstanceName, testerUC)
gserver := grpc.NewServer()
defer gserver.GracefulStop()
problemsDelivery.NewProblemHandlers(gserver, problemUC)
contestsDelivery.NewContestHandlers(gserver, contestUC)
testerDelivery.NewTesterHandlers(gserver, testerUC)
reflection.Register(gserver)
ln, err := net.Listen("tcp", cfg.Address)
if err != nil {
panic(err)
}
go func() {
if err = gserver.Serve(ln); err != nil {
panic(err)
}
}()
fmt.Println("server started")
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGTERM, syscall.SIGINT)
<-stop
}