动态更改服务器 url

jNi*_*ick 5 swagger-ui openapi

我有为多个客户端部署的网络应用程序(比如说 client1.com、client2.com 和 client3.com)。我使用 OA3 规范编写了 REST API 文档。

我有index.yaml,看起来像:

openapi: 3.0.0

info:
  title: REST API
  description: REST API
  version: 0.0.1

servers:
  - url: http://client1.com/api
  description: Something ...

tags: ...
Run Code Online (Sandbox Code Playgroud)

和标准 Swagger-ui index.html

// Begin Swagger UI call region
const ui = SwaggerUIBundle({
  url: "docs/index.yaml",
  dom_id: '#swagger-ui',
  deepLinking: true,
  presets: [
    SwaggerUIBundle.presets.apis,
    SwaggerUIStandalonePreset.slice(1)
  ],
  plugins: [
    SwaggerUIBundle.plugins.DownloadUrl
  ],
  layout: "StandaloneLayout"
});
// End Swagger UI call region
window.ui = ui;
Run Code Online (Sandbox Code Playgroud)

我的问题是,我需要根据我部署应用程序的位置和客户端来更改servers.url,以便每个客户端都可以在自己的服务器上测试API,并且我不希望客户端看到彼此并相互了解REST API 文档。

我如何“动态”更改/设置 server.url ?一种“绕行”解决方案是为每个客户端复制index.yaml并硬编码servers.url,但我确信有更好的方法,但我没有看到/不知道。

编辑#1:给出重复的答案没有帮助,因为当我将服务器设置为

servers: 
  - url: /api
Run Code Online (Sandbox Code Playgroud)

swagger ui 仍然指向,http://localhost:8080/api/但我的应用程序 url 是http://localhost:8080/myapp/. 我可以设置servers: - url: /myapp/api,但这是 index.yaml 中的硬编码值,这对我不起作用。我需要“可配置”服务器网址。但还是谢谢你

编辑#2:我当前的解决方案是使用服务器端代码处理 yaml。在index.yaml我有:

servers:
  - url: ##INSERT_SERVERS_TAG_HEREapi
Run Code Online (Sandbox Code Playgroud)

我替换##INSERT_SERVERS_TAG_HEREmyapp.client.com/. 但我仍在寻找更好的解决方案。