Files
parcer/server/src/server.ts

112 lines
3.3 KiB
TypeScript
Raw Normal View History

2024-05-30 04:49:38 +05:30
import express from 'express';
2024-10-03 17:19:20 +05:30
import path from 'path';
2024-05-30 04:49:38 +05:30
import http from 'http';
import cors from 'cors';
2024-09-25 18:02:29 +05:30
import dotenv from 'dotenv';
dotenv.config();
2024-10-01 15:30:13 +05:30
import { record, workflow, storage, auth, integration, proxy } from './routes';
2024-05-30 04:49:38 +05:30
import { BrowserPool } from "./browser-management/classes/BrowserPool";
2024-09-23 23:47:10 +05:30
import logger from './logger';
2024-10-14 23:47:17 +05:30
import { connectDB, syncDB } from './storage/db'
2024-09-25 11:54:04 +05:30
import bodyParser from 'body-parser';
2024-09-23 23:47:10 +05:30
import cookieParser from 'cookie-parser';
import csrf from 'csurf';
2024-05-30 04:49:38 +05:30
import { SERVER_PORT } from "./constants/config";
2024-09-11 13:21:16 +05:30
import { Server } from "socket.io";
2024-10-03 16:34:30 +05:30
import { readdirSync } from "fs"
2024-10-07 01:37:50 +05:30
import { fork } from 'child_process';
2024-10-29 04:00:33 +05:30
import { capture } from "./utils/analytics";
2024-10-28 23:12:12 +05:30
import swaggerUi from 'swagger-ui-express';
import swaggerSpec from './swagger/config';
2024-05-30 04:49:38 +05:30
const app = express();
2024-09-25 17:01:55 +05:30
app.use(cors({
2024-10-24 22:26:12 +05:30
origin: 'http://localhost:5173',
2024-09-25 17:01:55 +05:30
credentials: true,
}));
2024-05-30 04:49:38 +05:30
app.use(express.json());
const server = http.createServer(app);
/**
* Globally exported singleton instance of socket.io for socket communication with the client.
* @type {Server}
*/
export const io = new Server(server);
/**
* {@link BrowserPool} globally exported singleton instance for managing browsers.
*/
export const browserPool = new BrowserPool();
2024-09-25 11:54:04 +05:30
app.use(bodyParser.json({ limit: '10mb' }))
2024-10-12 22:46:41 +05:30
app.use(bodyParser.urlencoded({ extended: true, limit: '10mb', parameterLimit: 9000 }));
2024-09-23 23:47:35 +05:30
// parse cookies - "cookie" is true in csrfProtection
app.use(cookieParser())
2024-09-23 23:47:10 +05:30
app.use('/record', record);
app.use('/workflow', workflow);
app.use('/storage', storage);
2024-09-15 17:57:45 +05:30
app.use('/auth', auth);
2024-09-17 21:03:15 +05:30
app.use('/integration', integration);
2024-10-01 15:30:13 +05:30
app.use('/proxy', proxy);
2024-10-28 23:12:12 +05:30
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
2024-10-03 17:19:20 +05:30
readdirSync(path.join(__dirname, 'api')).forEach((r) => {
const route = require(path.join(__dirname, 'api', r));
const router = route.default || route; // Use .default if available, fallback to route
if (typeof router === 'function') {
app.use('/api', router); // Use the default export or named router
} else {
console.error(`Error: ${r} does not export a valid router`);
}
});
2024-10-03 16:34:30 +05:30
2024-10-30 12:04:38 +05:30
const isProduction = process.env.NODE_ENV === 'production';
2024-10-30 22:05:16 +05:30
const workerPath = path.resolve(__dirname, isProduction ? './worker.js' : './worker.ts');
2024-10-30 12:04:38 +05:30
2024-10-30 22:05:16 +05:30
let workerProcess: any;
2024-10-30 21:12:51 +05:30
if (!isProduction) {
workerProcess = fork(workerPath, [], {
execArgv: ['--inspect=5859'],
});
2024-10-30 22:05:16 +05:30
workerProcess.on('message', (message: any) => {
2024-10-30 21:12:51 +05:30
console.log(`Message from worker: ${message}`);
});
2024-10-30 22:05:16 +05:30
workerProcess.on('error', (error: any) => {
2024-10-30 21:12:51 +05:30
console.error(`Error in worker: ${error}`);
});
2024-10-30 22:05:16 +05:30
workerProcess.on('exit', (code: any) => {
2024-10-30 21:12:51 +05:30
console.log(`Worker exited with code: ${code}`);
});
}
2024-10-07 01:37:50 +05:30
2024-05-30 04:49:38 +05:30
app.get('/', function (req, res) {
2024-10-29 04:00:33 +05:30
capture(
'maxun-oss-server-run', {
2024-10-30 22:05:53 +05:30
event: 'server_started',
}
2024-10-29 04:00:33 +05:30
);
2024-09-25 11:07:09 +05:30
return res.send('Maxun server started 🚀');
2024-05-30 04:49:38 +05:30
});
2024-09-24 18:03:26 +05:30
server.listen(SERVER_PORT, async () => {
2024-10-30 22:05:16 +05:30
try {
await connectDB();
await syncDB();
logger.log('info', `Server listening on port ${SERVER_PORT}`);
} catch (error: any) {
logger.log('error', `Failed to connect to the database: ${error.message}`);
process.exit(1); // Exit the process if DB connection fails
}
2024-09-24 18:03:26 +05:30
});
2024-10-07 01:40:13 +05:30
2024-10-30 22:05:16 +05:30
2024-10-07 01:40:13 +05:30
process.on('SIGINT', () => {
console.log('Main app shutting down...');
2024-10-30 22:05:16 +05:30
if (!isProduction) {
workerProcess.kill();
}
2024-10-07 01:40:13 +05:30
process.exit();
});