在Debian上安裝Swagger,可以按照以下步驟進行:
安裝Docker: 如果你還沒有安裝Docker,可以使用以下命令來安裝:
sudo apt update
sudo apt install docker.io
拉取Swagger UI Docker鏡像: 使用以下命令從Docker Hub拉取Swagger UI的鏡像:
docker pull swaggerapi/swagger-ui
運行Swagger UI容器: 運行以下命令來啟動Swagger UI容器,并將其映射到本地的80端口:
docker run -p 80:80 -d swaggerapi/swagger-ui
訪問Swagger UI:
打開瀏覽器,訪問 http://localhost/swagger-ui/
,你應該能夠看到Swagger UI界面。
如果你已經在Debian上安裝了Node.js和npm,可以使用以下步驟來安裝Swagger UI:
安裝Swagger UI: 使用npm安裝Swagger UI:
npm install -g swagger-ui-express
創建一個簡單的Express應用:
創建一個新的目錄并進入該目錄,然后創建一個名為 app.js
的文件,內容如下:
const express = require('express');
const swaggerUi = require('swagger-ui-express');
const YAML = require('yamljs');
const app = express();
// Load Swagger document
const swaggerDocument = YAML.load('./swagger.yaml');
// 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文檔:
在同一目錄下創建一個名為 swagger.yaml
的文件,并添加你的API文檔。例如:
swagger: '2.0'
info:
title: Sample API
description: A sample API to demonstrate Swagger UI
version: '1.0.0'
paths:
/users:
get:
summary: List all users
responses:
'200':
description: An array of users
schema:
type: array
items:
$ref: '#/definitions/User'
definitions:
User:
type: object
properties:
id:
type: integer
format: int64
name:
type: string
啟動Express應用: 在終端中運行以下命令來啟動你的Express應用:
node app.js
訪問Swagger UI:
打開瀏覽器,訪問 http://localhost:3000/api-docs
,你應該能夠看到Swagger UI界面。
如果你已經在Debian上安裝了Python和pip,可以使用以下步驟來安裝Swagger UI:
安裝Swagger UI: 使用pip安裝Swagger UI:
pip install swagger-ui-python
創建一個簡單的Flask應用:
創建一個新的目錄并進入該目錄,然后創建一個名為 app.py
的文件,內容如下:
from flask import Flask, jsonify
from swagger_ui_python import swagger_ui_blueprint
app = Flask(__name__)
# Swagger UI blueprint
app.register_blueprint(swagger_ui_blueprint,
url_prefix='/swagger-ui',
config={
'app_name': "Sample API"
})
# Sample endpoint
@app.route('/api/users')
def get_users():
return jsonify([
{'id': 1, 'name': 'Alice'},
{'id': 2, 'name': 'Bob'}
])
if __name__ == '__main__':
app.run(debug=True)
啟動Flask應用: 在終端中運行以下命令來啟動你的Flask應用:
python app.py
訪問Swagger UI:
打開瀏覽器,訪問 http://localhost:5000/swagger-ui/
,你應該能夠看到Swagger UI界面。
選擇適合你環境的方法進行安裝即可。