Files
webRtc/WebApp/src/server.ts

44 lines
1.5 KiB
TypeScript
Raw Normal View History

2026-02-27 18:35:40 +08:00
import * as express from 'express';
import * as path from 'path';
import * as fs from 'fs';
import * as morgan from 'morgan';
import signaling from './signaling';
import { log, LogLevel } from './log';
import Options from './class/options';
import { reset as resetHandler }from './class/httphandler';
2026-03-01 23:41:42 +08:00
import { initSwagger } from './swagger';
2026-02-27 18:35:40 +08:00
const cors = require('cors');
2026-03-01 23:41:42 +08:00
export const createServer = (config: Options): express.Express => {
const app: express.Express = express();
2026-02-27 18:35:40 +08:00
resetHandler(config.mode);
// logging http access
if (config.logging != "none") {
app.use(morgan(config.logging));
}
// const signal = require('./signaling');
app.use(cors({origin: '*'}));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.get('/config', (req, res) => res.json({ useWebSocket: config.type == 'websocket', startupMode: config.mode, logging: config.logging }));
app.use('/signaling', signaling);
app.use(express.static(path.join(__dirname, '../client/public')));
app.use('/module', express.static(path.join(__dirname, '../client/src')));
app.get('/', (req, res) => {
const indexPagePath: string = path.join(__dirname, '../client/public/index.html');
fs.access(indexPagePath, (err) => {
if (err) {
log(LogLevel.warn, `Can't find file ' ${indexPagePath}`);
res.status(404).send(`Can't find file ${indexPagePath}`);
} else {
res.sendFile(indexPagePath);
}
});
});
2026-03-01 23:41:42 +08:00
// 初始化Swagger
initSwagger(app, config);
2026-02-27 18:35:40 +08:00
return app;
};