lab2
This commit is contained in:
+83
-12
@@ -9,13 +9,19 @@ import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"mtusi-messenger/internal/config"
|
||||
"mtusi-messenger/internal/health"
|
||||
"mtusi-messenger/internal/logger"
|
||||
"mtusi-messenger/internal/platform/cache"
|
||||
"mtusi-messenger/internal/platform/token"
|
||||
"mtusi-messenger/internal/service/chat"
|
||||
"mtusi-messenger/internal/service/message"
|
||||
"mtusi-messenger/internal/service/session"
|
||||
"mtusi-messenger/internal/service/user"
|
||||
"mtusi-messenger/internal/storage/memory"
|
||||
"mtusi-messenger/internal/storage/postgres"
|
||||
storageredis "mtusi-messenger/internal/storage/redis"
|
||||
"mtusi-messenger/internal/transport/rest"
|
||||
)
|
||||
|
||||
@@ -41,16 +47,61 @@ func main() {
|
||||
}
|
||||
|
||||
func run(cfg config.Config, log *slog.Logger) error {
|
||||
store := memory.NewStore()
|
||||
userService := user.NewService(store.Users())
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
store, err := postgres.New(ctx, cfg.Postgres)
|
||||
if err != nil {
|
||||
return fmt.Errorf("connect database: %w", err)
|
||||
}
|
||||
|
||||
defer store.Close()
|
||||
|
||||
redisCache, err := connectRedis(cfg.Redis)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer func() { _ = redisCache.Close() }()
|
||||
|
||||
sessionRepo := session.Repository(store.Sessions())
|
||||
chatRepo := chat.Repository(store.Chats())
|
||||
|
||||
pingers := []health.Pinger{store}
|
||||
|
||||
if cfg.Redis.Enabled {
|
||||
if cfg.Cache.Enabled {
|
||||
distributed, err := cache.NewDistributed(redisCache, cache.DistributedConfig{
|
||||
LockTTL: cfg.Cache.LockTTL,
|
||||
WaitTimeout: cfg.Cache.LockWaitTimeout,
|
||||
}, log)
|
||||
if err != nil {
|
||||
return fmt.Errorf("build distributed cache: %w", err)
|
||||
}
|
||||
|
||||
chatRepo = storageredis.NewChatRepository(
|
||||
store.Chats(),
|
||||
distributed,
|
||||
cfg.Cache.ChatListTTL,
|
||||
log,
|
||||
)
|
||||
}
|
||||
|
||||
log.Info("redis cache enabled", slog.String("host", cfg.Redis.Host), slog.Int("port", cfg.Redis.Port))
|
||||
}
|
||||
|
||||
ttl := time.Duration(cfg.Session.TTLSeconds) * time.Second
|
||||
sessionService := session.NewService(sessionRepo, token.New(), ttl)
|
||||
|
||||
router, err := rest.NewRouter(
|
||||
rest.NewHandler(
|
||||
userService,
|
||||
chat.NewService(store.Chats()),
|
||||
user.NewService(store.Users()),
|
||||
chat.NewService(chatRepo),
|
||||
message.NewService(store.Messages()),
|
||||
health.New(pingers...),
|
||||
sessionService,
|
||||
),
|
||||
rest.NewSecurityHandler(userService),
|
||||
rest.NewSecurityHandler(sessionService),
|
||||
log,
|
||||
)
|
||||
if err != nil {
|
||||
@@ -60,12 +111,9 @@ func run(cfg config.Config, log *slog.Logger) error {
|
||||
server := &http.Server{
|
||||
Addr: cfg.Addr(),
|
||||
Handler: router,
|
||||
ReadHeaderTimeout: cfg.ReadHeaderTimeout,
|
||||
ReadHeaderTimeout: cfg.Server.ReadHeaderTimeout,
|
||||
}
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
errc := make(chan error, 1)
|
||||
|
||||
go func() {
|
||||
@@ -88,9 +136,9 @@ func run(cfg config.Config, log *slog.Logger) error {
|
||||
case <-ctx.Done():
|
||||
}
|
||||
|
||||
log.Info("shutdown signal received", slog.Duration("timeout", cfg.ShutdownTimeout))
|
||||
log.Info("shutdown signal received", slog.Duration("timeout", cfg.Server.ShutdownTimeout))
|
||||
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), cfg.ShutdownTimeout)
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), cfg.Server.ShutdownTimeout)
|
||||
defer cancel()
|
||||
|
||||
if err := server.Shutdown(shutdownCtx); err != nil {
|
||||
@@ -101,3 +149,26 @@ func run(cfg config.Config, log *slog.Logger) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func connectRedis(cfg config.Redis) (cache.LockStore, error) {
|
||||
if !cfg.Enabled {
|
||||
return cache.NoOpCache{}, nil
|
||||
}
|
||||
|
||||
client, err := cache.NewRedis(cache.RedisConfig{
|
||||
Host: cfg.Host,
|
||||
Port: cfg.Port,
|
||||
Password: cfg.Password,
|
||||
DB: cfg.DB,
|
||||
MaxRetries: cfg.MaxRetries,
|
||||
DialTimeout: cfg.DialTimeout,
|
||||
ReadTimeout: cfg.ReadTimeout,
|
||||
WriteTimeout: cfg.WriteTimeout,
|
||||
PoolSize: cfg.PoolSize,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("connect redis: %w", err)
|
||||
}
|
||||
|
||||
return client, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user