Files
JARVIS/backend/app/main.py
DESKTOP-72TV0V4\caoxiaozhu 204cb223a3 Fix Log model registration - import models before init_db
The Log model was not being registered with SQLAlchemy's metadata,
causing the logs table to not be created on startup.
2026-03-21 12:02:35 +08:00

78 lines
1.9 KiB
Python

from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.database import init_db
import app.models # noqa: F401 - 注册所有模型
from app.routers import (
auth_router,
conversation_router,
document_router,
task_router,
forum_router,
graph_router,
agent_router,
todo_router,
settings_router,
folder_router,
skill_router,
log_router,
)
from app.routers.scheduler import router as scheduler_router
from app.services.scheduler_service import start_scheduler, stop_scheduler, get_scheduler_status
from app.config import settings
import os
@asynccontextmanager
async def lifespan(app: FastAPI):
# 启动
os.makedirs(settings.DATA_DIR, exist_ok=True)
os.makedirs(settings.UPLOAD_DIR, exist_ok=True)
os.makedirs(settings.CHROMA_PERSIST_DIR, exist_ok=True)
await init_db()
start_scheduler()
yield
# 关闭
stop_scheduler()
app = FastAPI(
title=settings.APP_NAME,
version=settings.APP_VERSION,
lifespan=lifespan,
)
# CORS
app.add_middleware(
CORSMiddleware,
allow_origins=settings.CORS_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# 注册路由
app.include_router(auth_router)
app.include_router(conversation_router)
app.include_router(document_router)
app.include_router(task_router)
app.include_router(forum_router)
app.include_router(graph_router)
app.include_router(agent_router)
app.include_router(todo_router)
app.include_router(settings_router)
app.include_router(folder_router)
app.include_router(skill_router)
app.include_router(log_router)
app.include_router(scheduler_router)
@app.get("/api/health")
async def health():
return {
"status": "ok",
"version": settings.APP_VERSION,
"llm_provider": settings.LLM_PROVIDER,
"scheduler": get_scheduler_status(),
}