2026-04-29 15:18:30 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* Swagger配置文件
|
|
|
|
|
|
* 用于设置API文档的基本信息和路由
|
|
|
|
|
|
*/
|
|
|
|
|
|
import * as swaggerJSDoc from 'swagger-jsdoc';
|
|
|
|
|
|
import * as swaggerUi from 'swagger-ui-express';
|
|
|
|
|
|
import { Express } from 'express';
|
|
|
|
|
|
import Options from './class/options';
|
2026-05-06 16:08:00 +08:00
|
|
|
|
import { log, LogLevel } from './log';
|
2026-04-29 15:18:30 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 初始化Swagger
|
|
|
|
|
|
* @param app Express应用实例
|
|
|
|
|
|
* @param config 配置选项
|
|
|
|
|
|
*/
|
|
|
|
|
|
export const initSwagger = (app: Express, config: Options): void => {
|
|
|
|
|
|
// 根据配置生成服务器URL
|
|
|
|
|
|
const protocol = config.secure ? 'https' : 'http';
|
|
|
|
|
|
const port = config.port || 8080;
|
|
|
|
|
|
const serverUrl = `${protocol}://localhost:${port}`;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Swagger配置选项
|
|
|
|
|
|
*/
|
|
|
|
|
|
const swaggerOptions = {
|
|
|
|
|
|
definition: {
|
|
|
|
|
|
openapi: '3.0.0',
|
|
|
|
|
|
info: {
|
|
|
|
|
|
title: 'WebRTC Signaling API',
|
|
|
|
|
|
version: '1.0.0',
|
|
|
|
|
|
description: 'WebRTC信令服务器API文档',
|
|
|
|
|
|
contact: {
|
|
|
|
|
|
name: 'WebRTC Team',
|
|
|
|
|
|
email: 'contact@webrtc.example.com'
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
servers: [
|
|
|
|
|
|
{
|
|
|
|
|
|
url: serverUrl,
|
|
|
|
|
|
description: '本地开发服务器'
|
|
|
|
|
|
}
|
|
|
|
|
|
],
|
|
|
|
|
|
components: {
|
|
|
|
|
|
securitySchemes: {
|
|
|
|
|
|
sessionAuth: {
|
|
|
|
|
|
type: 'apiKey',
|
|
|
|
|
|
in: 'header',
|
|
|
|
|
|
name: 'session-id',
|
|
|
|
|
|
description: '会话ID'
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
security: [
|
|
|
|
|
|
{
|
|
|
|
|
|
sessionAuth: []
|
|
|
|
|
|
}
|
|
|
|
|
|
]
|
|
|
|
|
|
},
|
|
|
|
|
|
apis: ['./src/class/httphandler.ts', './src/signaling.ts']
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const swaggerSpec = swaggerJSDoc(swaggerOptions);
|
|
|
|
|
|
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
|
2026-05-06 16:08:00 +08:00
|
|
|
|
log(LogLevel.info, `Swagger文档已初始化,访问 ${serverUrl}/api-docs 查看`);
|
2026-04-29 15:18:30 +08:00
|
|
|
|
};
|