Merge pull request #495 from getmaxun/connect-session

feat: migrate session storage to postgres
This commit is contained in:
Karishma Shukla
2025-03-24 00:25:41 +05:30
committed by GitHub
2 changed files with 32 additions and 2 deletions

View File

@@ -28,6 +28,7 @@
"body-parser": "^1.20.3",
"buffer": "^6.0.3",
"bullmq": "^5.12.15",
"connect-pg-simple": "^10.0.0",
"connect-redis": "^8.0.1",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
@@ -103,6 +104,7 @@
]
},
"devDependencies": {
"@types/connect-pg-simple": "^7.0.3",
"@types/cookie-parser": "^1.4.7",
"@types/express": "^4.17.13",
"@types/js-cookie": "^3.0.6",

View File

@@ -18,6 +18,8 @@ import { fork } from 'child_process';
import { capture } from "./utils/analytics";
import swaggerUi from 'swagger-ui-express';
import swaggerSpec from './swagger/config';
import connectPgSimple from 'connect-pg-simple';
import pg from 'pg';
import session from 'express-session';
@@ -30,13 +32,31 @@ app.use(cors({
}));
app.use(express.json());
const { Pool } = pg;
const pool = new Pool({
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_NAME,
password: process.env.DB_PASSWORD,
port: process.env.DB_PORT ? parseInt(process.env.DB_PORT, 10) : undefined,
});
const PgSession = connectPgSimple(session);
app.use(
session({
store: new PgSession({
pool: pool,
tableName: 'session',
createTableIfMissing: true,
}),
secret: 'mx-session',
resave: false, // Do not resave the session if it hasn't changed
saveUninitialized: true, // Save new sessions
cookie: { secure: false }, // Set to true if using HTTPS
cookie: {
secure: false, // Set to true if using HTTPS
maxAge: 24 * 60 * 60 * 1000, // 1-day session expiration
},
})
);
@@ -159,7 +179,15 @@ process.on('SIGINT', async () => {
} catch (error: any) {
console.error('Error updating runs:', error);
}
try {
console.log('Closing PostgreSQL connection pool...');
await pool.end();
console.log('PostgreSQL connection pool closed');
} catch (error) {
console.error('Error closing PostgreSQL connection pool:', error);
}
if (!isProduction) {
if (workerProcess) workerProcess.kill();
if (recordingWorkerProcess) recordingWorkerProcess.kill();