59 lines
1.5 KiB
Python
59 lines
1.5 KiB
Python
import logging
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from app.config import settings
|
|
from app.database import Base, SessionLocal, engine, ensure_database_schema
|
|
from app.routers import html
|
|
from app.routers.html import cleanup_expired_files
|
|
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
|
|
)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
settings.html_storage_dir.mkdir(parents=True, exist_ok=True)
|
|
Base.metadata.create_all(bind=engine)
|
|
ensure_database_schema()
|
|
|
|
app = FastAPI(
|
|
title=settings.app_name,
|
|
version="2.0.0",
|
|
description=(
|
|
"Store agent-generated educational HTML pages and return a direct access URL. "
|
|
"The generated OpenAPI document can be imported directly into Tencent Cloud "
|
|
"Agent plugins."
|
|
),
|
|
)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=settings.allowed_origins,
|
|
allow_credentials=False,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
app.include_router(html.router, prefix=settings.api_prefix)
|
|
|
|
|
|
@app.on_event("startup")
|
|
def cleanup_on_startup() -> None:
|
|
db = SessionLocal()
|
|
try:
|
|
deleted_count = cleanup_expired_files(db)
|
|
if deleted_count > 0:
|
|
logger.info("Deleted %s expired HTML files during startup", deleted_count)
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
@app.get("/", summary="Health check")
|
|
def health_check() -> dict[str, str]:
|
|
return {
|
|
"message": "HTML Knowledge API is running",
|
|
"openapi_url": "/openapi.json",
|
|
}
|