2024-07-14 21:26:34 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-08-14 15:24:57 +00:00
|
|
|
"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"
|
2024-08-14 10:36:43 +00:00
|
|
|
"github.com/ilyakaznacheev/cleanenv"
|
|
|
|
_ "github.com/jackc/pgx/v5/stdlib"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"github.com/valkey-io/valkey-go"
|
2024-07-14 21:26:34 +00:00
|
|
|
"go.uber.org/zap"
|
2024-08-14 10:36:43 +00:00
|
|
|
"net"
|
2024-07-14 21:26:34 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-08-14 10:36:43 +00:00
|
|
|
var cfg lib.Config
|
|
|
|
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-08-14 10:36:43 +00:00
|
|
|
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()
|
2024-07-14 21:26:34 +00:00
|
|
|
|
2024-08-14 10:36:43 +00:00
|
|
|
sessionStorage := storage.NewSessionStorage(vk, cfg, logger)
|
2024-07-14 21:26:34 +00:00
|
|
|
|
2024-08-14 10:36:43 +00:00
|
|
|
sessionService := services.NewSessionService(sessionStorage, userStorage, cfg)
|
|
|
|
userService := services.NewUserService(userStorage, sessionStorage, cfg)
|
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
|
|
|
server := transport.NewAuthServer(sessionService, userService, logger, ln)
|
2024-07-14 21:26:34 +00:00
|
|
|
|
2024-08-14 10:36:43 +00:00
|
|
|
go func() {
|
|
|
|
if err = server.GRPCServer.Serve(ln); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
2024-07-14 21:26:34 +00:00
|
|
|
|
|
|
|
stop := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(stop, syscall.SIGTERM, syscall.SIGINT)
|
|
|
|
|
|
|
|
<-stop
|
|
|
|
}
|