82 lines
2.2 KiB
Go
82 lines
2.2 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"
|
|
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"
|
|
"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/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 := sqlx.Connect("pgx", cfg.PostgresDSN)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer db.Close()
|
|
|
|
//contestStorage := storage.NewContestStorage(db, logger)
|
|
//contestService := services.NewContestService(contestStorage)
|
|
|
|
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)
|
|
|
|
gserver := grpc.NewServer()
|
|
defer gserver.GracefulStop()
|
|
|
|
problemsDelivery.NewProblemHandlers(gserver, problemUC)
|
|
contestsDelivery.NewContestHandlers(gserver, contestUC)
|
|
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)
|
|
}
|
|
}()
|
|
|
|
stop := make(chan os.Signal, 1)
|
|
signal.Notify(stop, syscall.SIGTERM, syscall.SIGINT)
|
|
|
|
<-stop
|
|
}
|