|
|
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...'); |
|
|
|
|
|
|
|
|
initializeSchema().then(() => { |
|
|
console.log('Schema initialization completed'); |
|
|
}).catch(err => { |
|
|
console.error('Failed to initialize schema:', err); |
|
|
}); |
|
|
|
|
|
|
|
|
app.set('view engine', 'ejs'); |
|
|
app.set('views', path.join(__dirname, 'public/views')); |
|
|
|
|
|
|
|
|
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'); |
|
|
|
|
|
|
|
|
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'); |
|
|
|
|
|
|
|
|
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: [] }); |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
app.use('/', authRoutes); |
|
|
app.use('/admin', adminRoutes); |
|
|
app.use('/user', userRoutes); |
|
|
app.use('/investment', investmentRoutes); |
|
|
console.log('Routes configured'); |
|
|
|
|
|
|
|
|
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'); |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
app.get('/health', (req, res) => { |
|
|
console.log('Health check requested'); |
|
|
res.status(200).send('OK'); |
|
|
}); |
|
|
|
|
|
|
|
|
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}`); |
|
|
}); |