const express = require('express'); const session = require('express-session'); const path = require('path'); const multer = require('multer'); const { pool, initializeSchema } = require('./config/db'); const authRoutes = require('./routes/auth'); const adminRoutes = require('./routes/admin'); const userRoutes = require('./routes/user'); const investmentRoutes = require('./routes/investment'); const app = express(); console.log('Starting server...'); // Initialize database schema initializeSchema().then(() => { console.log('Schema initialization completed'); }).catch(err => { console.error('Failed to initialize schema:', err); }); // Set view engine app.set('view engine', 'ejs'); app.set('views', path.join(__dirname, 'public/views')); // Middleware app.use(express.urlencoded({ extended: true })); app.use(express.static(path.join(__dirname, 'public'))); app.use(session({ secret: process.env.SESSION_SECRET || 'your-session-secret', resave: false, saveUninitialized: false })); console.log('Session middleware configured'); // Multer setup for file uploads const storage = multer.diskStorage({ destination: (req, file, cb) => { console.log('Setting up multer destination'); cb(null, 'kyc_uploads/'); }, filename: (req, file, cb) => { console.log(`Uploading file: ${file.originalname}`); cb(null, `${Date.now()}-${file.originalname}`); } }); const upload = multer({ storage }); app.use('/user/kyc', upload.fields([ { name: 'id_doc', maxCount: 1 }, { name: 'utility_doc', maxCount: 1 }, { name: 'selfie_doc', maxCount: 1 }, { name: 'ssn_doc', maxCount: 1 } ])); console.log('Multer middleware configured'); // Root route app.get('/', async (req, res) => { try { console.log('Serving root route'); const [testimonials] = await pool.query('SELECT * FROM testimonials WHERE status = ? LIMIT 3', ['approved']).catch(() => [[]]); const [withdrawals] = await pool.query(` SELECT w.*, u.username FROM withdrawals w JOIN users u ON w.user_id = u.id WHERE w.status = ? ORDER BY w.created_at DESC LIMIT 5 `, ['approved']).catch(() => [[]]); res.render('index', { title: 'HYIP Platform', testimonials: testimonials || [], withdrawals: withdrawals || [] }); } catch (error) { console.error('Error fetching root data:', error); res.render('index', { title: 'HYIP Platform', testimonials: [], withdrawals: [] }); } }); // Routes app.use('/', authRoutes); app.use('/admin', adminRoutes); app.use('/user', userRoutes); app.use('/investment', investmentRoutes); console.log('Routes configured'); // Serve KYC documents (admin access only) app.get('/admin/kyc-document/:id', async (req, res) => { try { console.log(`Serving KYC document ID: ${req.params.id}`); const [doc] = await pool.query('SELECT file_path FROM kyc_documents WHERE id = ?', [req.params.id]); if (!doc.length) return res.status(404).send('Document not found'); res.sendFile(path.resolve(doc[0].file_path)); } catch (error) { console.error('Error serving KYC document:', error); res.status(500).send('Error retrieving document'); } }); // Health check endpoint app.get('/health', (req, res) => { console.log('Health check requested'); res.status(200).send('OK'); }); // Error handling app.use((err, req, res, next) => { console.error('Server error:', err.stack); res.status(500).send('Something went wrong!'); }); const PORT = process.env.PORT || 7860; app.listen(PORT, () => { console.log(`Server running on port ${PORT}`); });