rebuild(stage-1): establish isolated application skeleton
This commit is contained in:
@@ -0,0 +1 @@
|
||||
"""Application composition root."""
|
||||
@@ -0,0 +1,19 @@
|
||||
from fastapi import FastAPI
|
||||
|
||||
from backend.bootstrap.settings import Settings
|
||||
from backend.http.errors import install_error_handlers
|
||||
from backend.http.router import api_router
|
||||
|
||||
|
||||
def create_application(settings: Settings | None = None) -> FastAPI:
|
||||
runtime = settings or Settings.from_environment()
|
||||
application = FastAPI(
|
||||
title="小白复盘",
|
||||
version="0.1.0",
|
||||
docs_url="/api/docs" if runtime.debug else None,
|
||||
redoc_url=None,
|
||||
)
|
||||
application.state.settings = runtime
|
||||
install_error_handlers(application)
|
||||
application.include_router(api_router, prefix="/api")
|
||||
return application
|
||||
@@ -0,0 +1,22 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class Settings:
|
||||
environment: str
|
||||
debug: bool
|
||||
data_directory: Path
|
||||
|
||||
@classmethod
|
||||
def from_environment(cls) -> Settings:
|
||||
environment = os.getenv("APP_ENV", "development").strip().lower()
|
||||
data_directory = Path(os.getenv("APP_DATA_DIR", "data")).resolve()
|
||||
return cls(
|
||||
environment=environment,
|
||||
debug=environment == "development",
|
||||
data_directory=data_directory,
|
||||
)
|
||||
Reference in New Issue
Block a user