v_1
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
const express = require('express');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const cors = require('cors');
|
||||
|
||||
const app = express();
|
||||
const PORT = 8000;
|
||||
|
||||
// Middleware
|
||||
app.use(cors());
|
||||
app.use(express.json({ limit: '50mb' })); // Increased limit for base64 images
|
||||
app.use(express.static(__dirname)); // Serve static files from current directory
|
||||
|
||||
const DATA_FILE = path.join(__dirname, 'contacts_data.json');
|
||||
const CREDENTIALS_FILE = path.join(__dirname, 'credentials.json');
|
||||
|
||||
// Initialize data file if it doesn't exist
|
||||
if (!fs.existsSync(DATA_FILE)) {
|
||||
const initialData = {
|
||||
contacts: [
|
||||
{ id: 1, name: 'John Smith', unit: 'Marketing', phone: '+351 123 456 789', mobile: '+351 987 654 321', email: 'john.smith@company.com', department: 'IT', locations: ['Martinhal Oriente'], photo: 'https://i.pravatar.cc/150?img=12' },
|
||||
{ id: 2, name: 'Maria Santos', unit: 'Finance', phone: '+351 123 456 790', mobile: '+351 987 654 322', email: 'maria.santos@company.com', department: 'Accounting', locations: ['Martinhal Lisbon', 'Martinhal Quinta'], photo: 'https://i.pravatar.cc/150?img=5' },
|
||||
{ id: 3, name: 'Pedro Costa', unit: 'Operations', phone: '+351 123 456 791', mobile: '+351 987 654 323', email: 'pedro.costa@company.com', department: 'Maintenance', locations: ['Martinhal Sagres'], photo: 'https://i.pravatar.cc/150?img=33' }
|
||||
],
|
||||
departments: ['IT', 'Accounting', 'Board', 'Housekeeping', 'Maintenance'],
|
||||
locations: ['Martinhal Oriente', 'Martinhal Lisbon', 'Martinhal Quinta', 'Martinhal Sagres']
|
||||
};
|
||||
fs.writeFileSync(DATA_FILE, JSON.stringify(initialData, null, 2));
|
||||
console.log('Created initial data file:', DATA_FILE);
|
||||
}
|
||||
|
||||
// Check if credentials.json exists
|
||||
if (!fs.existsSync(CREDENTIALS_FILE)) {
|
||||
console.error('WARNING: credentials.json not found!');
|
||||
console.error('Please make sure credentials.json is in the same directory as server.js');
|
||||
}
|
||||
|
||||
// Get credentials (for debugging)
|
||||
app.get('/api/credentials', (req, res) => {
|
||||
try {
|
||||
if (!fs.existsSync(CREDENTIALS_FILE)) {
|
||||
return res.status(404).json({ error: 'credentials.json not found' });
|
||||
}
|
||||
const credentials = JSON.parse(fs.readFileSync(CREDENTIALS_FILE, 'utf8'));
|
||||
res.json(credentials);
|
||||
} catch (error) {
|
||||
console.error('Error reading credentials:', error);
|
||||
res.status(500).json({ error: 'Failed to read credentials' });
|
||||
}
|
||||
});
|
||||
|
||||
// Get all data
|
||||
app.get('/api/data', (req, res) => {
|
||||
try {
|
||||
const data = JSON.parse(fs.readFileSync(DATA_FILE, 'utf8'));
|
||||
res.json(data);
|
||||
} catch (error) {
|
||||
console.error('Error reading data:', error);
|
||||
res.status(500).json({ error: 'Failed to read data' });
|
||||
}
|
||||
});
|
||||
|
||||
// Save all data
|
||||
app.post('/api/data', (req, res) => {
|
||||
try {
|
||||
fs.writeFileSync(DATA_FILE, JSON.stringify(req.body, null, 2));
|
||||
res.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('Error saving data:', error);
|
||||
res.status(500).json({ error: 'Failed to save data' });
|
||||
}
|
||||
});
|
||||
|
||||
// Backup endpoint (optional - creates timestamped backup)
|
||||
app.post('/api/backup', (req, res) => {
|
||||
try {
|
||||
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
||||
const backupFile = path.join(__dirname, `backup_${timestamp}.json`);
|
||||
const data = fs.readFileSync(DATA_FILE, 'utf8');
|
||||
fs.writeFileSync(backupFile, data);
|
||||
res.json({ success: true, file: backupFile });
|
||||
} catch (error) {
|
||||
console.error('Error creating backup:', error);
|
||||
res.status(500).json({ error: 'Failed to create backup' });
|
||||
}
|
||||
});
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log('=================================');
|
||||
console.log(`Server running at http://localhost:${PORT}`);
|
||||
console.log(`Data file: ${DATA_FILE}`);
|
||||
console.log(`Credentials file: ${CREDENTIALS_FILE}`);
|
||||
console.log(`Credentials exists: ${fs.existsSync(CREDENTIALS_FILE)}`);
|
||||
console.log('=================================');
|
||||
});
|
||||
Reference in New Issue
Block a user