-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
78 lines (65 loc) · 2.42 KB
/
server.js
File metadata and controls
78 lines (65 loc) · 2.42 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
const express = require('express');
const path = require('path');
const fs = require('fs');
require('dotenv').config();
const app = express();
const PORT = process.env.PORT;
const IP_ADDRESS = process.env.IP_ADDRESS;
app.use(express.static(path.join(__dirname, 'public')));
const PROFILE_COLORS_FILE = path.join(__dirname, 'profile-colors.json');
const DEFAULT_PROFILE_COLORS = {
default: 'blue',
colors: [
{ key: 'blue', label: 'Azul', hex: '#3b82f6' },
{ key: 'purple', label: 'Roxo', hex: '#4a24a3' },
{ key: 'yellow', label: 'Amarelo', hex: '#f59e0b' },
{ key: 'red', label: 'Vermelho', hex: '#ef4444' }
]
};
function sanitizeProfileColorsConfig(raw) {
if (!raw || typeof raw !== 'object') return DEFAULT_PROFILE_COLORS;
const inputColors = Array.isArray(raw.colors) ? raw.colors : [];
const normalizedColors = inputColors
.map((item) => {
const key = String(item?.key || '').trim().toLowerCase().replace(/[^a-z0-9_-]/g, '');
const hex = String(item?.hex || '').trim();
const label = String(item?.label || '').trim();
if (!key || !/^#([0-9a-fA-F]{6})$/.test(hex)) return null;
return { key, hex, label: label || hex };
})
.filter(Boolean);
if (!normalizedColors.length) return DEFAULT_PROFILE_COLORS;
const defaultKeyRaw = String(raw.default || '').trim().toLowerCase();
const defaultKey = normalizedColors.some((color) => color.key === defaultKeyRaw)
? defaultKeyRaw
: normalizedColors[0].key;
return { default: defaultKey, colors: normalizedColors };
}
function loadProfileColorsConfig() {
try {
const raw = fs.readFileSync(PROFILE_COLORS_FILE, 'utf8');
const parsed = JSON.parse(raw);
return sanitizeProfileColorsConfig(parsed);
} catch {
return DEFAULT_PROFILE_COLORS;
}
}
app.get('/config.js', (req, res) => {
res.set('Cache-Control', 'no-store');
res.type('application/javascript');
const profileColors = loadProfileColorsConfig();
res.send(
`window.__API_URL__ = ${JSON.stringify(process.env.API_URL || '')};\n` +
`window.__MF_PROFILE_COLORS = ${JSON.stringify(profileColors)};`
);
});
app.get('/config.json', (req, res) => {
res.set('Cache-Control', 'no-store');
res.json({ apiUrl: process.env.API_URL || '' });
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.listen(PORT, IP_ADDRESS, () => {
console.log(`Frontend rodando em http://${IP_ADDRESS}:${PORT}`);
});