143 lines
4.7 KiB
JavaScript
143 lines
4.7 KiB
JavaScript
const http = require('http');
|
|
|
|
const PORT = process.env.PORT || 3000;
|
|
|
|
const server = http.createServer((req, res) => {
|
|
// Set CORS headers
|
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
|
|
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
|
|
|
|
// Handle preflight requests
|
|
if (req.method === 'OPTIONS') {
|
|
res.writeHead(200);
|
|
res.end();
|
|
return;
|
|
}
|
|
|
|
// Set content type
|
|
res.setHeader('Content-Type', 'text/html; charset=utf-8');
|
|
|
|
// Handle different routes
|
|
switch (req.url) {
|
|
case '/api/deploy':
|
|
if (req.method === 'POST') {
|
|
let body = '';
|
|
req.on('data', chunk => {
|
|
body += chunk.toString();
|
|
});
|
|
req.on('end', async () => {
|
|
try {
|
|
await deploy();
|
|
console.log('Deployed');
|
|
} catch (error) {
|
|
console.error('Error deploying:', error);
|
|
}
|
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
res.end(JSON.stringify({
|
|
message: 'Echo response',
|
|
received: body,
|
|
timestamp: new Date().toISOString()
|
|
}));
|
|
});
|
|
} else {
|
|
res.writeHead(405, { 'Content-Type': 'application/json' });
|
|
res.end(JSON.stringify({ error: 'Method not allowed. Use POST.' }));
|
|
}
|
|
break;
|
|
|
|
case '/api/deploy-crm-admin':
|
|
if (req.method === 'POST') {
|
|
let body = '';
|
|
req.on('data', chunk => {
|
|
body += chunk.toString();
|
|
});
|
|
req.on('end', async () => {
|
|
// try {
|
|
// await deployCrmAdmin();
|
|
// console.log('Deployed CRM Admin');
|
|
// } catch (error) {
|
|
// console.error('Error deploying:', error);
|
|
// }
|
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
res.end(JSON.stringify({
|
|
message: 'Echo response',
|
|
received: body,
|
|
timestamp: new Date().toISOString()
|
|
}));
|
|
});
|
|
} else {
|
|
res.writeHead(405, { 'Content-Type': 'application/json' });
|
|
res.end(JSON.stringify({ error: 'Method not allowed. Use POST.' }));
|
|
}
|
|
break;
|
|
|
|
default:
|
|
res.writeHead(404, { 'Content-Type': 'application/json' });
|
|
res.end(JSON.stringify({ error: 'Not found' }));
|
|
}
|
|
});
|
|
|
|
server.listen(PORT, () => {
|
|
console.log(`🚀 Server running at http://localhost:${PORT}`);
|
|
console.log(`📱 API endpoints available at http://localhost:${PORT}/api/*`);
|
|
console.log(`⏰ Started at ${new Date().toLocaleString()}`);
|
|
});
|
|
|
|
// Graceful shutdown
|
|
process.on('SIGTERM', () => {
|
|
console.log('SIGTERM received, shutting down gracefully');
|
|
server.close(() => {
|
|
console.log('Process terminated');
|
|
});
|
|
});
|
|
|
|
process.on('SIGINT', () => {
|
|
console.log('SIGINT received, shutting down gracefully');
|
|
server.close(() => {
|
|
console.log('Process terminated');
|
|
});
|
|
});
|
|
|
|
const deploy = () => {
|
|
console.log('Deploying...');
|
|
return new Promise((resolve, reject) => {
|
|
const { exec } = require('child_process');
|
|
exec('cd ../gitea-backend && bash dummy.sh', (error, stdout, stderr) => {
|
|
if (error) {
|
|
console.error(`Error: ${error.message}`);
|
|
reject(error);
|
|
return;
|
|
}
|
|
//if (stderr) {
|
|
//console.error(`Error: ${stderr}`);
|
|
//reject(stderr);
|
|
//return;
|
|
//}
|
|
console.log(`Output: ${stdout}`);
|
|
resolve();
|
|
});
|
|
});
|
|
}
|
|
|
|
const deployCrmAdmin = () => {
|
|
console.log('Deploying...');
|
|
return new Promise((resolve, reject) => {
|
|
const { exec } = require('child_process');
|
|
exec('cd ../next-ava-crm-admin-panel && bash deploy.sh', (error, stdout, stderr) => {
|
|
if (error) {
|
|
console.error(`Error: ${error.message}`);
|
|
reject(error);
|
|
return;
|
|
}
|
|
//if (stderr) {
|
|
//console.error(`Error: ${stderr}`);
|
|
//reject(stderr);
|
|
//return;
|
|
//}
|
|
console.log(`Output: ${stdout}`);
|
|
resolve();
|
|
});
|
|
});
|
|
}
|