在 Debian 上使用 Swagger 进行 API 文档共享
一 方案总览
二 方案一 集成到 Node.js Express
openapi: 3.0.0
info:
title: Sample API
version: 1.0.0
paths:
/hello:
get:
summary: 返回问候
responses:
'200':
description: OK
content:
application/json:
schema:
type: object
properties:
message:
type: string
const express = require('express');
const swaggerUi = require('swagger-ui-express');
const YAML = require('yamljs');
const app = express();
const swaggerDocument = YAML.load('./openapi.yaml');
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.get('/hello', (req, res) => res.json({ message: 'Hello, Swagger on Debian' }));
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server on :${PORT}, docs at /api-docs`));
三 方案二 生成静态文档并用 Nginx 托管
四 方案三 使用 Docker 快速部署 Swagger UI
docker run -d -p 8080:8080 \
-e SWAGGER_JSON=/spec/openapi.yaml \
-v $(pwd)/openapi.yaml:/spec/openapi.yaml \
swaggerapi/swagger-ui
五 安全与运维建议