|
|
@@ -88,18 +88,10 @@ def _clean_headers(headers):
|
|
|
return {k: v for k, v in headers.items() if k.lower() not in HOP_BY_HOP_HEADERS}
|
|
|
|
|
|
|
|
|
-@app.api_route("/api/store/{full_path:path}", methods=["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"])
|
|
|
-async def proxy_to_store(full_path: str, request: Request):
|
|
|
- """
|
|
|
- 简易网关:监听 33333 端口,将 /api/store/* 转发到 alien_store 服务。
|
|
|
- """
|
|
|
- target_url = f"{settings.STORE_BASE_URL}/api/store/{full_path}"
|
|
|
+async def _proxy(request: Request, target_url: str, service_tag: str) -> Response:
|
|
|
+ """通用反向代理:转发请求体、头部、查询参数并回写下游响应。"""
|
|
|
client_ip = request.client.host if request.client else "-"
|
|
|
-
|
|
|
- # 读取请求体
|
|
|
body = await request.body()
|
|
|
-
|
|
|
- # 过滤头部
|
|
|
headers = _clean_headers(dict(request.headers))
|
|
|
|
|
|
try:
|
|
|
@@ -112,10 +104,12 @@ async def proxy_to_store(full_path: str, request: Request):
|
|
|
params=request.query_params,
|
|
|
)
|
|
|
except Exception as exc:
|
|
|
- logger.error("proxy to store failed ip=%s url=%s err=%s", client_ip, target_url, exc)
|
|
|
+ logger.error(
|
|
|
+ "proxy to %s failed ip=%s url=%s err=%s",
|
|
|
+ service_tag, client_ip, target_url, exc,
|
|
|
+ )
|
|
|
raise HTTPException(status_code=HTTP_502_BAD_GATEWAY, detail="Upstream unavailable")
|
|
|
|
|
|
- # 返回下游响应
|
|
|
return Response(
|
|
|
content=resp.content,
|
|
|
status_code=resp.status_code,
|
|
|
@@ -123,6 +117,20 @@ async def proxy_to_store(full_path: str, request: Request):
|
|
|
media_type=resp.headers.get("content-type"),
|
|
|
)
|
|
|
|
|
|
+
|
|
|
+@app.api_route("/api/store/{full_path:path}", methods=["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"])
|
|
|
+async def proxy_to_store(full_path: str, request: Request):
|
|
|
+ """监听 43333 端口,转发 /api/store/* 到 alien_store 服务。"""
|
|
|
+ target_url = f"{settings.STORE_BASE_URL}/api/store/{full_path}"
|
|
|
+ return await _proxy(request, target_url, "store")
|
|
|
+
|
|
|
+
|
|
|
+@app.api_route("/api/contract/{full_path:path}", methods=["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"])
|
|
|
+async def proxy_to_contract(full_path: str, request: Request):
|
|
|
+ """转发 /api/contract/* 到 alien_contract 服务。"""
|
|
|
+ target_url = f"{settings.CONTRACT_BASE_URL}/api/contract/{full_path}"
|
|
|
+ return await _proxy(request, target_url, "contract")
|
|
|
+
|
|
|
if __name__ == "__main__":
|
|
|
import uvicorn
|
|
|
- uvicorn.run(app, host="0.0.0.0", port=33333)
|
|
|
+ uvicorn.run(app, host="0.0.0.0", port=43333)
|