我正在开发一个 API,它从本地文件夹返回一些文件,以模拟我们为开发人员环境提供的系统。
大多数系统的工作原理是输入识别人员的代码并返回他的文件。但是,该系统的一条路径具有独特的行为:它使用 POST 方法(请求正文包含 Id 代码),我正在努力使其工作。
这是我当前的代码:
import json
from pathlib import Path
import yaml
from fastapi import FastAPI
from pydantic.main import BaseModel
app = FastAPI()
class RequestModel(BaseModel):
assetId: str
@app.get("/{group}/{service}/{assetId}")
async def return_json(group: str, service: str, assetId: str):
with open("application-dev.yml", "r") as config_file:
output_dir = yaml.load(config_file)['path']
path = Path(output_dir + f"{group}/{service}/")
file = [f for f in path.iterdir() if f.stem == assetId][0]
if file.exists():
with file.open() as target_file:
return json.load(target_file)
@app.post("/DataService/ServiceProtocol")
async def return_post_path(request: RequestModel):
return return_json("DataService", "ServiceProtocol", …
Run Code Online (Sandbox Code Playgroud) 我需要将“ /swagger-ui.html ”重定向到文档页面。
我试过:
app = FastAPI()
@app.get("/swagger-ui.html")
async def docs_redirect():
response = RedirectResponse(url='/docs')
return response
Run Code Online (Sandbox Code Playgroud)
和
app = FastAPI(docs_url="/swagger-ui.html")
@app.get("/")
async def docs_redirect():
response = RedirectResponse(url='/swagger-ui.html')
return response
Run Code Online (Sandbox Code Playgroud)
但是,直接运行项目(使用 uvicorn 命令)我可以工作,但是当我将其放在 Docker 容器上时,它会在浏览器上输出此消息,询问位置,但没有任何内容可以作为输入:
无法推断基本 URL。当使用动态 Servlet 注册或 API 位于 API 网关后面时,这种情况很常见。基本 url 是提供所有 swagger 资源的根。例如,如果 api 在http://example.org/api/v2/api-docs上可用,则基本 URL 为http://example.org/api/。请手动输入位置:
这是我的 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 …
Run Code Online (Sandbox Code Playgroud)