49 lines
1.5 KiB
Go
49 lines
1.5 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/organization"
|
|
)
|
|
|
|
const organizationPoolMaxConns = 8
|
|
|
|
// newOrganizationHandler builds company reads and writes from the PostgreSQL
|
|
// transaction boundary. Without DATABASE_URL the composite handler keeps the
|
|
// operations on its 501 fallback.
|
|
func newOrganizationHandler(
|
|
ctx context.Context,
|
|
settings config.Config,
|
|
logger *slog.Logger,
|
|
keyring *identity.Keyring,
|
|
) (*v1.OrganizationHandler, func(), error) {
|
|
if settings.DatabaseURL == "" {
|
|
logger.Warn("organization operations are not served because DATABASE_URL is not set")
|
|
return nil, func() {}, nil
|
|
}
|
|
if keyring == nil {
|
|
return nil, nil, fmt.Errorf("bootstrap.newOrganizationHandler - identity keyring must be set")
|
|
}
|
|
pool, err := postgres.NewSizedPool(ctx, settings.DatabaseURL, organizationPoolMaxConns)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("bootstrap.newOrganizationHandler - create pool: %w", err)
|
|
}
|
|
service, err := organization.NewService(organization.Dependencies{
|
|
Transactor: postgres.NewTransactor(pool),
|
|
Keyring: keyring,
|
|
Clock: time.Now,
|
|
})
|
|
if err != nil {
|
|
pool.Close()
|
|
return nil, nil, fmt.Errorf("bootstrap.newOrganizationHandler - create service: %w", err)
|
|
}
|
|
return v1.NewOrganizationHandler(service, settings.AppPublicURL), pool.Close, nil
|
|
}
|