rtc-voice-chat/backend/server.py
2026-04-02 20:15:15 +08:00

70 lines
2.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import logging
import os
from pathlib import Path
from dotenv import load_dotenv
BASE_DIR = Path(__file__).parent
load_dotenv(BASE_DIR / ".env", override=False)
LOG_LEVEL = os.getenv("LOG_LEVEL", "DEBUG").upper()
logging.basicConfig(
level=getattr(logging, LOG_LEVEL, logging.DEBUG),
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
datefmt="%H:%M:%S",
)
# 压制第三方库的 DEBUG 噪音
logging.getLogger("httpx").setLevel(logging.WARNING)
logging.getLogger("httpcore").setLevel(logging.WARNING)
logging.getLogger("openai").setLevel(logging.WARNING)
logging.getLogger("uvicorn").setLevel(logging.INFO)
# 路由必须在 load_dotenv 之后导入,因为模块级代码会读取环境变量
from routes.v1.chat_callback import router as chat_callback_router # noqa: E402
from routes.v1.debug import router as debug_router # noqa: E402
from routes.v1.history import router as history_router # noqa: E402
from routes.v1.proxy import router as proxy_router # noqa: E402
from routes.v1.scenes import router as scenes_router # noqa: E402
from fastapi import FastAPI # noqa: E402
from fastapi.middleware.cors import CORSMiddleware # noqa: E402
app = FastAPI(
title="RTC AIGC 后端",
description=(
"火山引擎 RTC AI 语音对话后端服务。\n\n"
"## 鉴权说明\n"
"- **内部接口**`/getScenes` `/proxy` `/api/session/history`):由 java-mock 网关调用,"
"需附加 `X-Internal-Signature` HMAC 签名 Header。\n"
"- **LLM 回调**`/api/chat_callback`):由火山引擎 RTC 平台回调,"
"需在 `Authorization: Bearer <key>` 中携带 `CUSTOM_LLM_API_KEY`。\n"
"- **调试接口**`/debug/*`):无鉴权,仅用于本地开发。"
),
version="1.0.0",
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/health", include_in_schema=False)
async def health():
return {"status": "ok"}
app.include_router(proxy_router, prefix="/v1")
app.include_router(scenes_router, prefix="/v1")
app.include_router(chat_callback_router, prefix="/v1")
app.include_router(debug_router, prefix="/v1")
app.include_router(history_router, prefix="/v1")
if __name__ == "__main__":
import uvicorn
uvicorn.run("server:app", host="0.0.0.0", port=3001, reload=True)