Files
diary-news/backend/app/main.py
xiaji 07534eb144 feat(ingest): API Push 短新闻接口层
- POST /api/v1/ingest:鉴权(X-Ingest-Token) + 限速(每 token 2 篇/秒,
  Redis 滑动桶,INGEST_RATE_PER_SEC 可调) + 三层去重(L1 external_id /
  L2 content_hash / L3 DB UNIQUE 兜底,均带 reason)
- 写入字段:is_short_news=True、translation/format/image_ai_status='n/a'、
  classify_status=(有 tags?'ok':'pending')、commentary_{angel,meituan}_status='pending'、
  body_zh_text=body_text(走统一路径,前端/prompt 不用改)
- services/fetchers/api_push.py:compute_content_hash + synthesize_url +
  normalize_published_at + build_initial_status 纯函数
- schemas/ingest.py:IngestPayload(title 1-200/body 1-5000/tags 去重去空) +
  IngestResponse(article_id/content_hash/status/reason/matched_external_id)
- admin.py:POST/GET/DELETE /admin/sources/{id}/ingest-tokens — owner 生成
  (raw_token 仅一次性返回)、列出、撤销
- schemas/article.py:ArticleListItem 加 is_short_news/source_ref;
  ArticleDetail 加 is_short_news/source_ref/external_id
- main.py:挂 ingest router;config.py + .env.example:ingest_rate_per_sec 默认 2

短新闻由 commit 1 enrichment_loop 自动接管 classify + 双 provider commentary,
跳过 format/image。
2026-06-14 16:04:45 +08:00

120 lines
3.4 KiB
Python

"""FastAPI 入口。
- 注册路由
- 启动 / 关闭事件:连接池、调度器
- CORS
- 全局异常处理
"""
from __future__ import annotations
import logging
from contextlib import asynccontextmanager
from fastapi import FastAPI, Request
from fastapi.exceptions import RequestValidationError
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from starlette.exceptions import HTTPException as StarletteHTTPException
from app.api import admin, admin_llm, articles, auth, bookmarks, ingest, me, sources, subscriptions
from app.config import settings
from app.database import engine
from app.redis_client import close_redis, get_redis
logger = logging.getLogger("news.api")
logging.basicConfig(
level=settings.log_level,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
)
@asynccontextmanager
async def lifespan(app: FastAPI):
# 启动
logger.info("api starting, tz=%s", settings.tz)
# 触发 redis 连接
await get_redis().ping()
yield
# 关闭
logger.info("api shutting down")
await close_redis()
await engine.dispose()
app = FastAPI(
title="Diary News",
description="Private news aggregator",
version="0.1.0",
default_response_class=JSONResponse,
lifespan=lifespan,
docs_url="/api/docs" if settings.log_level == "DEBUG" else None,
redoc_url=None,
)
# CORS:网页 + Android,简单放开(私有)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # MVP 放开,生产收紧
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# === 全局异常处理(RFC 7807) ===
@app.exception_handler(StarletteHTTPException)
async def http_exc_handler(request: Request, exc: StarletteHTTPException):
return JSONResponse(
status_code=exc.status_code,
content={
"type": "about:blank",
"title": exc.detail if isinstance(exc.detail, str) else "Error",
"status": exc.status_code,
"instance": str(request.url),
},
headers=exc.headers or None,
)
@app.exception_handler(RequestValidationError)
async def validation_exc_handler(request: Request, exc: RequestValidationError):
return JSONResponse(
status_code=422,
content={
"type": "about:blank",
"title": "Validation Error",
"status": 422,
"errors": exc.errors(),
"instance": str(request.url),
},
)
# === 路由 ===
API_PREFIX = "/api/v1"
app.include_router(auth.router, prefix=API_PREFIX)
app.include_router(me.router, prefix=API_PREFIX)
app.include_router(articles.router, prefix=API_PREFIX)
app.include_router(sources.router, prefix=API_PREFIX)
app.include_router(bookmarks.router, prefix=API_PREFIX)
app.include_router(subscriptions.router, prefix=API_PREFIX)
app.include_router(ingest.router, prefix=API_PREFIX)
app.include_router(admin.router, prefix=API_PREFIX)
app.include_router(admin_llm.router, prefix=API_PREFIX)
# === 健康检查 ===
@app.get(f"{API_PREFIX}/healthz", include_in_schema=False)
async def healthz():
try:
await get_redis().ping()
except Exception as e:
return JSONResponse({"status": "degraded", "redis": str(e)}, status_code=503)
return {"status": "ok"}
@app.get(f"{API_PREFIX}/", include_in_schema=False)
async def root():
return {"name": "diary-news", "version": app.version, "docs": f"{API_PREFIX}/docs"}