feat: stabilize API startup and lifecycle management

- Refactor server initialization to separate concerns and improve error handling.
- Implement centralized environment validation using Zod.
- Introduce database, Redis, and queue lifecycle management.
- Add health check endpoints for liveness and readiness.
- Enhance error handling middleware for better response structure.
- Implement rate limiting for API endpoints.
- Add request ID middleware for traceability.
- Create Sequelize CLI configuration and baseline migration for schema management.
- Establish CI workflow with Gitea for testing and syntax checks.
- Document foundational changes and migration strategy in PHASE_0_FOUNDATION_STABILIZATION.md.
- Add Docker Compose configuration for local development and testing.
- Implement unit and integration tests for critical functionality.
This commit is contained in:
Sathira Sri Sathara
2026-09-03 13:33:26 +05:30
parent 624fce31c5
commit 267e80e2ec
40 changed files with 1682 additions and 261 deletions
+73 -12
View File
@@ -1,20 +1,81 @@
/**
* Copyright (c) 2026 Niolla
* All rights reserved.
*
* This source code is proprietary and confidential.
* Unauthorized copying, modification, distribution, or use
* of this file, via any medium, is strictly prohibited.
*/
// server.js
require("dotenv").config();
const app = require("./app");
const { validateEnvironment } = require("./app/config/env.config");
const PORT = process.env.PORT || 3070;
const bootstrap = async () => {
const env = validateEnvironment();
const { initializeDatabase, closeDatabase } = require("./app/config/database.lifecycle");
const { initializeRedis, closeRedis } = require("./app/config/redis.lifecycle");
try {
await initializeDatabase();
console.log("Database connection ready.");
await initializeRedis();
console.log("Redis connection ready.");
} catch (error) {
await Promise.allSettled([closeRedis(), closeDatabase()]);
throw error;
}
// Start the server
app.listen(PORT, "0.0.0.0", () => {
console.log(`Server running on http://localhost:${PORT}`);
});
const { closeQueues } = require("./app/config/queue.lifecycle");
const startAllCrons = require("./cron");
const app = require("./app");
let stopCrons = async () => {};
if (env.RUN_CRON) stopCrons = startAllCrons();
const server = await new Promise((resolve, reject) => {
const listener = app.listen(env.PORT, "0.0.0.0", () => resolve(listener));
listener.once("error", reject);
});
console.log(`ZUMRI API listening on port ${env.PORT}.`);
let shuttingDown = false;
const shutdown = async (reason, exitCode = 0) => {
if (shuttingDown) return;
shuttingDown = true;
console.log(`Shutting down (${reason}).`);
const forceTimer = setTimeout(() => {
console.error("Graceful shutdown timed out.");
process.exit(1);
}, env.SHUTDOWN_TIMEOUT_MS);
forceTimer.unref();
try {
await new Promise((resolve) => server.close(resolve));
await stopCrons();
await closeQueues();
await closeRedis();
await closeDatabase();
clearTimeout(forceTimer);
process.exit(exitCode);
} catch (error) {
console.error("Shutdown failed", { name: error.name, message: error.message });
process.exit(1);
}
};
process.once("SIGTERM", () => shutdown("SIGTERM"));
process.once("SIGINT", () => shutdown("SIGINT"));
process.once("unhandledRejection", (reason) => {
const error = reason instanceof Error ? reason : new Error("Unhandled rejection");
console.error("Unhandled rejection", { name: error.name, message: error.message });
shutdown("unhandledRejection", 1);
});
process.once("uncaughtException", (error) => {
console.error("Uncaught exception", { name: error.name, message: error.message });
shutdown("uncaughtException", 1);
});
return { server, shutdown };
};
if (require.main === module) {
bootstrap().catch((error) => {
console.error("API startup failed", { name: error.name, message: error.message });
process.exitCode = 1;
});
}
module.exports = { bootstrap };