46 lines
1.3 KiB
Go
46 lines
1.3 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/identity"
|
|
"lincodex/backend/internal/usecase/supportcase"
|
|
)
|
|
|
|
const supportPoolMaxConns = 8
|
|
|
|
func newSupportHandler(
|
|
ctx context.Context,
|
|
settings config.Config,
|
|
logger *slog.Logger,
|
|
keyring *identity.Keyring,
|
|
) (*v1.SupportHandler, func(), error) {
|
|
if settings.DatabaseURL == "" {
|
|
logger.Warn("support operations are not served because DATABASE_URL is not set")
|
|
return nil, func() {}, nil
|
|
}
|
|
if keyring == nil {
|
|
return nil, nil, fmt.Errorf("bootstrap.newSupportHandler - identity keyring must be set")
|
|
}
|
|
pool, err := postgres.NewSizedPool(ctx, settings.DatabaseURL, supportPoolMaxConns)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("bootstrap.newSupportHandler - create pool: %w", err)
|
|
}
|
|
service, err := supportcase.NewService(supportcase.Dependencies{
|
|
Transactor: postgres.NewTransactor(pool),
|
|
Keyring: keyring,
|
|
Clock: time.Now,
|
|
})
|
|
if err != nil {
|
|
pool.Close()
|
|
return nil, nil, fmt.Errorf("bootstrap.newSupportHandler - create service: %w", err)
|
|
}
|
|
return v1.NewSupportHandler(service), pool.Close, nil
|
|
}
|