运行 Docker 时如何重定向 FastAPI 文档
How to redirect FastAPI Documentation while running on Docker
我需要将“/swagger-ui.html”重定向到文档页面。
我试过了:
app = FastAPI()
@app.get("/swagger-ui.html")
async def docs_redirect():
response = RedirectResponse(url='/docs')
return response
和
app = FastAPI(docs_url="/swagger-ui.html")
@app.get("/")
async def docs_redirect():
response = RedirectResponse(url='/swagger-ui.html')
return response
但是,运行 项目直接(使用 uvicorn 命令)我可以工作,但是当我把它放在 Docker 容器上时,它会在浏览器上输出这条消息,询问位置,输入无效:
Unable to infer base url. This is common when using dynamic servlet
registration or when the API is behind an API Gateway. The base url is
the root of where all the swagger resources are served. For e.g. if
the api is available at http://example.org/api/v2/api-docs then the
base url is http://example.org/api/. Please enter the location
manually:
这是我的 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"]
试试这个代码,当我使用 docker-compose
:
时它对我有用
app = FastAPI()
@app.get("/")
async def docs_redirect():
return RedirectResponse(url='/docs')
我需要将“/swagger-ui.html”重定向到文档页面。
我试过了:
app = FastAPI()
@app.get("/swagger-ui.html")
async def docs_redirect():
response = RedirectResponse(url='/docs')
return response
和
app = FastAPI(docs_url="/swagger-ui.html")
@app.get("/")
async def docs_redirect():
response = RedirectResponse(url='/swagger-ui.html')
return response
但是,运行 项目直接(使用 uvicorn 命令)我可以工作,但是当我把它放在 Docker 容器上时,它会在浏览器上输出这条消息,询问位置,输入无效:
Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway. The base url is the root of where all the swagger resources are served. For e.g. if the api is available at http://example.org/api/v2/api-docs then the base url is http://example.org/api/. Please enter the location manually:
这是我的 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"]
试试这个代码,当我使用 docker-compose
:
app = FastAPI()
@app.get("/")
async def docs_redirect():
return RedirectResponse(url='/docs')