// src/index.js - Main Entry Point require('dotenv').config(); // Ensure env variables are loaded first const logger = require('./logger'); const { connectDB } = require('./database'); const { connectWhatsApp } = require('./connection'); /** * Starts the application by connecting to the database * and then establishing the WhatsApp connection. */ async function start() { logger.info("==================================="); logger.info(" Starting WhatsApp Bot "); logger.info("==================================="); try { await connectDB(); await connectWhatsApp(); } catch (error) { logger.fatal({ err: error }, "Critical error during application startup sequence."); process.exit(1); // Exit if essential connections fail } } // Execute startup sequence start().catch(err => { // This catch is unlikely to be hit if errors in start() are handled // but provides a final safety net. logger.fatal({ err: err }, "FATAL UNHANDLED ERROR during startup!"); process.exit(1); }); // Optional: Graceful shutdown handling process.on('SIGINT', () => { logger.warn("Received SIGINT. Shutting down gracefully..."); // Add cleanup logic here if needed (e.g., close DB connection) process.exit(0); }); process.on('SIGTERM', () => { logger.warn("Received SIGTERM. Shutting down gracefully..."); // Add cleanup logic here process.exit(0); });