"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const path_1 = __importDefault(require("path"));
const cors_1 = __importDefault(require("cors"));
const dotenv_1 = __importDefault(require("dotenv"));
dotenv_1.default.config();
const passport_1 = __importDefault(require("passport"));
const swagger_ui_express_1 = __importDefault(require("swagger-ui-express"));
const swagger_output_json_1 = __importDefault(require("./swagger-output.json")); // Import the generated JSON
require("./config/proj4-config");
const database_1 = require("./config/database");
// routes
const mongodb_routes_1 = __importDefault(require("./routes/mongodb/mongodb-routes"));
const county_router_1 = __importDefault(require("./routes/county-router/county-router"));
const admin_router_1 = __importDefault(require("./routes/admin-router/admin-router"));
const all_controller_1 = __importDefault(require("./controllers/all-controller"));
/**
* Initializes and configures the Express application.
* Sets up middleware, routes, and error handling for the application.
*
* @namespace app
*/
// Initialize Express app
/**
* @type {express.Application}
*/
const app = (0, express_1.default)();
// Middleware for parsing JSON and URL-encoded bodies
app.use(express_1.default.json());
app.use(express_1.default.urlencoded({ extended: true }));
// Enable Cross-Origin Resource Sharing (CORS)
/**
* Enables Cross-Origin Resource Sharing (CORS).
* @memberof app
*/
app.use((0, cors_1.default)());
// Set EJS as the templating engine
/**
* Configures the templating engine for the application.
* @memberof app
*/
app.set('view engine', 'ejs');
app.set('views', path_1.default.join(__dirname, 'views')); // Set the views directory
// Initialize Passport for authentication
/**
* Initializes Passport middleware for authentication.
* @memberof app
*/
app.use(passport_1.default.initialize());
// Define the application port
/**
* The port on which the server will run.
* @type {number|string}
*/
const PORT = process.env.PORT || 5000;
// Swagger API documentation route
/**
* Serves Swagger API documentation.
* @memberof app
*/
app.use('/api-docs', swagger_ui_express_1.default.serve, swagger_ui_express_1.default.setup(swagger_output_json_1.default));
// Routes for MongoDB operations
/**
* Routes for MongoDB operations.
* @memberof app
*/
app.use('/api/', mongodb_routes_1.default);
// Routes for county-specific operations
/**
* Routes for county-specific operations.
* @memberof app
*/
app.use('/api/v2/', county_router_1.default);
// Routes for admin operations
/**
* Routes for administrative operations.
* @memberof app
*/
app.use('/', admin_router_1.default);
// Routes for all controller
/**
* Routes for all controller.
* @memberof app
*/
app.use('/controller/', all_controller_1.default);
// Default route for the root URL
/**
* Default route for the root URL.
* @memberof app
* @param {express.Request} req - The request object.
* @param {express.Response} res - The response object.
* @returns {void}
*/
app.get('/', (req, res) => {
res.send('TRYING TO ACCESS??? THINK AGAIN HEHEHEHE!');
});
// Start the server
/**
* Starts the Express server.
* @memberof app
* @returns {void}
*/
app.listen(PORT, () => {
console.log(`Server is running on port:${PORT}`);
console.log('Swagger docs available at http://localhost:5000/api-docs');
});
// Graceful shutdown for process termination signals
/**
* Handles cleanup operations on process termination.
* Closes the MongoDB connection and exits the process.
*
* @memberof app
* @async
* @function cleanup
* @returns {Promise<void>}
*/
const cleanup = () => __awaiter(void 0, void 0, void 0, function* () {
console.log('Cleaning up before shutdown...');
yield (0, database_1.closeClient)(); // Close the MongoDB connection here
process.exit(0);
});
// Listen for termination signals and invoke cleanup
/**
* Listens for termination signals and triggers cleanup.
* @memberof app
*/
process.on('SIGTERM', () => {
console.log('SIGTERM signal received: closing MongoDB connection');
cleanup();
});
process.on('SIGINT', () => {
console.log('SIGINT signal received: closing MongoDB connection');
cleanup();
});