fastapi-地址反向代理

代理代码

  • 使用httpx库
  • 异步代理
async def reverse_proxy_request(url, method, post_data):
    async with httpx.AsyncClient() as client:
        # 发起代理请求
        try:
            if method == "GET":
                response = await client.get(url)
            elif method == "POST":
                response = await client.post(url, json=post_data)
            else:
                response = await client.get(url)

        except httpx.RequestError:
            raise HTTPException(status_code=500, detail="Failed to proxy the request")

    if response.status_code != 200:
        raise HTTPException(status_code=response.status_code, detail="Failed to proxy the request")
    content_type = response.headers.get("Content-Type", "").lower()

    return response.text, content_type

完整接口

注意⚠️:这里的代理,只是在数据库做一个地址映射,不是真正的代理(fastapi目前没有支持的库和组件)。所以这个地址可能还会调用其他的地址来获取一些信息,js文件等,需要将接口配置为复用以及通配。
:::info
比如 GET http://dev.a.com/dash1 <–> 代理 <–> http://dev.b.com/dash1
这时http://dev.b.com/dash1需要请求一些其他的js文件、或者post请求其他地址获取数据,但是再次请求是由http://dev.a.com/dash1发起的,也就是会变为 POST http://dev.a.com/dash1或者GET http://dev.a.com/dash1/need.js。也就是接口需要做循环代理的
:::

@router.api_route(path="/godash/{report_name}/{file_path:path}", methods=["GET", "POST"])
async def mapping_url(request: Request, report_name: str, file_path: Optional[str] = None,
                      db: AsyncSession = Depends(get_session)):
    try:
        method = request.method
        if method == "POST":
            post_data = await request.json()

        mapping = redis_client.get(report_name)
        url = mapping.decode()
        
        if file_path:
            url = urljoin(url, file_path)
        response, content_type = await reverse_proxy_request(url, method, post_data)
        
        return HTMLResponse(content=response, status_code=200, headers={"content-type": content_type})

    except Exception as e:
        traceback.print_exc()
        logger.info(f"error,{e}")
        raise HTTPException(status_code=404, detail=f"reportMapping error,{e}")



async def reverse_proxy_request(url, method, post_data):
    async with httpx.AsyncClient() as client:
        # 发起代理请求
        try:
            if method == "GET":
                response = await client.get(url)
            elif method == "POST":
                response = await client.post(url, json=post_data)
            else:
                response = await client.get(url)

        except httpx.RequestError:
            raise HTTPException(status_code=500, detail="Failed to proxy the request")

    if response.status_code != 200:
        raise HTTPException(status_code=response.status_code, detail="Failed to proxy the request")
    content_type = response.headers.get("Content-Type", "").lower()

    return response.text, content_type

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值