-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
339 lines (305 loc) · 10.7 KB
/
Copy pathserver.js
File metadata and controls
339 lines (305 loc) · 10.7 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Clean single-server implementation: REST API + WebSocket relay + file persistence
const path = require('path');
const fs = require('fs');
const express = require('express');
const http = require('http');
const { WebSocketServer } = require('ws');
const { v4: uuidv4 } = require('uuid');
const { Pool } = require('pg');
const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 3000;
const DATA_DIR = path.join(__dirname, 'data');
const ORDERS_FILE = path.join(DATA_DIR, 'orders.json');
const REALTIME_TOKEN = process.env.REALTIME_TOKEN || '';
// PostgreSQL connection config (read from env vars or DATABASE_URL)
const POSTGRES_URL = process.env.DATABASE_URL || null;
let pgPool = null;
let usingPostgres = false;
async function tryInitPostgres() {
try {
if (!POSTGRES_URL) {
console.log('Postgres not configured; will use file-based fallback');
return;
}
pgPool = new Pool({ connectionString: POSTGRES_URL });
const client = await pgPool.connect();
await client.query('SELECT 1'); // Test connection
client.release();
await ensureOrdersTablePostgres();
await ensureProductsTablePostgres();
usingPostgres = true;
console.log('Connected to PostgreSQL, using SQL persistence');
} catch (err) {
console.warn('Postgres init failed, falling back to file persistence:', err.message || err);
pgPool = null;
usingPostgres = false;
}
}
async function ensureOrdersTablePostgres() {
if (!pgPool) return;
const create = `
CREATE TABLE IF NOT EXISTS orders (
id VARCHAR(64) PRIMARY KEY,
data JSON NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
`;
await pgPool.query(create);
}
// Add products table and endpoints
async function ensureProductsTablePostgres() {
if (!pgPool) return;
const create = `
CREATE TABLE IF NOT EXISTS products (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
description TEXT,
price NUMERIC(10, 2) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
`;
await pgPool.query(create);
}
// Ensure products table exists
ensureProductsTablePostgres();
// health endpoint will be added after app is initialized
function ensureDataDir() {
if (!fs.existsSync(DATA_DIR)) fs.mkdirSync(DATA_DIR, { recursive: true });
}
async function loadOrders() {
if (usingPostgres && pgPool) {
const res = await pgPool.query('SELECT id, data FROM orders ORDER BY created_at ASC');
return res.rows.map(r => r.data);
}
try {
ensureDataDir();
if (!fs.existsSync(ORDERS_FILE)) {
fs.writeFileSync(ORDERS_FILE, JSON.stringify([]), 'utf8');
return [];
}
const raw = fs.readFileSync(ORDERS_FILE, 'utf8');
return JSON.parse(raw || '[]');
} catch (err) {
console.error('Failed to load orders:', err);
return [];
}
}
async function saveOrders(orders) {
if (usingPostgres && pgPool) {
const client = await pgPool.connect();
try {
await client.query('BEGIN');
await client.query('DELETE FROM orders');
for (const o of orders) {
const id = String(o.id || uuidv4());
await client.query('INSERT INTO orders (id, data) VALUES ($1, $2)', [id, o]);
}
await client.query('COMMIT');
} catch (err) {
await client.query('ROLLBACK');
console.error('Failed to save orders:', err);
} finally {
client.release();
}
return;
}
try {
ensureDataDir();
fs.writeFileSync(ORDERS_FILE, JSON.stringify(orders, null, 2), 'utf8');
} catch (err) {
console.error('Failed to save orders:', err);
}
}
const app = express();
app.use(express.json({ limit: '1mb' }));
// Simple token middleware for both REST and WebSocket query param
function checkTokenMiddleware(req, res, next) {
if (!REALTIME_TOKEN) return next();
const token = req.header('x-realtime-token') || req.query.token || '';
if (token !== REALTIME_TOKEN) {
return res.status(401).json({ error: 'invalid token' });
}
next();
}
app.use('/api', checkTokenMiddleware);
// CORS for convenience when serving pages from other host during development
app.use((req, res, next) => {
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, x-realtime-token');
if (req.method === 'OPTIONS') return res.sendStatus(204);
next();
});
// health endpoint
app.get('/health', (req, res) => {
res.json({ ok: true, mysql: usingPostgres });
});
// REST API
app.get('/api/orders', async (req, res) => {
try {
const orders = await loadOrders();
res.json(orders);
} catch (err) {
res.status(500).json({ error: 'failed to load orders' });
}
});
app.get('/api/orders/:id', async (req, res) => {
try {
const orders = await loadOrders();
const order = orders.find((o) => String(o.id) === String(req.params.id));
if (!order) return res.status(404).json({ error: 'not found' });
res.json(order);
} catch (err) {
res.status(500).json({ error: 'failed to load order' });
}
});
app.post('/api/orders', async (req, res) => {
try {
const incoming = req.body || {};
const id = incoming.id || uuidv4();
const now = new Date().toISOString();
const order = Object.assign({}, incoming, { id, createdAt: now, updatedAt: now });
const orders = await loadOrders();
orders.push(order);
await saveOrders(orders);
broadcast({ type: 'orders:created', order, orders });
res.status(201).json(order);
} catch (err) {
console.error('post order failed', err);
res.status(500).json({ error: 'failed to create order' });
}
});
app.put('/api/orders/:id', async (req, res) => {
try {
const orders = await loadOrders();
const idx = orders.findIndex((o) => String(o.id) === String(req.params.id));
if (idx === -1) return res.status(404).json({ error: 'not found' });
const existing = orders[idx];
const updated = Object.assign({}, existing, req.body, { updatedAt: new Date().toISOString() });
orders[idx] = updated;
await saveOrders(orders);
broadcast({ type: 'orders:updated', order: updated, orders });
res.json(updated);
} catch (err) {
console.error('put order failed', err);
res.status(500).json({ error: 'failed to update order' });
}
});
app.delete('/api/orders/:id', async (req, res) => {
try {
const orders = await loadOrders();
const idx = orders.findIndex((o) => String(o.id) === String(req.params.id));
if (idx === -1) return res.status(404).json({ error: 'not found' });
const removed = orders.splice(idx, 1)[0];
await saveOrders(orders);
broadcast({ type: 'orders:deleted', order: removed, orders });
res.json({ ok: true });
} catch (err) {
console.error('delete order failed', err);
res.status(500).json({ error: 'failed to delete order' });
}
});
// Add API endpoints for products
app.get('/api/products', async (req, res) => {
try {
const result = await pgPool.query('SELECT * FROM products ORDER BY created_at ASC');
res.json(result.rows);
} catch (err) {
console.error('Failed to fetch products:', err);
res.status(500).json({ error: 'Failed to fetch products' });
}
});
app.post('/api/products', async (req, res) => {
try {
const { name, description, price } = req.body;
if (!name || !price) {
return res.status(400).json({ error: 'Name and price are required' });
}
const result = await pgPool.query(
'INSERT INTO products (name, description, price) VALUES ($1, $2, $3) RETURNING *',
[name, description, price]
);
res.status(201).json(result.rows[0]);
} catch (err) {
console.error('Failed to add product:', err);
res.status(500).json({ error: 'Failed to add product' });
}
});
// Add DELETE endpoint for products
app.delete('/api/products/:id', async (req, res) => {
try {
const { id } = req.params;
const result = await pgPool.query('DELETE FROM products WHERE id = $1 RETURNING *', [id]);
if (result.rowCount === 0) {
return res.status(404).json({ error: 'Product not found' });
}
res.status(200).json({ message: 'Product deleted successfully', product: result.rows[0] });
} catch (err) {
console.error('Failed to delete product:', err);
res.status(500).json({ error: 'Failed to delete product' });
}
});
// Serve static files (the client app)
app.use(express.static(path.join(__dirname)));
const server = http.createServer(app);
// WebSocket server for realtime broadcasts
const wss = new WebSocketServer({ server });
function wsVerifyToken(query) {
if (!REALTIME_TOKEN) return true;
const token = (query && query.token) || '';
return token === REALTIME_TOKEN;
}
wss.on('connection', (ws, req) => {
// simple token check using url query
const url = new URL(req.url, `http://${req.headers.host}`);
const tokenOk = wsVerifyToken(Object.fromEntries(url.searchParams.entries()));
if (!tokenOk) {
try { ws.close(1008, 'invalid token'); } catch (e) {}
return;
}
// send current orders on connect
(async () => {
try {
const orders = await loadOrders();
ws.send(JSON.stringify({ type: 'orders:sync', orders }));
} catch (err) {
console.error('ws send failed:', err);
}
})();
ws.on('message', (raw) => {
// relay JSON messages to other clients (but do not persist blindly)
let msg = null;
try {
msg = JSON.parse(raw);
} catch (e) {
console.warn('received non-json ws message');
return;
}
// Accept 'publish' messages that contain a full orders array and optionally save
if (msg && msg.type === 'orders:publish' && Array.isArray(msg.orders)) {
// optional save if client asks for server canonicality
if (msg.save === true) {
saveOrders(msg.orders).catch(err => console.error('saveOrders failed', err));
}
broadcast(msg, ws);
} else {
// broadcast other messages as-is
broadcast(msg, ws);
}
});
});
function broadcast(obj, except) {
const raw = JSON.stringify(obj);
for (const client of wss.clients) {
if (client.readyState === client.OPEN && client !== except) {
try { client.send(raw); } catch (e) { console.warn('broadcast failed', e); }
}
}
}
(async () => {
await tryInitPostgres();
server.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
if (REALTIME_TOKEN) console.log('Realtime token auth is ENABLED');
});
})();