inital
This commit is contained in:
57
backend/app/main.py
Normal file
57
backend/app/main.py
Normal file
@@ -0,0 +1,57 @@
|
||||
from contextlib import asynccontextmanager
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from .database import create_tables, AsyncSessionLocal
|
||||
from .scheduler import start_scheduler, shutdown_scheduler
|
||||
from .api import news, admin
|
||||
from .config import settings
|
||||
from .models.news import LLMConfig
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
await create_tables()
|
||||
await seed_initial_llm_config()
|
||||
start_scheduler()
|
||||
yield
|
||||
shutdown_scheduler()
|
||||
|
||||
|
||||
async def seed_initial_llm_config():
|
||||
"""Insert default LLM config on first run if none exists."""
|
||||
from sqlalchemy import select
|
||||
async with AsyncSessionLocal() as db:
|
||||
result = await db.execute(select(LLMConfig).limit(1))
|
||||
if result.scalar_one_or_none():
|
||||
return
|
||||
if not settings.initial_llm_api_key:
|
||||
return
|
||||
config = LLMConfig(
|
||||
name="默认配置",
|
||||
provider=settings.initial_llm_provider,
|
||||
api_key=settings.initial_llm_api_key,
|
||||
base_url=settings.initial_llm_base_url,
|
||||
model_name=settings.initial_llm_model,
|
||||
is_active=True,
|
||||
)
|
||||
db.add(config)
|
||||
await db.commit()
|
||||
|
||||
|
||||
app = FastAPI(title="医药情报 API", version="1.0.0", lifespan=lifespan)
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
app.include_router(news.router, prefix="/api/news", tags=["news"])
|
||||
app.include_router(admin.router, prefix="/api/admin", tags=["admin"])
|
||||
|
||||
|
||||
@app.get("/api/health")
|
||||
async def health():
|
||||
return {"status": "ok"}
|
||||
Reference in New Issue
Block a user