feat(accounts): add consent settings and organization management

This commit is contained in:
TakuyaYagam1 committed 2026-10-07 13:00:39 +03:00
1 parent 24638d46d2
commit 391af7a1ef
83 files changed
+10198 -613

No files matched your search

+27
View File
@@ -0,0 +1,27 @@
package bootstrap
import (
"context"
"time"
"lincodex/backend/config"
v1 "lincodex/backend/internal/adapter/primary/http/v1"
"lincodex/backend/internal/adapter/secondary/postgres"
"lincodex/backend/internal/usecase/consent"
)
const consentPoolMaxConns = 4
func newConsentHandler(ctx context.Context, settings config.Config) (*v1.ConsentHandler, func(), error) {
if settings.DatabaseURL == "" {
return nil, func() {}, nil
}
pool, err := postgres.NewSizedPool(ctx, settings.DatabaseURL, consentPoolMaxConns)
if err != nil {
return nil, nil, err
}
store := postgres.NewConsentStore(pool)
reader := consent.NewQueryService(store, time.Now)
commands := consent.NewService(postgres.NewTransactor(pool), time.Now)
return v1.NewConsentHandler(reader, commands), pool.Close, nil
}
+16 -6
View File
@@ -31,8 +31,18 @@ func newTrustedProxies(settings config.Config) []netip.Prefix { return settings.
// newAPIHandler composes the handler of the public API from the business
// areas that are built. An area without a handler answers 501.
func newAPIHandler(health *v1.HealthHandler, problems *problem.Responder, identityHandler *v1.IdentityHandler) *v1.Handler {
return v1.NewHandler(health, problems, v1.WithIdentity(identityHandler))
func newAPIHandler(
health *v1.HealthHandler,
problems *problem.Responder,
identityHandler *v1.IdentityHandler,
consentHandler *v1.ConsentHandler,
organizationHandler *v1.OrganizationHandler,
) *v1.Handler {
return v1.NewHandler(health, problems,
v1.WithIdentity(identityHandler),
v1.WithConsent(consentHandler),
v1.WithOrganization(organizationHandler),
)
}
// newIdentityHandler builds the registration and session operations and the
@@ -45,15 +55,12 @@ func newIdentityHandler(
logger *slog.Logger,
cache *redisadapter.Cache,
metrics *prometheus.Metrics,
keyring *identity.Keyring,
) (*v1.IdentityHandler, func(), error) {
if settings.DatabaseURL == "" {
logger.Warn("identity operations are not served because DATABASE_URL is not set")
return nil, func() {}, nil
}
keyring, err := newIdentityKeyring(settings, logger)
if err != nil {
return nil, nil, err
}
renderer, err := mailtemplate.NewRenderer(settings.AppPublicURL)
if err != nil {
return nil, nil, fmt.Errorf("bootstrap.newIdentityHandler - create mail renderer: %w", err)
@@ -146,6 +153,9 @@ func newIdentityHandler(
// counters no longer match after restart, and CSRF tokens differ between
// restarts and instances.
func newIdentityKeyring(settings config.Config, logger *slog.Logger) (*identity.Keyring, error) {
if settings.DatabaseURL == "" {
return nil, nil
}
secret := []byte(settings.IdentityKeySecret)
if len(secret) == 0 {
secret = make([]byte, identity.MinKeySecretBytes)
@@ -0,0 +1,48 @@
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
}
+3
View File
@@ -47,6 +47,9 @@ func initialize(
newAPIHandler,
newTrustedProxies,
newIdentityHandler,
newIdentityKeyring,
newConsentHandler,
newOrganizationHandler,
newSessionSecurity,
httpadapter.NewRouter,
newOutboxRelay,
+55 -17
View File
@@ -46,17 +46,21 @@ func initialize(ctx context.Context, config2 config.Config, logger *slog.Logger,
healthHandler := newHealthHandler(readiness, config2)
responder := problem.NewResponder(logger)
metrics := prometheus.New()
identityHandler, cleanup4, err := newIdentityHandler(ctx, config2, logger, cache, metrics)
keyring, err := newIdentityKeyring(config2, logger)
if err != nil {
cleanup3()
cleanup2()
cleanup()
return nil, nil, err
}
handler := newAPIHandler(healthHandler, responder, identityHandler)
sessionSecurity := newSessionSecurity(identityHandler)
v := newTrustedProxies(config2)
httpHandler, err := httpadapter.NewRouter(healthHandler, handler, sessionSecurity, responder, metrics, logger, v)
identityHandler, cleanup4, err := newIdentityHandler(ctx, config2, logger, cache, metrics, keyring)
if err != nil {
cleanup3()
cleanup2()
cleanup()
return nil, nil, err
}
consentHandler, cleanup5, err := newConsentHandler(ctx, config2)
if err != nil {
cleanup4()
cleanup3()
@@ -64,15 +68,7 @@ func initialize(ctx context.Context, config2 config.Config, logger *slog.Logger,
cleanup()
return nil, nil, err
}
relay, cleanup5, err := newOutboxRelay(ctx, config2, logger, metrics)
if err != nil {
cleanup4()
cleanup3()
cleanup2()
cleanup()
return nil, nil, err
}
bootstrapEventConsumers, cleanup6, err := newEventConsumers(ctx, config2, logger, metrics)
organizationHandler, cleanup6, err := newOrganizationHandler(ctx, config2, logger, keyring)
if err != nil {
cleanup5()
cleanup4()
@@ -81,7 +77,10 @@ func initialize(ctx context.Context, config2 config.Config, logger *slog.Logger,
cleanup()
return nil, nil, err
}
worker, cleanup7, err := newJobWorker(ctx, config2, logger, metrics)
handler := newAPIHandler(healthHandler, responder, identityHandler, consentHandler, organizationHandler)
sessionSecurity := newSessionSecurity(identityHandler)
v := newTrustedProxies(config2)
httpHandler, err := httpadapter.NewRouter(healthHandler, handler, sessionSecurity, responder, metrics, logger, v)
if err != nil {
cleanup6()
cleanup5()
@@ -91,7 +90,17 @@ func initialize(ctx context.Context, config2 config.Config, logger *slog.Logger,
cleanup()
return nil, nil, err
}
mailsenderWorker, cleanup8, err := newMailSenderWorker(ctx, config2, logger, metrics, mailSender)
relay, cleanup7, err := newOutboxRelay(ctx, config2, logger, metrics)
if err != nil {
cleanup6()
cleanup5()
cleanup4()
cleanup3()
cleanup2()
cleanup()
return nil, nil, err
}
bootstrapEventConsumers, cleanup8, err := newEventConsumers(ctx, config2, logger, metrics)
if err != nil {
cleanup7()
cleanup6()
@@ -102,7 +111,7 @@ func initialize(ctx context.Context, config2 config.Config, logger *slog.Logger,
cleanup()
return nil, nil, err
}
sampler, cleanup9, err := newDeliveryMetrics(ctx, config2, logger, metrics)
worker, cleanup9, err := newJobWorker(ctx, config2, logger, metrics)
if err != nil {
cleanup8()
cleanup7()
@@ -114,8 +123,37 @@ func initialize(ctx context.Context, config2 config.Config, logger *slog.Logger,
cleanup()
return nil, nil, err
}
mailsenderWorker, cleanup10, err := newMailSenderWorker(ctx, config2, logger, metrics, mailSender)
if err != nil {
cleanup9()
cleanup8()
cleanup7()
cleanup6()
cleanup5()
cleanup4()
cleanup3()
cleanup2()
cleanup()
return nil, nil, err
}
sampler, cleanup11, err := newDeliveryMetrics(ctx, config2, logger, metrics)
if err != nil {
cleanup10()
cleanup9()
cleanup8()
cleanup7()
cleanup6()
cleanup5()
cleanup4()
cleanup3()
cleanup2()
cleanup()
return nil, nil, err
}
application := NewApplication(config2, httpHandler, metrics, logger, mailSender, relay, bootstrapEventConsumers, worker, mailsenderWorker, sampler)
return application, func() {
cleanup11()
cleanup10()
cleanup9()
cleanup8()
cleanup7()