2023-09-02 12:13:50 +00:00
|
|
|
import 'dotenv/config';
|
2023-09-14 16:53:03 +00:00
|
|
|
import './init';
|
2023-08-31 13:17:57 +00:00
|
|
|
import express from 'express';
|
2023-09-02 17:01:55 +00:00
|
|
|
import 'express-async-errors';
|
2023-09-02 14:53:57 +00:00
|
|
|
import compression from 'compression';
|
2023-10-21 16:26:13 +00:00
|
|
|
import swaggerUI from 'swagger-ui-express';
|
2023-09-02 17:01:55 +00:00
|
|
|
import passport from 'passport';
|
2023-09-05 06:08:38 +00:00
|
|
|
import morgan from 'morgan';
|
2023-09-03 11:28:53 +00:00
|
|
|
import { websiteRouter } from './router/website';
|
2023-09-04 17:18:43 +00:00
|
|
|
import { workspaceRouter } from './router/workspace';
|
2023-09-17 06:41:40 +00:00
|
|
|
import { telemetryRouter } from './router/telemetry';
|
2023-10-21 16:26:13 +00:00
|
|
|
import {
|
|
|
|
trpcExpressMiddleware,
|
|
|
|
trpcOpenapiDocument,
|
|
|
|
trpcOpenapiHttpHandler,
|
|
|
|
} from './trpc';
|
2023-09-30 16:01:43 +00:00
|
|
|
import { initUdpServer } from './udp/server';
|
2023-10-03 11:47:17 +00:00
|
|
|
import { createServer } from 'http';
|
|
|
|
import { initSocketio } from './ws';
|
2023-10-04 17:56:33 +00:00
|
|
|
import { monitorManager } from './model/monitor';
|
2023-10-21 16:26:13 +00:00
|
|
|
import { env } from './utils/env';
|
2023-10-28 12:12:53 +00:00
|
|
|
import cors from 'cors';
|
2023-10-31 16:02:52 +00:00
|
|
|
import { serverStatusRouter } from './router/serverStatus';
|
2024-01-05 15:05:19 +00:00
|
|
|
import { initCronjob } from './cronjob';
|
2024-01-05 16:16:43 +00:00
|
|
|
import { logger } from './utils/logger';
|
2024-01-06 14:08:10 +00:00
|
|
|
import { monitorRouter } from './router/monitor';
|
2024-01-07 15:22:39 +00:00
|
|
|
import { healthRouter } from './router/health';
|
2024-01-28 17:00:35 +00:00
|
|
|
import path from 'path';
|
2023-09-02 17:01:55 +00:00
|
|
|
|
2024-01-13 10:16:09 +00:00
|
|
|
const port = env.port;
|
2023-08-31 13:17:57 +00:00
|
|
|
|
|
|
|
const app = express();
|
2023-10-03 11:47:17 +00:00
|
|
|
const httpServer = createServer(app);
|
2023-08-31 13:17:57 +00:00
|
|
|
|
2023-09-30 16:01:43 +00:00
|
|
|
initUdpServer(port);
|
|
|
|
|
2023-10-03 11:47:17 +00:00
|
|
|
initSocketio(httpServer);
|
2023-09-18 08:08:16 +00:00
|
|
|
|
2024-01-05 15:05:19 +00:00
|
|
|
initCronjob();
|
|
|
|
|
2023-10-04 17:56:33 +00:00
|
|
|
monitorManager.startAll();
|
|
|
|
|
2023-09-02 14:53:57 +00:00
|
|
|
app.use(compression());
|
|
|
|
app.use(express.json());
|
2023-09-02 17:01:55 +00:00
|
|
|
app.use(passport.initialize());
|
2023-10-28 12:12:53 +00:00
|
|
|
app.use(morgan('tiny'));
|
|
|
|
app.use(cors());
|
2024-01-28 17:00:35 +00:00
|
|
|
app.use(express.static('public'));
|
2023-09-02 14:53:57 +00:00
|
|
|
|
|
|
|
// http://expressjs.com/en/advanced/best-practice-security.html#at-a-minimum-disable-x-powered-by-header
|
|
|
|
app.disable('x-powered-by');
|
|
|
|
|
2023-10-23 16:01:03 +00:00
|
|
|
app.use(
|
|
|
|
'/tracker.js',
|
|
|
|
express.static('./public/tracker.js', {
|
|
|
|
maxAge: '7d',
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2024-01-07 15:22:39 +00:00
|
|
|
app.use('/health', healthRouter);
|
2023-09-03 11:28:53 +00:00
|
|
|
app.use('/api/website', websiteRouter);
|
2023-09-04 17:18:43 +00:00
|
|
|
app.use('/api/workspace', workspaceRouter);
|
2024-01-06 14:08:10 +00:00
|
|
|
app.use('/monitor', monitorRouter);
|
2023-10-12 14:53:12 +00:00
|
|
|
app.use('/telemetry', telemetryRouter);
|
2023-10-31 16:02:52 +00:00
|
|
|
app.use('/serverStatus', serverStatusRouter);
|
|
|
|
|
|
|
|
app.use('/trpc', trpcExpressMiddleware);
|
2023-09-02 14:53:57 +00:00
|
|
|
|
2023-10-21 16:26:13 +00:00
|
|
|
if (env.allowOpenapi) {
|
|
|
|
app.use('/open/_ui', swaggerUI.serve, swaggerUI.setup(trpcOpenapiDocument));
|
2023-10-22 17:44:08 +00:00
|
|
|
app.use('/open/_document', (req, res) => res.send(trpcOpenapiDocument));
|
2023-10-21 16:26:13 +00:00
|
|
|
app.use('/open', trpcOpenapiHttpHandler);
|
|
|
|
}
|
2023-09-27 09:56:32 +00:00
|
|
|
|
2024-01-28 17:00:35 +00:00
|
|
|
// fallback
|
2024-02-22 15:51:52 +00:00
|
|
|
app.use('/*', (req, res) => {
|
|
|
|
if (req.method === 'GET' && req.accepts('html')) {
|
2024-01-28 17:00:35 +00:00
|
|
|
res.sendFile(path.join(process.cwd(), 'public', 'index.html'));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-09-02 14:53:57 +00:00
|
|
|
app.use((err: any, req: any, res: any, next: any) => {
|
2024-02-22 15:51:52 +00:00
|
|
|
logger.error('[express]', err);
|
2023-09-03 13:05:22 +00:00
|
|
|
res.status(500).json({ message: err.message });
|
2023-08-31 13:17:57 +00:00
|
|
|
});
|
|
|
|
|
2023-10-03 11:47:17 +00:00
|
|
|
httpServer.listen(port, () => {
|
2024-01-20 08:22:44 +00:00
|
|
|
logger.info(`Server is listening on port ${port}...`);
|
|
|
|
if (env.allowOpenapi) {
|
|
|
|
logger.info(`Openapi UI: http://127.0.0.1:${port}/open/_ui`);
|
|
|
|
}
|
|
|
|
logger.info(`Website: http://127.0.0.1:${port}`);
|
2023-08-31 13:17:57 +00:00
|
|
|
});
|