将 OpenAPI 部署到本地主机
Deploy OpenAPI to localhost
我的组织有很多用于不同项目的 API。我需要一个 OpenAPI 实现,它允许我创建一个独立的门户,其中包含我们所有产品的所有这些 API(更像是一个存储库)。
是否有支持此功能的 OpenAPI?
另一种选择是:能够将多个实例合并为一个 OpenAPI 实例。
有多种方法可以实现 API 目录。
Swagger UI(开源)
Swagger UI 3.0.19+ 可以使用 url
参数 定义。
// index.html
const ui = SwaggerUIBundle({
dom_id: '#swagger-ui',
urls: [
{name: "petstore", url: "http://petstore.swagger.io/v2/swagger.json"},
{name: "instagram", url: "https://api.apis.guru/v2/specs/instagram.com/1.0.0/swagger.yaml"}
],
"urls.primaryName": "petstore", // default spec
...
结果:
由于 Swagger UI 是开源的,您可以根据需要自定义其布局和外观。
SwaggerHub(商业)
SwaggerHub 为团队和组织提供 API 目录托管,无论是在云端还是在本地。 SwaggerHub 还支持 API 设计、协作、代码生成和工作流集成等。
披露:我在制作 SwaggerHub 的公司工作。
为此,一个很好的解决方案是按照指示使用 SwaggerHub。但是,此工具对私人 API 不是免费的。
我有同样的需求,所以,为了帮助社区进行 OpenAPI 设计,我编写了一个名为“OpenAPI Dev Tool”的新工具(在 github)。
使用 OpenAPI Dev Tool,我们可以为 Swagger UI/Redoc 部署多个文档(用于多个使用上下文),具有热重载功能,如 SwaggerHub。
真的好用
每个API在一个配置文件中表示:
{
"folder": "./specs", // Root folder where the specifications are stored
"specs": [ // Array of specifications (several specifications can be exposed)
{ // First specification file
"file": "/petstore.yaml", // Relative path of the specification main file (from "folder" parameter). It has to be an OpenAPI file in YAML or JSON.
"context": { // Object used for template generation (see Template usage chapter below)
...
}
},
{ // Second specification file
"file": "/petstore2.yaml"
...
}
]
}
然后,您只需点击 npx openapi-dev-tool serve
即可为整个 API 服务
使用 http://localhost:3000
等打开浏览器 :)
我的组织有很多用于不同项目的 API。我需要一个 OpenAPI 实现,它允许我创建一个独立的门户,其中包含我们所有产品的所有这些 API(更像是一个存储库)。
是否有支持此功能的 OpenAPI?
另一种选择是:能够将多个实例合并为一个 OpenAPI 实例。
有多种方法可以实现 API 目录。
Swagger UI(开源)
Swagger UI 3.0.19+ 可以使用 url
参数
// index.html
const ui = SwaggerUIBundle({
dom_id: '#swagger-ui',
urls: [
{name: "petstore", url: "http://petstore.swagger.io/v2/swagger.json"},
{name: "instagram", url: "https://api.apis.guru/v2/specs/instagram.com/1.0.0/swagger.yaml"}
],
"urls.primaryName": "petstore", // default spec
...
结果:
由于 Swagger UI 是开源的,您可以根据需要自定义其布局和外观。
SwaggerHub(商业)
SwaggerHub 为团队和组织提供 API 目录托管,无论是在云端还是在本地。 SwaggerHub 还支持 API 设计、协作、代码生成和工作流集成等。
披露:我在制作 SwaggerHub 的公司工作。
为此,一个很好的解决方案是按照指示使用 SwaggerHub。但是,此工具对私人 API 不是免费的。 我有同样的需求,所以,为了帮助社区进行 OpenAPI 设计,我编写了一个名为“OpenAPI Dev Tool”的新工具(在 github)。
使用 OpenAPI Dev Tool,我们可以为 Swagger UI/Redoc 部署多个文档(用于多个使用上下文),具有热重载功能,如 SwaggerHub。
真的好用
每个API在一个配置文件中表示:
{
"folder": "./specs", // Root folder where the specifications are stored
"specs": [ // Array of specifications (several specifications can be exposed)
{ // First specification file
"file": "/petstore.yaml", // Relative path of the specification main file (from "folder" parameter). It has to be an OpenAPI file in YAML or JSON.
"context": { // Object used for template generation (see Template usage chapter below)
...
}
},
{ // Second specification file
"file": "/petstore2.yaml"
...
}
]
}
然后,您只需点击 npx openapi-dev-tool serve
即可为整个 API 服务
使用 http://localhost:3000
等打开浏览器 :)