Files
mkt.ose.tw/backend/app/main.py
2026-03-23 20:23:58 +08:00

64 lines
2.0 KiB
Python

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.api.router import api_router
from app.core.config import settings
from app.schemas.health import (
HealthStatusResponse,
ReadinessDependency,
ReadinessStatusResponse,
)
def create_app() -> FastAPI:
app = FastAPI(
title=settings.app_name,
version=settings.app_version,
docs_url="/docs",
redoc_url="/redoc",
)
app.add_middleware(
CORSMiddleware,
allow_origins=settings.cors_allowed_origin_list,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(api_router, prefix=settings.api_prefix)
@app.get("/health", response_model=HealthStatusResponse)
async def healthcheck() -> HealthStatusResponse:
return HealthStatusResponse(status="ok")
@app.get("/health/ready", response_model=ReadinessStatusResponse)
async def readiness_check() -> ReadinessStatusResponse:
dependencies = [
ReadinessDependency(
name="database_url",
configured=bool(settings.database_url),
detail="DATABASE_URL is set.",
),
ReadinessDependency(
name="directus_base_url",
configured=bool(settings.directus_base_url),
detail="DIRECTUS_BASE_URL is set.",
),
ReadinessDependency(
name="directus_admin_token",
configured=bool(settings.directus_admin_token),
detail="DIRECTUS_ADMIN_TOKEN is optional for read-only auth checks.",
),
]
status_value = "ready" if all(item.configured or item.name == "directus_admin_token" for item in dependencies) else "degraded"
return ReadinessStatusResponse(
status=status_value,
app_env=settings.app_env,
app_name=settings.app_name,
dependencies=dependencies,
)
return app
app = create_app()