Files
video_socket-server/src/swagger.ts

65 lines
1.6 KiB
TypeScript
Raw Normal View History

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';
/**
* 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 查看`);
};