Swagger是一个强大的API文档工具集,可以帮助开发者设计、构建、记录和使用RESTful API。以下是在Linux系统中使用Swagger进行API文档管理的详细指南:
# 使用Docker安装Swagger Editor
docker pull swaggerapi/swagger-editor
docker run -d -p 8080:8080 swaggerapi/swagger-editor
# 或者使用npm安装
npm install -g swagger-editor
swagger-editor
# 使用Docker安装Swagger UI
docker pull swaggerapi/swagger-ui
docker run -d -p 8081:8080 -e SWAGGER_JSON=/foo/swagger.json -v /path/to/your/swagger.json:/foo/swagger.json swaggerapi/swagger-ui
# 下载最新版Swagger Codegen
wget https://repo1.maven.org/maven2/io/swagger/swagger-codegen-cli/2.4.19/swagger-codegen-cli-2.4.19.jar -O swagger-codegen-cli.jar
# 使用示例
java -jar swagger-codegen-cli.jar generate -i http://petstore.swagger.io/v2/swagger.json -l python -o /tmp/python-client
创建一个swagger.yaml
或swagger.json
文件来定义你的API:
# swagger.yaml 示例
swagger: "2.0"
info:
version: "1.0.0"
title: "Sample API"
description: "A sample API to demonstrate Swagger"
termsOfService: "http://swagger.io/terms/"
contact:
name: "API Support"
email: "support@example.com"
license:
name: "MIT"
host: "api.example.com"
basePath: "/v1"
schemes:
- "https"
paths:
/users:
get:
summary: "Returns a list of users"
description: "Optional extended description in Markdown."
produces:
- "application/json"
responses:
200:
description: "A JSON array of user names"
schema:
type: "array"
items:
type: "string"
npm install swagger-jsdoc swagger-ui-express --save
然后在你的Express应用中:
const express = require('express');
const swaggerJsdoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
const app = express();
// Swagger配置
const options = {
definition: {
openapi: '3.0.0',
info: {
title: 'Sample API',
version: '1.0.0',
description: 'A sample API',
},
},
apis: ['./routes/*.js'], // 包含API文档注释的文件路径
};
const specs = swaggerJsdoc(options);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs));
// ...其他路由和中间件
app.listen(3000, () => console.log('Server running on port 3000'));
pip install flask-restplus # 或 flask-restx
然后在Flask应用中:
from flask import Flask
from flask_restplus import Api, Resource
app = Flask(__name__)
api = Api(app, version='1.0', title='Sample API',
description='A sample API')
@api.route('/users')
class UserList(Resource):
def get(self):
"""返回用户列表"""
return {'users': ['user1', 'user2']}
if __name__ == '__main__':
app.run(debug=True)
许多语言支持从代码注释自动生成Swagger文档:
swagger-core
注解flask-restplus
或drf-yasg
(Django REST framework)swagger-jsdoc
如果你的API已经存在,可以使用swagger-codegen
从API生成文档:
java -jar swagger-codegen-cli.jar generate -i http://your-api-url/swagger.json -l html -o ./api-docs
server {
listen 80;
server_name api-docs.example.com;
location / {
root /path/to/swagger-ui/dist;
index index.html;
}
location /api {
proxy_pass http://your-api-server;
}
}
Swagger Hub是一个基于云的平台,提供协作功能:
可以设置CI/CD流程,在API变更时自动更新文档:
# .gitlab-ci.yml 示例
update_docs:
stage: deploy
script:
- npm install -g swagger-cli
- swagger-cli bundle api/swagger.yaml -o public/swagger.json
only:
- master
# 示例:基本认证保护Swagger UI
location /api-docs {
auth_basic "Restricted Area";
auth_basic_user_file /etc/nginx/.htpasswd;
}
通过以上步骤,你可以在Linux环境中有效地使用Swagger进行API文档管理,提高API的可维护性和易用性。