在Debian系统上进行Swagger的版本控制,可以遵循以下步骤:
首先,确保你已经安装了Swagger工具。你可以使用npm(Node.js的包管理器)来安装Swagger。
sudo apt update
sudo apt install nodejs npm
sudo npm install -g swagger-jsdoc swagger-ui-express
创建一个新的Node.js项目,并初始化它。
mkdir my-swagger-project
cd my-swagger-project
npm init -y
在你的项目根目录下创建一个swagger.json
文件,用于定义你的API规范。
{
"swagger": "2.0",
"info": {
"description": "Sample API",
"version": "1.0.0"
},
"host": "api.example.com",
"basePath": "/v1",
"paths": {
"/users": {
"get": {
"summary": "List all users",
"responses": {
"200": {
"description": "A list of users"
}
}
}
}
}
}
在你的项目中创建一个app.js
文件,并使用Swagger UI Express来展示你的API文档。
const express = require('express');
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');
const app = express();
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}`);
});
使用Git进行版本控制。
git init
git add .
git commit -m "Initial commit with Swagger setup"
当你更新API规范时,修改swagger.json
文件并提交更改。
# 修改swagger.json文件
vim swagger.json
# 提交更改
git add swagger.json
git commit -m "Update API version to 1.0.1"
你可以将你的应用部署到Debian服务器上。使用PM2来管理你的Node.js应用。
sudo npm install -g pm2
pm2 start app.js --name my-swagger-app
你可以设置CI/CD管道来自动化部署过程。例如,使用GitHub Actions。
在项目根目录下创建.github/workflows/deploy.yml
文件:
name: Deploy to Debian
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Install dependencies
run: npm install
- name: Build and deploy
run: |
npm run build
pm2 deploy ecosystem.config.js --env production
在项目根目录下创建ecosystem.config.js
文件:
module.exports = {
apps: [{
name: 'my-swagger-app',
script: 'app.js',
instances: 'max',
exec_mode: 'cluster',
env: {
NODE_ENV: 'production'
},
env_production: {
NODE_ENV: 'production'
}
}]
};
通过以上步骤,你可以在Debian系统上进行Swagger的版本控制,并且可以方便地部署和更新你的API文档。