package main import ( "context" "fmt" "log/slog" "os" "os/signal" "runtime" "syscall" "time" httpapi "gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/api/http" "gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/cache" "gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/config" "gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/logging" "gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/password" "gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/service" "gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/session" "gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/store" "gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/store/db" ) func main() { cfg, err := config.Load() if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } logger, err := logging.New(cfg.Logger.Level, cfg.Env, cfg.Version) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } slog.SetDefault(logger) pool, err := db.Connect(context.Background(), cfg.Database) if err != nil { logger.Error("database connect failed", slog.Any("err", err)) os.Exit(1) } storage := db.New(pool, time.Duration(cfg.Database.QueryTimeoutMs)*time.Millisecond) defer storage.Close() sessClient, err := session.Connect(context.Background(), cfg.Cache) if err != nil { logger.Error("session store connect failed", slog.Any("err", err)) storage.Close() os.Exit(1) } defer sessClient.Close() sessions := session.NewValkey(sessClient) hasher := password.NewHasher(password.Default, min(runtime.NumCPU(), 4)) auth, err := service.NewAuth(storage, sessions, hasher, time.Duration(cfg.Session.TTLSeconds)*time.Second) if err != nil { logger.Error("auth init failed", slog.Any("err", err)) storage.Close() os.Exit(1) } var messages store.MessageStore = storage if cfg.HistoryCache.Mode != cache.ModeOff { hcClient, err := cache.Connect(cfg.HistoryCache) if err != nil { logger.Error("history cache config failed", slog.Any("err", err)) storage.Close() os.Exit(1) } defer hcClient.Close() hc := cache.New(storage.Messages, hcClient, cache.Options{ Mode: cfg.HistoryCache.Mode, PageTTL: time.Duration(cfg.HistoryCache.PageTTLSeconds) * time.Second, FillDelay: time.Duration(cfg.HistoryCache.FillDelayMs) * time.Millisecond, Logger: logger, }) defer hc.Close() messages = hc } logger.Info("history cache", slog.String("mode", cfg.HistoryCache.Mode)) srv := httpapi.NewServer(httpapi.Deps{ Config: cfg, Logger: logger, Store: storage, Auth: auth, Users: service.NewUsers(storage, hasher), Chats: service.NewChats(storage), Messages: service.NewMessages(messages), Ready: func(ctx context.Context) error { if err := storage.Ping(ctx); err != nil { return err } return sessions.Ping(ctx) }, }) ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) defer stop() startErr := make(chan error, 1) go func() { startErr <- srv.Start() }() select { case err := <-startErr: if err != nil { logger.Error("server failed to start", slog.Any("err", err)) storage.Close() os.Exit(1) } case <-ctx.Done(): logger.Info("shutdown signal received") } shutdownCtx, cancel := context.WithTimeout( context.Background(), time.Duration(cfg.HTTP.ShutdownTimeoutMs)*time.Millisecond, ) defer cancel() if err := srv.Shutdown(shutdownCtx); err != nil { logger.Error("shutdown failed", slog.Any("err", err)) storage.Close() os.Exit(1) } logger.Info("server stopped cleanly") }