/** * Swagger配置文件 * 用于设置API文档的基本信息和路由 */ import * as swaggerJSDoc from 'swagger-jsdoc'; import * as swaggerUi from 'swagger-ui-express'; import { Express } from 'express'; import Options from './class/options'; /** * 初始化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)); console.log(`Swagger文档已初始化,访问 ${serverUrl}/api-docs 查看`); };