This commit is contained in:
zhangzheng
2026-02-27 18:35:40 +08:00
parent adef8b4cce
commit 1bb1fee5cc
265 changed files with 104076 additions and 92 deletions

39
WebApp/src/server.ts Normal file
View File

@@ -0,0 +1,39 @@
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';
const cors = require('cors');
export const createServer = (config: Options): express.Application => {
const app: express.Application = express();
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);
}
});
});
return app;
};