50 lines
1.7 KiB
Go
50 lines
1.7 KiB
Go
package bootstrap
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"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/practice"
|
|
)
|
|
|
|
// practicePoolMaxConns covers concurrent debug run requests; each holds one
|
|
// short transaction.
|
|
const practicePoolMaxConns = 4
|
|
|
|
// newPracticeHandler builds the debug run operations of code practice and the
|
|
// PostgreSQL pool they own. Nothing connects here. It returns no handler when
|
|
// DATABASE_URL is unset, and the operations then answer 501. Without a code
|
|
// runner (CODE_RUNNER_URL unset) the operations answer 503 and store nothing.
|
|
// The application clock is time.Now.
|
|
func newPracticeHandler(
|
|
ctx context.Context,
|
|
settings config.Config,
|
|
logger *slog.Logger,
|
|
keyring *identity.Keyring,
|
|
) (*v1.PracticeHandler, func(), error) {
|
|
if settings.DatabaseURL == "" {
|
|
logger.Warn("code practice operations are not served because DATABASE_URL is not set")
|
|
return nil, func() {}, nil
|
|
}
|
|
if keyring == nil {
|
|
return nil, nil, errors.New("bootstrap.newPracticeHandler - identity keyring must be set")
|
|
}
|
|
pool, err := postgres.NewSizedPool(ctx, settings.DatabaseURL, practicePoolMaxConns)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("bootstrap.newPracticeHandler - create pool: %w", err)
|
|
}
|
|
service, err := practice.NewService(postgres.NewTransactor(pool), keyring, time.Now, settings.CodeRunner.Enabled())
|
|
if err != nil {
|
|
pool.Close()
|
|
return nil, nil, fmt.Errorf("bootstrap.newPracticeHandler - create service: %w", err)
|
|
}
|
|
return v1.NewPracticeHandler(service), pool.Close, nil
|
|
}
|