Files
ai-rpg/backend/app/main.py

77 lines
1.9 KiB
Python
Raw Normal View History

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
@asynccontextmanager
async def lifespan(app: FastAPI):
setup_logging()
log = get_logger("app")
log.info("app_starting", worker_mode=settings.is_worker)
# 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,
allow_origins=settings.cors_origins,
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)