-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
121 lines (108 loc) · 4.03 KB
/
server.js
File metadata and controls
121 lines (108 loc) · 4.03 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
// TheSaaSsin Operator Server — pure Node, no dependencies
const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = 4000;
const PUBLIC = path.join(__dirname, 'public');
const DATA = path.join(__dirname, 'data');
const MIME = {
'.html': 'text/html', '.css': 'text/css',
'.js': 'text/javascript', '.json': 'application/json',
'.mp4': 'video/mp4', '.ico': 'image/x-icon'
};
function readJSON(file) {
try { return JSON.parse(fs.readFileSync(path.join(DATA, file), 'utf8')); }
catch { return {}; }
}
function writeJSON(file, data) {
fs.writeFileSync(path.join(DATA, file), JSON.stringify(data, null, 2));
}
function body(req) {
return new Promise(res => {
let d = '';
req.on('data', c => d += c);
req.on('end', () => { try { res(JSON.parse(d)); } catch { res({}); } });
});
}
const ROUTES = {
'GET /api/clients': (_, res) => { res.end(JSON.stringify(readJSON('clients.json'))); },
'GET /api/leads': (_, res) => { res.end(JSON.stringify(readJSON('leads.json'))); },
'GET /api/outreach': (_, res) => { res.end(JSON.stringify(readJSON('outreach_queue.json'))); },
'POST /api/clients': async (req, res) => {
const data = await body(req);
const db = readJSON('clients.json');
data.id = Date.now();
data.createdAt = new Date().toISOString();
data.lastUpdated = new Date().toISOString();
data.status = 'active';
data.systems = data.systems || { landingPage: '', outreach: [], crm: {} };
db.clients.push(data);
writeJSON('clients.json', db);
res.end(JSON.stringify({ ok: true, client: data }));
},
'PATCH /api/clients': async (req, res) => {
const data = await body(req);
const db = readJSON('clients.json');
let updated = null;
db.clients = db.clients.map(c => {
if (c.id === data.id) {
updated = { ...c, ...data, lastUpdated: new Date().toISOString() };
return updated;
}
return c;
});
writeJSON('clients.json', db);
res.end(JSON.stringify({ ok: true, client: updated }));
},
'POST /api/leads': async (req, res) => {
const data = await body(req);
const db = readJSON('leads.json');
data.id = Date.now();
data.createdAt = new Date().toISOString();
data.status = data.status || 'new';
data.score = data.score || 0;
db.leads.push(data);
writeJSON('leads.json', db);
res.end(JSON.stringify({ ok: true, lead: data }));
},
'PATCH /api/leads': async (req, res) => {
const data = await body(req);
const db = readJSON('leads.json');
db.leads = db.leads.map(l => l.id === data.id ? { ...l, ...data } : l);
writeJSON('leads.json', db);
res.end(JSON.stringify({ ok: true }));
},
'POST /api/outreach': async (req, res) => {
const data = await body(req);
const db = readJSON('outreach_queue.json');
data.id = Date.now();
data.status = 'pending';
data.createdAt = new Date().toISOString();
db.queue.push(data);
writeJSON('outreach_queue.json', db);
res.end(JSON.stringify({ ok: true }));
},
'PATCH /api/outreach': async (req, res) => {
const data = await body(req);
const db = readJSON('outreach_queue.json');
db.queue = db.queue.map(q => q.id === data.id ? { ...q, status: data.status } : q);
writeJSON('outreach_queue.json', db);
res.end(JSON.stringify({ ok: true }));
}
};
http.createServer(async (req, res) => {
const key = `${req.method} ${req.url.split('?')[0]}`;
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Content-Type', 'application/json');
if (ROUTES[key]) return ROUTES[key](req, res);
// Static files
let filePath = req.url === '/' ? '/operator.html' : req.url;
filePath = path.join(PUBLIC, filePath);
const ext = path.extname(filePath);
res.setHeader('Content-Type', MIME[ext] || 'text/plain');
fs.readFile(filePath, (err, data) => {
if (err) { res.writeHead(404); res.end('Not found'); return; }
res.writeHead(200);
res.end(data);
});
}).listen(PORT, () => console.log(`Operator → http://localhost:${PORT}`));