Files
lincodex/backend/internal/bootstrap/notification.go
T

45 lines
1.4 KiB
Go

package bootstrap
import (
"context"
"fmt"
"log/slog"
"time"
"lincodex/backend/config"
v1 "lincodex/backend/internal/adapter/primary/http/v1"
"lincodex/backend/internal/adapter/secondary/postgres"
"lincodex/backend/internal/usecase/notification"
)
const notificationPoolMaxConns = 4
// newNotificationHandler builds the inbox operations and the PostgreSQL pool
// they own. Nothing connects here, so an unavailable database never blocks
// startup. It returns no handler when DATABASE_URL is unset, and the
// operations then answer 501.
func newNotificationHandler(
ctx context.Context,
settings config.Config,
logger *slog.Logger,
) (*v1.NotificationHandler, func(), error) {
if settings.DatabaseURL == "" {
logger.Warn("notification inbox operations are not served because DATABASE_URL is not set")
return nil, func() {}, nil
}
pool, err := postgres.NewSizedPool(ctx, settings.DatabaseURL, notificationPoolMaxConns)
if err != nil {
return nil, nil, fmt.Errorf("bootstrap.newNotificationHandler - create pool: %w", err)
}
service, err := notification.NewInboxService(
postgres.NewTransactor(pool),
notification.NewInboxContentResolver(notification.DefaultInboxSourceResolvers(settings.NotificationSyntheticEnabled)),
time.Now,
)
if err != nil {
pool.Close()
return nil, nil, fmt.Errorf("bootstrap.newNotificationHandler - create service: %w", err)
}
return v1.NewNotificationHandler(service), pool.Close, nil
}