File size: 3,556 Bytes
c7f545b
 
ee7dc99
 
e619481
c7f545b
 
ee7dc99
c7f545b
 
 
 
85e1df4
 
e619481
 
 
 
 
 
 
ee7dc99
 
 
c7f545b
ee7dc99
c7f545b
ee7dc99
c7f545b
ee7dc99
c7f545b
 
 
85e1df4
c7f545b
ee7dc99
 
85e1df4
 
 
 
 
 
 
 
c7f545b
ee7dc99
 
 
 
 
 
 
85e1df4
ee7dc99
83f3cc7
76ba035
 
 
e619481
76ba035
 
 
 
 
 
 
e619481
 
76ba035
 
e619481
76ba035
83f3cc7
 
ee7dc99
 
 
 
 
85e1df4
c7f545b
ee7dc99
c7f545b
 
85e1df4
76ba035
c7f545b
 
 
85e1df4
c7f545b
 
 
 
85e1df4
 
 
 
 
 
ee7dc99
 
85e1df4
ee7dc99
 
c7f545b
85e1df4
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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}`);
});