-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
55 lines (55 loc) · 1.41 KB
/
server.js
File metadata and controls
55 lines (55 loc) · 1.41 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
const dep = {
ws: import("ws"),
fs: import("fs"),
http: import("http"),
ejs: import("ejs"),
cache: import("cache-manager"),
mysql: import("easy-mysql-js"),
};
const cache = dep.cache.createCache({
ttl: 10000,
refreshThreshold: 0,
});
const port = process.env.PORT || 3001;
async function evaluate(db, body) {
switch (body.method) {
case "PAYLOAD": {
await cache.set(
db.find((license) => license.id === body.sec).key,
body.payload,
);
break;
}
case "COLLECT":
return await cache.get(body.sec);
break;
}
}
async function handle(req, res) {
let header;
const db = await mysql.Select("select * from license");
const body = JSON.parse(req.body);
switch (req.method.includes("POST")) {
case true:
Object.assign(header, {
type: "application/json",
body: evaluate(db, body),
});
break;
case false:
dep.fs.readFileSync("views/index.ejs", { encoding: "utf8" }, (data) => {
Object.assign(header, {
type: "text/html",
body: dep.ejs.render(data, {}, { async: true }),
});
});
break;
}
res.writeHeader(200, { "Content-Type": header.type });
res.write(header.body);
res.end();
}
const server = dep.http.createServer(function(req, res) { handle(req, res) });
server.listen(port, "0.0.0.0", () => {
console.log(`Server is running on http://0.0.0.0:${port}`);
});