66 lines
1.7 KiB
TypeScript
66 lines
1.7 KiB
TypeScript
/**
|
||
* Swagger配置文件
|
||
* 用于设置API文档的基本信息和路由
|
||
*/
|
||
import * as swaggerJSDoc from 'swagger-jsdoc';
|
||
import * as swaggerUi from 'swagger-ui-express';
|
||
import { Express } from 'express';
|
||
import Options from './class/options';
|
||
import { log, LogLevel } from './log';
|
||
|
||
/**
|
||
* 初始化Swagger
|
||
* @param app Express应用实例
|
||
* @param config 配置选项
|
||
*/
|
||
export const initSwagger = (app: Express, config: Options): void => {
|
||
const protocol = config.secure ? 'https' : 'http';
|
||
const port = config.port || 8080;
|
||
const serverUrl = `${protocol}://localhost:${port}`;
|
||
const servers = [
|
||
{
|
||
url: serverUrl,
|
||
description: config.secure ? '本地HTTPS服务器' : '本地HTTP服务器'
|
||
}
|
||
];
|
||
|
||
/**
|
||
* 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,
|
||
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));
|
||
log(LogLevel.info, `Swagger文档已初始化,访问 ${serverUrl}/api-docs 查看`);
|
||
};
|