ms-auth/main.go
2024-08-14 20:24:57 +05:00

78 lines
1.7 KiB
Go

package main
import (
"fmt"
"git.sch9.ru/new_gate/ms-auth/internal/lib"
"git.sch9.ru/new_gate/ms-auth/internal/services"
"git.sch9.ru/new_gate/ms-auth/internal/storage"
"git.sch9.ru/new_gate/ms-auth/internal/transport"
"github.com/ilyakaznacheev/cleanenv"
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/jmoiron/sqlx"
"github.com/valkey-io/valkey-go"
"go.uber.org/zap"
"net"
"os"
"os/signal"
"syscall"
)
func main() {
var cfg lib.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()
userStorage := storage.NewUserStorage(db, logger)
opts, err := valkey.ParseURL(cfg.RedisDSN)
if err != nil {
panic(err)
}
vk, err := valkey.NewClient(opts)
if err != nil {
panic(err)
}
defer vk.Close()
sessionStorage := storage.NewSessionStorage(vk, cfg, logger)
sessionService := services.NewSessionService(sessionStorage, userStorage, cfg)
userService := services.NewUserService(userStorage, sessionStorage, cfg)
ln, err := net.Listen("tcp", cfg.Address)
if err != nil {
panic(err)
}
server := transport.NewAuthServer(sessionService, userService, logger, ln)
go func() {
if err = server.GRPCServer.Serve(ln); err != nil {
panic(err)
}
}()
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGTERM, syscall.SIGINT)
<-stop
}