-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
133 lines (114 loc) · 4.69 KB
/
Copy pathserver.js
File metadata and controls
133 lines (114 loc) · 4.69 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// =====================================================
// ProView Cadence - Dynamic Backend Server
// Node.js + Express Server for Real-time Updates
// =====================================================
const express = require('express');
const fs = require('fs').promises;
const path = require('path');
const cors = require('cors');
const app = express();
const PORT = 8080;
const DATA_FILE = path.join(__dirname, 'data', 'data.json');
// Middleware
app.use(cors());
app.use(express.json({ limit: '50mb' }));
app.use(express.static(__dirname));
// =====================================================
// API Routes
// =====================================================
// GET - Fetch current data
app.get('/api/data', async (req, res) => {
try {
const data = await fs.readFile(DATA_FILE, 'utf8');
res.json(JSON.parse(data));
} catch (error) {
console.error('Error reading data:', error);
res.status(500).json({ error: 'Failed to read data' });
}
});
// POST - Save updated data
app.post('/api/data', async (req, res) => {
try {
const newData = req.body;
// Validate JSON
if (!newData || typeof newData !== 'object') {
return res.status(400).json({ error: 'Invalid data format' });
}
// Backup old data
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const backupFile = path.join(__dirname, 'data', `data-backup-${timestamp}.json`);
try {
const oldData = await fs.readFile(DATA_FILE, 'utf8');
await fs.writeFile(backupFile, oldData);
console.log(`✅ Backup created: ${backupFile}`);
} catch (backupError) {
console.warn('Warning: Could not create backup:', backupError.message);
}
// Write new data
await fs.writeFile(DATA_FILE, JSON.stringify(newData, null, 2));
console.log('✅ Data updated successfully!');
res.json({
success: true,
message: 'Data saved successfully!',
timestamp: new Date().toISOString()
});
} catch (error) {
console.error('Error saving data:', error);
res.status(500).json({ error: 'Failed to save data' });
}
});
// GET - List backups
app.get('/api/backups', async (req, res) => {
try {
const dataDir = path.join(__dirname, 'data');
const files = await fs.readdir(dataDir);
const backups = files
.filter(file => file.startsWith('data-backup-'))
.sort()
.reverse();
res.json({ backups });
} catch (error) {
console.error('Error listing backups:', error);
res.status(500).json({ error: 'Failed to list backups' });
}
});
// POST - Restore from backup
app.post('/api/restore', async (req, res) => {
try {
const { filename } = req.body;
const backupFile = path.join(__dirname, 'data', filename);
// Read backup
const backupData = await fs.readFile(backupFile, 'utf8');
// Restore
await fs.writeFile(DATA_FILE, backupData);
console.log(`✅ Restored from backup: ${filename}`);
res.json({
success: true,
message: 'Data restored successfully!'
});
} catch (error) {
console.error('Error restoring backup:', error);
res.status(500).json({ error: 'Failed to restore backup' });
}
});
// =====================================================
// Start Server
// =====================================================
app.listen(PORT, () => {
console.log('');
console.log('═══════════════════════════════════════════════════');
console.log('🚀 ProView Cadence Server Running!');
console.log('═══════════════════════════════════════════════════');
console.log(`📍 Main Website: http://localhost:${PORT}`);
console.log(`⚙️ Admin Panel: http://localhost:${PORT}/admin.html`);
console.log(`📡 API Endpoint: http://localhost:${PORT}/api/data`);
console.log('═══════════════════════════════════════════════════');
console.log('✨ Changes made in Admin Panel will update instantly!');
console.log('🔄 Press Ctrl+C to stop the server');
console.log('');
});
// Graceful shutdown
process.on('SIGINT', () => {
console.log('\n\n✅ Server stopped gracefully');
process.exit(0);
});