forked from badlogic/heissepreise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
144 lines (128 loc) · 5.16 KB
/
server.js
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
const fs = require("fs");
const path = require("path");
const http = require("http");
const analysis = require("./analysis");
const bundle = require("./bundle");
const csv = require("./site/js/misc");
const chokidar = require("chokidar");
const express = require("express");
const compression = require("compression");
function copyItemsToSite(dataDir) {
const items = analysis.readJSON(`${dataDir}/latest-canonical.json.${analysis.FILE_COMPRESSOR}`);
analysis.writeJSON(`site/output/data/latest-canonical.json`, items);
for (const store of analysis.STORE_KEYS) {
const storeItems = items.filter((item) => item.store === store);
analysis.writeJSON(`site/output/data/latest-canonical.${store}.compressed.json`, storeItems, false, 0, true);
}
const csvItems = csv.itemsToCSV(items);
fs.writeFileSync("site/output/data/latest-canonical.csv", csvItems, "utf-8");
console.log("Copied latest items to site.");
}
function scheduleFunction(hour, minute, second, func) {
const now = new Date();
const scheduledTime = new Date();
scheduledTime.setHours(hour);
scheduledTime.setMinutes(minute);
scheduledTime.setSeconds(second);
if (now > scheduledTime) {
scheduledTime.setDate(scheduledTime.getDate() + 1);
}
const delay = scheduledTime.getTime() - now.getTime();
console.log("Scheduling next function call: " + scheduledTime.toString());
setTimeout(async () => {
await func();
scheduleFunction(hour, minute, second, func);
}, delay);
}
function parseArguments() {
const args = process.argv.slice(2);
let port = process.env.PORT !== undefined && process.env.PORT != "" ? parseInt(process.env.PORT) : 3000;
let liveReload = process.env.NODE_ENV === "development" || false;
for (let i = 0; i < args.length; i++) {
if (args[i] === "-p" || args[i] === "--port") {
port = parseInt(args[i + 1]);
i++;
} else if (args[i] === "-l" || args[i] === "--live-reload") {
if (process.env.NODE_ENV !== "development") {
throw new Error("Live reload is only supported in development mode");
}
liveReload = true;
} else if (args[i] === "-h" || args[i] === "--help") {
console.log("Usage: node server.js [-p|--port PORT] [-l|--live-reload]");
console.log();
console.log("Options:");
console.log(" -p, --port PORT Port to listen on (default: 3000)");
console.log(" -l, --live-reload Enable live reload (automatically enabled if NODE_ENV is development)");
process.exit(0);
}
}
return { port, liveReload };
}
function setupLogging() {
// Poor man's logging framework, wooh...
const originalConsoleLog = console.log;
const logStream = fs.createWriteStream("site/output/data/log.txt", { flags: "a" });
logStream.write("===========================================\n\n");
console.log = (message) => {
const formattedMessage = `[${new Date().toISOString()}] ${message}\n`;
logStream.write(formattedMessage);
originalConsoleLog.apply(console, [message]);
};
}
(async () => {
const dataDir = "data";
const { port, liveReload } = parseArguments();
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir);
}
const outputDir = "site/output";
if (fs.existsSync("site/output/data/log.txt")) {
fs.copyFileSync("site/output/data/log.txt", "site/log.txt");
}
bundle.deleteDirectory(outputDir);
fs.mkdirSync(outputDir);
fs.mkdirSync(outputDir + "/data");
if (fs.existsSync("site/log.txt")) {
fs.copyFileSync("site/log.txt", "site/output/data/log.txt");
fs.unlinkSync("site/log.txt");
}
setupLogging();
bundle.bundle("site", outputDir, liveReload);
analysis.migrateCompression(dataDir, ".json", ".json.br");
analysis.migrateCompression(dataDir, ".json.gz", ".json.br");
if (fs.existsSync(`${dataDir}/latest-canonical.json.${analysis.FILE_COMPRESSOR}`)) {
copyItemsToSite(dataDir);
analysis.updateData(dataDir, (_newItems) => {
copyItemsToSite(dataDir);
});
} else {
await analysis.updateData(dataDir);
copyItemsToSite(dataDir);
}
scheduleFunction(5, 0, 0, async () => {
items = await analysis.updateData(dataDir);
copyItemsToSite(dataDir);
});
const app = express();
app.use(compression());
app.use(express.static("site/output"));
const server = http.createServer(app).listen(port, () => {
console.log(`App listening on port ${port}`);
});
if (liveReload) {
const socketIO = require("socket.io");
const sockets = [];
const io = socketIO(server);
io.on("connection", (socket) => sockets.push(socket));
let timeoutId = 0;
chokidar.watch("site/output").on("all", () => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
lastChangeTimestamp = Date.now();
for (let i = 0; i < sockets.length; i++) {
sockets[i].send(`${lastChangeTimestamp}`);
}
}, 500);
});
}
})();