-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
166 lines (150 loc) · 3.69 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import path from "path";
import countries from "./data/borders.json" assert { type: "json" };
import compression from "compression";
import bodyParser from "body-parser";
import schedule from "node-schedule";
import fs from "node:fs/promises";
import fscb from "fs";
import express from "express";
import { fileURLToPath } from "url";
const isProduction = process.env.NODE_ENV === "production";
const port = process.env.PORT || 5173;
const base = process.env.BASE || "/";
const DIST_DIR = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
"dist"
);
const app = express();
let country = null;
let rotateAngle = null;
newCountry();
let vite;
if (!isProduction) {
const { createServer } = await import("vite");
vite = await createServer({
server: { middlewareMode: true },
appType: "custom",
base,
});
app.use(vite.middlewares);
} else {
app.use(express.static(DIST_DIR, { index: false }));
}
app.use(compression());
app.use(
bodyParser.urlencoded({
extended: true,
})
);
app.use(bodyParser.json());
const rule = new schedule.RecurrenceRule();
rule.hour = 0;
rule.minute = 0;
rule.tz = "Etc/UTC";
const daily = schedule.scheduleJob(rule, function () {
newCountry();
});
app.get("*.js", function (req, res, next) {
req.url = req.url + ".gz";
res.set("Content-Encoding", "gzip");
next();
});
app.post("/getAnswer", (req, res) => {
res.send(
JSON.stringify({
country: country,
rotateAngle: rotateAngle,
})
);
res.end();
});
app.post("/getNum", (req, res) => {
if (isProduction) {
fscb.readFile("./data/data.json", (err, data) => {
if (err) {
fs.writeFile("./data/data.json", `{"num":1}`);
res.send(
JSON.stringify({
num: 1,
})
);
res.end();
} else {
data = JSON.parse(data);
res.send(
JSON.stringify({
num: data.num,
})
);
res.end();
}
});
} else {
res.send(
JSON.stringify({
num: "DEV",
})
);
res.end();
}
});
app.use("*", async (req, res) => {
try {
if (isProduction) {
const html = await fs.readFile("./dist/index.html", "utf-8");
res.status(200).set({ "Content-Type": "text/html" }).end(html);
} else {
const html = await vite.transformIndexHtml(
req.originalUrl,
await fs.readFile("index.html", "utf-8")
);
res.status(200).set({ "Content-Type": "text/html" }).end(html);
}
} catch (e) {
vite?.ssrFixStacktrace(e);
console.log(e.stack);
res.status(500).end(e.stack);
}
});
function randomCountry() {
let randomCountry =
countries[Math.floor(Math.random() * (countries.length - 1))].name;
while (
//broken countries
randomCountry == "Micronesia" ||
randomCountry == "Tuvalu" ||
randomCountry == "Palestine" ||
randomCountry == "Marshall Islands" ||
randomCountry == "Indonesia" ||
randomCountry == "Malaysia"
) {
randomCountry =
countries[Math.floor(Math.random() * (countries.length - 1))].name;
}
return randomCountry;
}
function newCountry() {
if (country != null) {
let lastCountry = country;
while (country == lastCountry) {
country = randomCountry();
}
} else {
country = randomCountry();
}
rotateAngle = Math.max(30, Math.random() * 330);
if (true) {
fscb.readFile("./data/data.json", (err, data) => {
if (err) {
fs.writeFile("./data/data.json", `{"num":1}`);
} else {
data = JSON.parse(data);
data.num += 1;
fs.writeFile("./data/data.json", JSON.stringify(data));
}
});
}
}
app.listen(port, () => {
console.log(`Server started at http://localhost:${port}`);
});