feat: 添加 Strapi 配置文件支持
- 新增 config.yaml:集中管理 Strapi API 配置 - 新增 config_loader.py:配置加载模块 - 更新 http_routes.py:从配置文件读取 API 端点 - 支持从 YAML 文件配置 FAQ 分类和语言 - 更新 requirements.txt:添加 pyyaml 依赖 优势: - 配置与代码分离,易于维护 - 添加新分类无需修改代码 - 支持热加载配置 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -10,6 +10,8 @@ from starlette.responses import JSONResponse
|
||||
from pydantic_settings import BaseSettings
|
||||
from pydantic import ConfigDict
|
||||
|
||||
from config_loader import load_config, get_category_endpoint
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
"""Server configuration"""
|
||||
@@ -22,18 +24,15 @@ class Settings(BaseSettings):
|
||||
|
||||
settings = Settings()
|
||||
|
||||
|
||||
# ============ FAQ Categories ============
|
||||
|
||||
FAQ_CATEGORIES = {
|
||||
"register": "faq-register",
|
||||
"order": "faq-order",
|
||||
"pre-order": "faq-pre-order",
|
||||
"payment": "faq-payment",
|
||||
"shipment": "faq-shipment",
|
||||
"return": "faq-return",
|
||||
"other": "faq-other-question",
|
||||
}
|
||||
# 加载配置文件
|
||||
try:
|
||||
strapi_config = load_config()
|
||||
# 使用配置文件中的 URL(如果存在)
|
||||
if strapi_config.base_url:
|
||||
settings.strapi_api_url = strapi_config.base_url
|
||||
except Exception:
|
||||
# 如果配置文件加载失败,使用环境变量
|
||||
strapi_config = None
|
||||
|
||||
|
||||
# ============ Company Info ============
|
||||
@@ -125,8 +124,12 @@ async def query_faq_http(
|
||||
limit: Maximum results to return
|
||||
"""
|
||||
try:
|
||||
# Map category to endpoint
|
||||
endpoint = FAQ_CATEGORIES.get(category, f"faq-{category}")
|
||||
# 从配置文件获取端点
|
||||
if strapi_config:
|
||||
endpoint = get_category_endpoint(category, strapi_config)
|
||||
else:
|
||||
# 回退到硬编码的默认值
|
||||
endpoint = f"faq-{category}"
|
||||
|
||||
headers = {"Content-Type": "application/json"}
|
||||
if settings.strapi_api_token and settings.strapi_api_token.strip():
|
||||
@@ -238,10 +241,27 @@ async def search_faq_http(
|
||||
try:
|
||||
all_results = []
|
||||
|
||||
# 获取所有分类
|
||||
if strapi_config:
|
||||
categories = strapi_config.faq_categories
|
||||
else:
|
||||
# 回退到默认分类
|
||||
categories = {
|
||||
"register": type("obj", (object,), {"endpoint": "faq-register"}),
|
||||
"order": type("obj", (object,), {"endpoint": "faq-order"}),
|
||||
"pre-order": type("obj", (object,), {"endpoint": "faq-pre-order"}),
|
||||
"payment": type("obj", (object,), {"endpoint": "faq-payment"}),
|
||||
"shipment": type("obj", (object,), {"endpoint": "faq-shipment"}),
|
||||
"return": type("obj", (object,), {"endpoint": "faq-return"}),
|
||||
"other": type("obj", (object,), {"endpoint": "faq-other-question"}),
|
||||
}
|
||||
|
||||
# Search all categories in parallel
|
||||
async with httpx.AsyncClient(timeout=30.0) as client:
|
||||
tasks = []
|
||||
for category_name, endpoint in FAQ_CATEGORIES.items():
|
||||
for category_name, category_config in categories.items():
|
||||
endpoint = category_config.endpoint if hasattr(category_config, "endpoint") else f"faq-{category_name}"
|
||||
|
||||
headers = {"Content-Type": "application/json"}
|
||||
if settings.strapi_api_token and settings.strapi_api_token.strip():
|
||||
headers["Authorization"] = f"Bearer {settings.strapi_api_token}"
|
||||
|
||||
Reference in New Issue
Block a user