const express = require('express'); const http = require('http'); const cors = require('cors'); const helmet = require('helmet'); const morgan = require('morgan'); const dotenv = require('dotenv'); const { app: appConfig } = require('./src/config/config'); const db = require('./src/models'); const socketio = require('socket.io'); const path = require('path'); // Load env dotenv.config(); const app = express(); const server = http.createServer(app); const io = socketio(server, { cors: { origin: appConfig.clientUrl, methods: ['GET', 'POST'], credentials: true, }, }); require('./src/sockets')(io); // Patient call scheduler const { schedulePatientCalls } = require('./src/services/patientCallScheduler'); const PATIENT_CALL_WEBHOOK_URL = process.env.PATIENT_CALL_WEBHOOK_URL || 'http://localhost:3000/api/v1/calls/webhook'; // Set your real webhook URL here schedulePatientCalls(PATIENT_CALL_WEBHOOK_URL); // Middleware app.use(cors({ origin: appConfig.clientUrl, credentials: true })); app.use(helmet()); app.use(morgan('dev')); app.use(express.json()); app.use(express.urlencoded({ extended: false })); // Serve static files from public app.use(express.static(path.join(__dirname, 'public'))); // Attach io to app for use in routes/controllers app.set('io', io); // Health check app.get('/api/health', (req, res) => res.json({ status: 'ok' })); // Mount all API routes app.use('/api/v1', require('./src/routes')); // Error handler app.use((err, req, res, next) => { console.error(err); res.status(err.status || 500).json({ error: err.message || 'Internal Server Error' }); }); // Start server const PORT = appConfig.port; db.sequelize.sync().then(() => { server.listen(PORT, () => { console.log(`Server running on port ${PORT}`); }); });