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-09-24 18:03:26 +05:30
|
|
|
import { connectDB, syncDB } from './db/config';
|
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-05-30 04:49:38 +05:30
|
|
|
|
2024-09-23 23:47:10 +05:30
|
|
|
const csrfProtection = csrf({ cookie: true })
|
|
|
|
|
|
2024-05-30 04:49:38 +05:30
|
|
|
const app = express();
|
2024-09-25 17:01:55 +05:30
|
|
|
app.use(cors({
|
|
|
|
|
origin: 'http://localhost:3000',
|
|
|
|
|
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' }))
|
|
|
|
|
app.use(bodyParser.urlencoded({ extended: true, limit: '10mb', parameterLimit: 10000 }));
|
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(csrfProtection)
|
|
|
|
|
|
2024-05-30 04:50:30 +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-05-30 04:50:30 +05:30
|
|
|
|
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-05-30 04:49:38 +05:30
|
|
|
app.get('/', function (req, res) {
|
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 00:05:22 +05:30
|
|
|
app.get('/csrf-token', (req, res) => {
|
2024-09-25 11:07:09 +05:30
|
|
|
res.json({ csrfToken: req.csrfToken() })
|
|
|
|
|
})
|
2024-09-24 00:05:22 +05:30
|
|
|
|
2024-09-24 18:03:26 +05:30
|
|
|
server.listen(SERVER_PORT, async () => {
|
|
|
|
|
await connectDB();
|
|
|
|
|
await syncDB();
|
|
|
|
|
logger.log('info', `Server listening on port ${SERVER_PORT}`);
|
|
|
|
|
});
|