feat: harden containers and ci
This commit is contained in:
@@ -39,6 +39,11 @@ const config = {
|
||||
rateLimit: {
|
||||
windowMs: optionalNumber(process.env.RATE_LIMIT_WINDOW_MS, 15 * 60 * 1000),
|
||||
max: optionalNumber(process.env.RATE_LIMIT_MAX, 100)
|
||||
},
|
||||
db: {
|
||||
max: optionalNumber(process.env.DB_POOL_MAX, 10),
|
||||
idleTimeoutMillis: optionalNumber(process.env.DB_POOL_IDLE_MS, 30000),
|
||||
connectionTimeoutMillis: optionalNumber(process.env.DB_POOL_CONNECTION_TIMEOUT_MS, 5000)
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -3,7 +3,10 @@ const config = require('../config');
|
||||
|
||||
const pool = new Pool({
|
||||
connectionString: config.databaseUrl,
|
||||
ssl: config.env === 'production' ? { rejectUnauthorized: false } : false
|
||||
ssl: config.env === 'production' ? { rejectUnauthorized: false } : false,
|
||||
max: config.db.max,
|
||||
idleTimeoutMillis: config.db.idleTimeoutMillis,
|
||||
connectionTimeoutMillis: config.db.connectionTimeoutMillis
|
||||
});
|
||||
|
||||
// Test database connection
|
||||
|
||||
@@ -1,6 +1,45 @@
|
||||
const app = require('./server');
|
||||
const config = require('./config');
|
||||
const pool = require('./database/connection');
|
||||
|
||||
app.listen(config.port, config.host, () => {
|
||||
const server = app.listen(config.port, config.host, () => {
|
||||
console.info(`MerchantsOfHope-SupplyANdDemandPortal backend server running on ${config.host}:${config.port}`);
|
||||
});
|
||||
|
||||
let terminating = false;
|
||||
|
||||
async function shutdown(reason, exitCode = 0) {
|
||||
if (terminating) {
|
||||
return;
|
||||
}
|
||||
|
||||
terminating = true;
|
||||
console.info(`Shutting down server (${reason})`);
|
||||
|
||||
try {
|
||||
await new Promise((resolve) => {
|
||||
server.close(resolve);
|
||||
});
|
||||
await pool.end();
|
||||
console.info('Server shutdown complete.');
|
||||
} catch (error) {
|
||||
console.error('Error during shutdown:', error);
|
||||
exitCode = exitCode || 1;
|
||||
} finally {
|
||||
process.exit(exitCode);
|
||||
}
|
||||
}
|
||||
|
||||
['SIGTERM', 'SIGINT'].forEach((signal) => {
|
||||
process.on(signal, () => shutdown(signal));
|
||||
});
|
||||
|
||||
process.on('unhandledRejection', (reason) => {
|
||||
console.error('Unhandled promise rejection:', reason);
|
||||
shutdown('unhandledRejection', 1);
|
||||
});
|
||||
|
||||
process.on('uncaughtException', (error) => {
|
||||
console.error('Uncaught exception:', error);
|
||||
shutdown('uncaughtException', 1);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user