Files
portfolio-hub/app/main.py
T
genichin 6e02d92282 Add ProofLine criteria documentation and Strategy Catalog v1 implementation
- Add acceptance criteria documentation (AC-0013 through AC-0023)
- Add Line-0003 discovery documentation
- Implement Strategy Catalog v1 with new database migration
- Update portfolio API endpoints and schemas
- Refactor strategy and portfolio services
- Add strategy management CLI commands
- Update domain models with strategy catalog support
- Add comprehensive tests for v1 strategies
- Update design documentation (data model, interfaces, runtime flows)
2026-08-15 11:18:31 +09:00

100 lines
3.4 KiB
Python

"""FastAPI application entrypoint."""
from contextlib import asynccontextmanager
from typing import Callable
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.api.health import router as health_router
from app.api.auth_routes import router as auth_router
from app.api.portfolios import router as portfolio_router
from app.api.users import router as users_router
from app.api.v1_admin_daemons import router as v1_admin_daemons_router
from app.api.v1_assets import router as v1_assets_router
from app.api.v1_daemon_credential import router as v1_daemon_credential_router
from app.api.v1_daemons import router as v1_daemons_router
from app.api.v1_portfolios import router as v1_portfolios_router
from app.api.v1_strategies import router as v1_strategies_router
from app.api.v1_tags import router as v1_tags_router
from app.api.v1_user_info import router as v1_user_info_router
from app.core.config import Settings, get_settings
from app.core.errors import register_exception_handlers
from app.core.security import hash_password
from app.repositories.factory import get_portfolio_repository_from_settings
from app.repositories.portfolio_repository import PortfolioRepository
def _bootstrap_initial_user(
settings: Settings,
repository_factory: Callable[[Settings], PortfolioRepository],
) -> None:
if not settings.db_enabled or settings.bootstrap_user_username is None or settings.bootstrap_user_password is None:
return
repository = repository_factory(settings)
try:
if repository.list_users():
return
repository.create_user(
settings.bootstrap_user_username,
email=settings.bootstrap_user_email,
password_hash=hash_password(settings.bootstrap_user_password),
permission="admin",
)
finally:
if callable(getattr(type(repository), "close", None)):
repository.close()
def create_app(
*,
settings_factory: Callable[[], Settings] = get_settings,
repository_factory: Callable[[Settings], PortfolioRepository] = get_portfolio_repository_from_settings,
) -> FastAPI:
settings = settings_factory()
@asynccontextmanager
async def lifespan(_: FastAPI):
_bootstrap_initial_user(settings, repository_factory)
if settings.db_enabled:
settings.require_credential_encryption_key()
yield
app = FastAPI(
title=settings.app_name,
version=settings.app_version,
docs_url="/docs",
redoc_url="/redoc",
root_path=settings.root_path,
lifespan=lifespan,
)
app.add_middleware(
CORSMiddleware,
allow_origins=settings.allowed_origins,
allow_credentials=True,
allow_methods=["GET", "POST", "PATCH", "DELETE", "OPTIONS"],
allow_headers=["Authorization", "Content-Type"],
)
app.include_router(health_router)
app.include_router(auth_router)
app.include_router(portfolio_router)
app.include_router(users_router)
app.include_router(v1_admin_daemons_router)
app.include_router(v1_assets_router)
app.include_router(v1_daemon_credential_router)
app.include_router(v1_daemons_router)
app.include_router(v1_portfolios_router)
app.include_router(v1_strategies_router)
app.include_router(v1_tags_router)
app.include_router(v1_user_info_router)
register_exception_handlers(app)
return app
app = create_app()