2024-07-14 21:26:34 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-10-09 17:07:38 +00:00
|
|
|
"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"
|
2024-08-14 10:36:43 +00:00
|
|
|
"github.com/ilyakaznacheev/cleanenv"
|
|
|
|
_ "github.com/jackc/pgx/v5/stdlib"
|
2024-07-14 21:26:34 +00:00
|
|
|
"go.uber.org/zap"
|
2024-10-09 17:07:38 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/reflection"
|
2024-08-14 10:36:43 +00:00
|
|
|
"net"
|
2024-07-14 21:26:34 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-10-09 17:07:38 +00:00
|
|
|
var cfg config.Config
|
2024-08-14 10:36:43 +00:00
|
|
|
err := cleanenv.ReadConfig(".env", &cfg)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("error reading config: %s", err.Error()))
|
|
|
|
}
|
2024-07-14 21:26:34 +00:00
|
|
|
|
|
|
|
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-10-09 17:07:38 +00:00
|
|
|
db, err := postgres.NewPostgresDB(cfg.PostgresDSN)
|
2024-08-14 10:36:43 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
2024-10-09 17:07:38 +00:00
|
|
|
vk, err := valkey.NewValkeyClient(cfg.RedisDSN)
|
2024-08-14 10:36:43 +00:00
|
|
|
|
2024-10-09 17:07:38 +00:00
|
|
|
userRepo := usersRepository.NewUserRepository(db, logger)
|
|
|
|
userUC := usersUseCase.NewUseCase(userRepo, nil, cfg)
|
2024-08-14 10:36:43 +00:00
|
|
|
|
2024-10-09 17:07:38 +00:00
|
|
|
sessionRepo := sessionsRepository.NewValkeyRepository(vk, cfg, logger)
|
|
|
|
sessionUC := sessionsUseCase.NewUseCase(sessionRepo, cfg)
|
2024-07-14 21:26:34 +00:00
|
|
|
|
2024-10-09 17:07:38 +00:00
|
|
|
gserver := grpc.NewServer(grpc.UnaryInterceptor(usersDelivery.TokenInterceptor(sessionUC)))
|
2024-07-14 21:26:34 +00:00
|
|
|
|
2024-10-09 17:07:38 +00:00
|
|
|
usersDelivery.NewUserHandlers(gserver, userUC)
|
|
|
|
sessionsDelivery.NewSessionHandlers(gserver, sessionUC, userUC)
|
|
|
|
reflection.Register(gserver)
|
2024-07-14 21:26:34 +00:00
|
|
|
|
2024-08-14 10:36:43 +00:00
|
|
|
ln, err := net.Listen("tcp", cfg.Address)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-07-14 21:26:34 +00:00
|
|
|
|
2024-08-14 10:36:43 +00:00
|
|
|
go func() {
|
2024-10-09 17:07:38 +00:00
|
|
|
if err = gserver.Serve(ln); err != nil {
|
2024-08-14 10:36:43 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
2024-07-14 21:26:34 +00:00
|
|
|
|
|
|
|
stop := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(stop, syscall.SIGTERM, syscall.SIGINT)
|
|
|
|
|
|
|
|
<-stop
|
|
|
|
}
|