ms-tester/main.go

110 lines
3.1 KiB
Go
Raw 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"
2024-11-01 18:22:43 +00:00
problemsSub "git.sch9.ru/new_gate/ms-tester/internal/problems/delivery/rabbitmq"
2024-10-13 16:32:11 +00:00
problemsRepository "git.sch9.ru/new_gate/ms-tester/internal/problems/repository"
problemsUseCase "git.sch9.ru/new_gate/ms-tester/internal/problems/usecase"
2024-11-01 18:22:43 +00:00
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"
2024-10-09 18:55:16 +00:00
"git.sch9.ru/new_gate/ms-tester/pkg/external/pandoc"
2024-11-01 18:22:43 +00:00
"git.sch9.ru/new_gate/ms-tester/pkg/external/postgres"
"git.sch9.ru/new_gate/ms-tester/pkg/external/rabbitmq"
2024-08-20 22:54:46 +00:00
"github.com/ilyakaznacheev/cleanenv"
"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))
}
2024-11-01 18:22:43 +00:00
db, err := postgres.NewPostgresDB(cfg.PostgresDSN)
2024-08-20 22:54:46 +00:00
if err != nil {
panic(err)
}
defer db.Close()
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-11-01 18:22:43 +00:00
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)
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)
2024-11-01 18:22:43 +00:00
testerDelivery.NewTesterHandlers(gserver, testerUC)
2024-10-13 16:32:11 +00:00
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
}
}()
2024-11-01 18:22:43 +00:00
fmt.Println("server started")
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
}