Kaynağa Gözat

分数配置接口代理转发

AnlaAnla 1 ay önce
ebeveyn
işleme
2c15a58a37
3 değiştirilmiş dosya ile 86 ekleme ve 0 silme
  1. 82 0
      app/api/config_proxy.py
  2. 2 0
      app/core/config.py
  3. 2 0
      app/main.py

+ 82 - 0
app/api/config_proxy.py

@@ -0,0 +1,82 @@
+from fastapi import APIRouter, HTTPException, Body, status
+from fastapi.responses import JSONResponse
+import httpx
+from ..core.logger import get_logger
+from ..core.config import settings
+
+logger = get_logger(__name__)
+
+router = APIRouter()
+
+
+@router.get("/scoring_config", summary="[代理] 获取评分配置")
+async def proxy_get_scoring_config():
+    """
+    转发请求到分数的获取配置接口
+    """
+    target_url = settings.SCORE_SERVER_CONFIG_URL
+    logger.info(f"正在代理 GET 请求到: {target_url}")
+
+    async with httpx.AsyncClient() as client:
+        try:
+            # 发送请求给项目1
+            resp = await client.get(target_url, timeout=10.0)
+
+            # 如果项目1返回错误状态码,抛出对应的 HTTPException
+            if resp.status_code != 200:
+                logger.error(f"项目1返回错误: {resp.status_code} - {resp.text}")
+                raise HTTPException(
+                    status_code=resp.status_code,
+                    detail=resp.json().get("detail", "上游服务错误")
+                )
+
+            # 返回项目1的数据
+            return resp.json()
+
+        except httpx.RequestError as exc:
+            logger.error(f"连接项目1失败: {exc}")
+            raise HTTPException(
+                status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
+                detail=f"无法连接到评分服务 (Project 1): {exc}"
+            )
+
+
+@router.put("/scoring_config", summary="[代理] 更新评分配置")
+async def proxy_update_scoring_config(config_data: dict = Body(...)):
+    """
+    转发请求到分数的更新配置接口
+    """
+    target_url = settings.SCORE_SERVER_CONFIG_URL
+    logger.info(f"正在代理 PUT 请求到: {target_url}")
+
+    async with httpx.AsyncClient() as client:
+        try:
+            # 将前端传来的 json 原封不动转发给项目1
+            resp = await client.put(target_url, json=config_data, timeout=10.0)
+
+            # 处理项目1的响应
+            if resp.status_code != 200:
+                # 可能是 400 校验错误,或者是 500 内部错误
+                error_detail = "上游服务错误"
+                try:
+                    error_detail = resp.json().get("detail", error_detail)
+                except:
+                    pass
+
+                logger.warning(f"项目1拒绝更新: {resp.status_code} - {error_detail}")
+                raise HTTPException(
+                    status_code=resp.status_code,
+                    detail=error_detail
+                )
+
+            return JSONResponse(
+                status_code=resp.status_code,
+                content=resp.json()
+            )
+
+        except httpx.RequestError as exc:
+            logger.error(f"连接项目1失败: {exc}")
+            raise HTTPException(
+                status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
+                detail=f"无法连接到评分服务 (Project 1): {exc}"
+            )

+ 2 - 0
app/core/config.py

@@ -17,6 +17,8 @@ class Settings:
     # 分数计算接口url
     SCORE_UPDATE_SERVER_URL = "http://127.0.0.1:7744"
     SCORE_RECALCULATE_ENDPOINT = f"{SCORE_UPDATE_SERVER_URL}/api/card_score/score_recalculate"
+    # 分数计算配置接口
+    SCORE_SERVER_CONFIG_URL = f"{SCORE_UPDATE_SERVER_URL}/api/config/scoring_config"
 
     # --- 数据库配置 ---
     DB_NAME = 'card_score_database'

+ 2 - 0
app/main.py

@@ -10,6 +10,7 @@ from app.api import cards as cards_router
 from app.api import images as images_router
 from app.api import labelme as labelme_router
 from app.api import formate_xy as formate_xy_router
+from app.api import config_proxy as config_proxy_router
 from .core.config import settings
 from .core.logger import setup_logging, get_logger
 
@@ -44,3 +45,4 @@ app.include_router(cards_router.router, prefix=f"{settings.API_PREFIX}/cards", t
 app.include_router(images_router.router, prefix=f"{settings.API_PREFIX}/images", tags=["Images"])
 app.include_router(labelme_router.router, prefix=f"{settings.API_PREFIX}/labelme", tags=["Labelme"])
 app.include_router(formate_xy_router.router, prefix=f"{settings.API_PREFIX}/formate_xy", tags=["Formate"])
+app.include_router(config_proxy_router.router, prefix=f"{settings.API_PREFIX}/config", tags=["ConfigProxy"])