如何使用FastAPI合并swagger文档?

问题描述 投票:0回答:1

我有两个 FastAPI 服务在不同的端口上运行:

from fastapi import FastAPI

app = FastAPI()


@app.get("/test")
async def root():
    return {"message": "Hello World"}


@app.get("/test/hello/{name}")
async def say_hello(name: str):
    return {"message": f"Hello {name}"}
from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}


@app.get("/hello/{name}")
async def say_hello(name: str):
    return {"message": f"Hello {name}"}

version: '3.9'
services:

  first:
    build:
      context: ./first
      dockerfile: Dockerfile
    ports:
      - "8000:8000"

  second:
    build:
      context: ./second
      dockerfile: Dockerfile
    ports:
      - "9000:9000"

我需要一个端点,我可以在其中选择带有我需要的文档的服务,如下所示: enter image description here

如何组合这些服务的 swagger 文档? FastAPI 的文档说你可以定制一个,但我不知道如何将它们组合成一个端点。

python swagger fastapi openapi swagger-ui
1个回答
0
投票

我克隆了官方存储库

https://github.com/swagger-api/swagger-ui
。我创建了一个单独的服务,将
/dist
文件夹移动到其中,并更改了
swagger-initializer.js
文件中的路径:

window.onload = function() {
 //<editor-fold desc="Changeable Configuration Block">

 // the following lines will be replaced by docker/configurator, when it 
runs in a docker-container
 window.ui = SwaggerUIBundle({
    urls: [
      {url: "http://localhost:8000/openapi.json", name: "First Service"},
      {url: "http://localhost:9000/openapi.json", name: "Second Service"},
    ],
    dom_id: '#swagger-ui',
    deepLinking: true,
    presets: [
      SwaggerUIBundle.presets.apis,
      SwaggerUIStandalonePreset
    ],
    plugins: [
      SwaggerUIBundle.plugins.DownloadUrl
    ],
    layout: "StandaloneLayout"
 });
 //</editor-fold>
};

添加了 Dockerfile

FROM node:14
RUN npm install -g http-server
COPY . /usr/share/nginx/html
CMD ["http-server", "/usr/share/nginx/html", "-p", "80"]

我得到了这个界面:

© www.soinside.com 2019 - 2024. All rights reserved.