ms-auth/main.go
Vyacheslav1557 93eaf89d78 refactor:
2024-10-09 22:07:38 +05:00

77 lines
2.1 KiB
Go

package main
import (
"fmt"
"git.sch9.ru/new_gate/ms-auth/config"
sessionsDelivery "git.sch9.ru/new_gate/ms-auth/internal/sessions/delivery/grpc"
sessionsRepository "git.sch9.ru/new_gate/ms-auth/internal/sessions/repository"
sessionsUseCase "git.sch9.ru/new_gate/ms-auth/internal/sessions/usecase"
usersDelivery "git.sch9.ru/new_gate/ms-auth/internal/users/delivery/grpc"
usersRepository "git.sch9.ru/new_gate/ms-auth/internal/users/repository"
usersUseCase "git.sch9.ru/new_gate/ms-auth/internal/users/usecase"
"git.sch9.ru/new_gate/ms-auth/pkg/external/postgres"
"git.sch9.ru/new_gate/ms-auth/pkg/external/valkey"
"github.com/ilyakaznacheev/cleanenv"
_ "github.com/jackc/pgx/v5/stdlib"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"net"
"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()
vk, err := valkey.NewValkeyClient(cfg.RedisDSN)
userRepo := usersRepository.NewUserRepository(db, logger)
userUC := usersUseCase.NewUseCase(userRepo, nil, cfg)
sessionRepo := sessionsRepository.NewValkeyRepository(vk, cfg, logger)
sessionUC := sessionsUseCase.NewUseCase(sessionRepo, cfg)
gserver := grpc.NewServer(grpc.UnaryInterceptor(usersDelivery.TokenInterceptor(sessionUC)))
usersDelivery.NewUserHandlers(gserver, userUC)
sessionsDelivery.NewSessionHandlers(gserver, sessionUC, userUC)
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
}