在Ubuntu中配置Swagger UI可以通过多种方法实现,以下是几种常见的方法:
sudo apt update
sudo apt install docker.io
docker pull swaggerapi/swagger-ui-express
docker run -p 8080:8080 swaggerapi/swagger-ui-express
http://localhost:8080,你应该能看到Swagger UI界面。sudo apt update
sudo apt install nodejs npm
mkdir swagger-ui-project
cd swagger-ui-project
npm init -y
npm install swagger-ui-express
app.js 的文件,并添加以下代码:const express = require('express');
const swaggerUi = require('swagger-ui-express');
const YAML = require('yamljs');
// Load Swagger document
const swaggerDocument = YAML.load('./swagger.yaml');
const app = express();
// Serve Swagger docs
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
swagger.yaml 的文件,并添加你的API文档。例如:swagger: '2.0'
info:
title: Sample API
description: A sample API to demonstrate Swagger UI integration
version: '1.0.0'
paths:
/users:
get:
summary: List all users
responses:
'200':
description: A list of users
content:
application/json:
schema:
type: array
items:
$ref: '#/definitions/User'
definitions:
User:
type: object
properties:
id:
type: integer
name:
type: string
required:
- id
- name
node app.js
http://localhost:3000/api-docs,你应该能看到Swagger UI界面。sudo apt install nginx
wget https://github.com/swagger-api/swagger-ui/releases/download/v3.47.1/swagger-ui.zip
unzip swagger-ui.zip
/etc/nginx/sites-available/default 文件,添加以下内容:server {
listen 80;
server_name localhost;
location /swagger-editor {
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /swagger-ui {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
}
}
sudo systemctl restart nginx
http://localhost/swagger-editorhttp://localhost/swagger-ui以上方法可以帮助你在Ubuntu上成功安装和配置Swagger UI,并进行API文档的查看和测试。选择适合你的方法并按照步骤操作即可。如果在安装过程中遇到问题,可以参考相关的官方文档或社区论坛寻求帮助。