Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
72 lines (61 loc) · 2.23 KB
/
Copy pathapp.js
File metadata and controls
72 lines (61 loc) · 2.23 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
const express = require('express');
const fs = require('fs');
const path = require('path');
const os = require('os');
function createServer() {
const app = express();
const colorEnv = process.env.COLOR;
const backgroundColor = colorEnv || 'blue'; // Default to blue if not set
const logsDir = path.join(__dirname, 'logs');
// Ensure logs directory exists
if (!fs.existsSync(logsDir)) {
fs.mkdirSync(logsDir, { recursive: true });
}
const logFilePath = path.join(logsDir, 'app.log');
const logStream = fs.createWriteStream(logFilePath, { flags: 'a' });
const logMessage = (message) => {
const timestamp = new Date().toISOString();
const formatted = `[${timestamp}] ${message}`;
console.log(formatted);
logStream.write(formatted + '\n');
};
// App startup logs
logMessage('🚀 Starting Color Display Application...');
logMessage(`🌐 Hostname: ${os.hostname()}`);
logMessage(`🎨 Color set to: ${backgroundColor}`);
logMessage(`📁 Serving HTML from: ${path.join(__dirname, 'index.html')}`);
logMessage(`📄 Logging to file: ${logFilePath}`);
// Route
app.get('/', (req, res) => {
const userAgent = req.headers['user-agent'] || 'Unknown';
const ip = req.headers['x-forwarded-for'] || req.socket.remoteAddress || 'Unknown';
const method = req.method;
const url = req.originalUrl;
logMessage(`📥 Request: ${method} ${url} | IP: ${ip} | UA: ${userAgent}`);
const htmlPath = path.join(__dirname, 'index.html');
fs.readFile(htmlPath, 'utf8', (err, data) => {
if (err) {
const errorMsg = `❌ Error reading ${htmlPath}: ${err.message}`;
logMessage(errorMsg);
return res.status(500).send(errorMsg);
}
const output = data.replace(/{{COLOR}}/g, backgroundColor);
res.send(output);
});
});
return { app, logStream };
}
module.exports = { createServer };
// Run server if executed directly
if (require.main === module) {
const { app } = createServer();
const PORT = process.env.PORT || 8080;
const server = app.listen(PORT, () => {
console.log(`✅ Server running at http://localhost:${PORT}`);
});
process.on('SIGTERM', () => {
server.close(() => {
console.log('🛑 Server terminated gracefully');
});
});
}