2026-06-19 11:28:04 +03:00
|
|
|
"""FastAPI application entrypoint."""
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
from contextlib import asynccontextmanager
|
|
|
|
|
|
|
|
|
|
from fastapi import FastAPI
|
|
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
|
|
|
|
|
from app.api import admin, auth, misc, presets, sessions, worlds
|
|
|
|
|
from app.config import settings
|
|
|
|
|
from app.logging_setup import get_logger, setup_logging
|
|
|
|
|
|
2026-06-19 16:48:07 +03:00
|
|
|
# Configure logging as early as possible — at import time, before uvicorn
|
|
|
|
|
# finishes its own logger setup. This ensures LOG_LEVEL is honored for the
|
|
|
|
|
# very first request and for startup messages from submodules.
|
|
|
|
|
setup_logging()
|
|
|
|
|
log = get_logger("app")
|
|
|
|
|
|
2026-06-19 11:28:04 +03:00
|
|
|
|
|
|
|
|
@asynccontextmanager
|
|
|
|
|
async def lifespan(app: FastAPI):
|
2026-06-19 16:48:07 +03:00
|
|
|
# Re-apply logging config in case any submodule reset it during import.
|
2026-06-19 11:28:04 +03:00
|
|
|
setup_logging()
|
2026-06-19 16:48:07 +03:00
|
|
|
log.info("app_starting", worker_mode=settings.is_worker, log_level=settings.log_level)
|
2026-06-19 11:28:04 +03:00
|
|
|
|
|
|
|
|
# Initialize DB tables and seed defaults
|
|
|
|
|
from app.migrations.init_db import init_db
|
|
|
|
|
try:
|
|
|
|
|
await init_db()
|
|
|
|
|
except Exception as e:
|
|
|
|
|
log.error("init_db_failed", error=str(e))
|
|
|
|
|
|
|
|
|
|
# Initialize RAG collections (using current DB-backed embedding settings)
|
|
|
|
|
try:
|
|
|
|
|
from app.core.rag import get_rag
|
|
|
|
|
from app.core.settings_service import get_all_settings
|
|
|
|
|
from app.db import AsyncSessionLocal
|
|
|
|
|
async with AsyncSessionLocal() as session:
|
|
|
|
|
settings_map = await get_all_settings(session)
|
|
|
|
|
await get_rag(settings_map)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
log.warning("rag_init_failed", error=str(e))
|
|
|
|
|
|
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
log.info("app_stopping")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app = FastAPI(
|
|
|
|
|
title="AI RPG Backend",
|
|
|
|
|
version="0.1.0",
|
|
|
|
|
description="Flexible AI-powered role-playing game backend.",
|
|
|
|
|
lifespan=lifespan,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
app.add_middleware(
|
|
|
|
|
CORSMiddleware,
|
2026-06-19 11:54:41 +03:00
|
|
|
allow_origins=settings.cors_origins_list,
|
2026-06-19 11:28:04 +03:00
|
|
|
allow_credentials=True,
|
|
|
|
|
allow_methods=["*"],
|
|
|
|
|
allow_headers=["*"],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/health")
|
|
|
|
|
async def health():
|
|
|
|
|
return {"status": "ok"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/")
|
|
|
|
|
async def root():
|
|
|
|
|
return {"app": "ai-rpg", "version": "0.1.0"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Routers
|
|
|
|
|
app.include_router(auth.router)
|
|
|
|
|
app.include_router(admin.router)
|
|
|
|
|
app.include_router(presets.router)
|
|
|
|
|
app.include_router(worlds.router)
|
|
|
|
|
app.include_router(sessions.router)
|
|
|
|
|
app.include_router(misc.router)
|