简体   繁体   English

如何在 Docker 上运行时重定向 FastAPI 文档

[英]How to redirect FastAPI Documentation while running on Docker

I need to redirect " /swagger-ui.html " to the documentation page.我需要将“ /swagger-ui.html ”重定向到文档页面。

I tried:我试过了:

app = FastAPI()

@app.get("/swagger-ui.html")
async def docs_redirect():
    response = RedirectResponse(url='/docs')
    return response

and

app = FastAPI(docs_url="/swagger-ui.html")

@app.get("/")
async def docs_redirect():
    response = RedirectResponse(url='/swagger-ui.html')
    return response

But, running the project directly (using uvicorn command) I works, but when I put it on a Docker container, it outputs this message on the browser, asking for the location, where nothing works as input:但是,直接运行项目(使用 uvicorn 命令)我可以工作,但是当我把它放在 Docker 容器上时,它会在浏览器上输出这条消息,询问位置,输入没有任何作用:

Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway.无法推断 base url。这在使用动态 servlet 注册或 API 在 API 网关后面时很常见。 The base url is the root of where all the swagger resources are served.基础 url 是提供所有 swagger 资源的根。 For eg if the api is available at http://example.org/api/v2/api-docs then the base url is http://example.org/api/ .例如,如果 api 在http://example.org/api/v2/api-docs可用,那么基础 url 是http://example.org/api/ Please enter the location manually:请手动输入位置:

Here's my dockerfile:这是我的 dockerfile:

FROM python:3.8
USER root
RUN mkdir -p /usr/local/backend
WORKDIR /usr/local/backend
EXPOSE 8080
ARG BUILD_ENV=dev 
ENV BUILD_ENV=$BUILD_ENV
COPY . /usr/local/backend
RUN pip install -r requirements.txt
ENTRYPOINT ["uvicorn", "app.main:app", "--port", "8080"]

Try this code, it works for me when I use docker-compose :试试这个代码,当我使用docker-compose时它对我有用:

app = FastAPI()

@app.get("/")
async def docs_redirect():
    return RedirectResponse(url='/docs')

To avoid showing the redirect in the docs page避免在文档页面中显示重定向

@app.get("/", include_in_schema=False)
async def docs_redirect():
    return RedirectResponse(url='/docs')

Documentation here: https://fastapi.tiangolo.com/advanced/path-operation-advanced-configuration/#exclude-from-openapi此处的文档: https://fastapi.tiangolo.com/advanced/path-operation-advanced-configuration/#exclude-from-openapi

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 docker 容器内运行时自动在 API 路由文件中进行 FASTAPI 拾取更改? - How to make FASTAPI pickup changes in an API routing file automatically while running inside a docker container? FastAPI 应用程序在本地运行但不在 Docker 容器中 - FastAPI app running locally but not in Docker container 在 Docker 中运行的 FastAPI 应用程序上实现 pytest - Implement pytest over FastAPI app running in Docker 运行 FastAPI 的 docker img 时出现 ModuleNotFoundError - ModuleNotFoundError when running docker img of FastAPI 使用 uvicorn 和 gunicorn nginx 在 docker 中运行 FastAPI - Running FastAPI in docker with uvicorn and gunicorn nginx 如何使用 FastApi 端点或另一个 docker 容器上的 python 脚本查询在 docker 容器上运行的 postgres 数据库? - How do i query a postgres database running on docker container using a FastApi endpoint or python script on another docker container? 将 FastApi 文档分成几个部分 - Separate FastApi documentation into sections 在返回托管在 FastAPI + uvicorn + Docker 应用程序上的状态 200 之前,继续获取“307 临时重定向” - 如何返回状态 200? - Keep getting "307 Temporary Redirect" before returning status 200 hosted on FastAPI + uvicorn + Docker app - how to return status 200? 如何在 fastapi 端点内重定向到动态 url - How to redirect to dynamic url inside fastapi endpoint 如何在生产服务器的 Fastapi 中禁用 Swagger ui 文档? - How to disable Swagger ui documentation in Fastapi for production server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM