-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
69 lines (66 loc) · 2.03 KB
/
server.js
File metadata and controls
69 lines (66 loc) · 2.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
import fs from "fs";
import { exec } from "child_process";
import fetch from "node-fetch";
import { createServer } from "http";
var csldat, slcdat;
exec("pip install -r requirements.txt");
var updCDat = async () => {
//every hour update countries.json
//var data=await (await fetch("https://api.covid19api.com/countries")).json();
var data = (
await (
await fetch(
"https://covid-api.com/api/regions?per_page=1002&order=name&sort=asc"
)
).json()
)["data"];
csldat = JSON.stringify(
Object.fromEntries(data.map((e) => [e["name"], e["iso"]]))
);
fs.writeFileSync("countriesSlug.json", csldat);
};
setInterval(updCDat, 3600000);
updCDat().then(() => {
createServer((req, res) => {
var mimeType = "image/png";
if (req.url.endsWith(".css")) {
mimeType = "text/css";
} else if (["", "/"].includes(req.url) || req.url.endsWith(".html")) {
mimeType = "text/html";
}
res.writeHead(200, { "Content-Type": mimeType });
if (["", "/"].includes(req.url)) {
res.write(
fs
.readFileSync("public/index.html", "utf8")
.replace(
"***countries***",
fs.readFileSync("countriesSlug.json", "utf8")
)
);
} else if (req.url.startsWith("/c/")) {
if (!fs.existsSync("/tmp/c")) fs.mkdirSync("/tmp/c");
var execProcess = exec(
'python3 server.py "' +
req.url
.substr(3)
.toLowerCase()
.replace(/%20/g, " ")
.replace(/\"/g, "") +
'"'
);
execProcess.stdout.pipe(process.stdout);
execProcess.stderr.pipe(process.stderr);
execProcess.on("exit", function () {
var img = "/tmp" + req.url.toLowerCase().replace(/%20/g, " ") + ".png";
if (fs.existsSync(img)) res.write(fs.readFileSync(img));
res.end();
});
return;
} else {
if (fs.existsSync("public" + req.url))
res.write(fs.readFileSync("public" + req.url, "utf8"));
}
res.end();
}).listen(process.env.PORT || 8080);
});