-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
137 lines (113 loc) · 3.9 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
const express = require("express");
const fs = require("fs");
const rateLimit = require("express-rate-limit");
const bodyParser = require("body-parser");
const cors = require("cors");
const path = require("path");
const apiLimiter = rateLimit({
windowMs: 1 * 60 * 1000, // Timeframe
max: 10000, // Max requests per timeframe per ip
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
handler: (request, response, next, options) => {
// writeErrorLog(
// `Too many misc API requests`,
// `IP ${request.client._peername.address}`
// );
return response.status(options.statusCode).send(options.message);
},
});
const app = express();
const port = 5000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
var corsOptions = {
origin: "*",
};
app.use(cors(corsOptions));
// app.use(apiLimiter);
app.use(function (req, res, next) {
if (req.secure) {
res.setHeader(
"Strict-Transport-Security",
"max-age=31536000; includeSubDomains; preload"
);
}
next();
});
app.use(
"/legacy_viewer",
express.static(path.join(__dirname, "legacy_viewer")),
);
app.use("/viewer", express.static(path.join(__dirname, "viewer/build")));
app.use("/editor", express.static(path.join(__dirname, "editor/build")));
const getKey = (uuid) => {
var files = fs
.readdirSync(path.join(__dirname, "keys"))
.filter((fn) => fn.startsWith(uuid));
if (files.length > 0) {
const path = `keys/${files[0]}`;
return path;
}
return null;
};
// for /key/:uuid requests, return the corresponding json file
app.get("/key/:uuid", (req, res) => {
const uuid = req.params.uuid;
// get the json file starting with the uuid
var keyfile = getKey(uuid);
if (keyfile) {
res.sendFile(keyfile, { root: __dirname });
return;
} else {
// if the key is not found, get all keys from the github repo https://github.com/Artsdatabanken/clavis-keys and store them in the keys folder
const { exec } = require("child_process");
exec(`git clone https://github.com/Artsdatabanken/clavis-keys.git`);
var files = fs.readdirSync(path.join(__dirname, "clavis-keys"));
files.forEach((file) => {
// if the file is a json file and it is not in the keys folder or the file in the keys folder is not the same size as the file in the clavis-keys folder, move the file to the keys folder
if (
file.endsWith(".json") &&
(!fs.existsSync(`keys/${file}`) ||
fs.statSync(`clavis-keys/${file}`).size !=
fs.statSync(
`keys/${fs.readdirSync(path.join(__dirname, "keys"))[0]}`
).size)
) {
fs.rmSync(`keys/${file}`, { recursive: true, force: true });
fs.renameSync(`clavis-keys/${file}`, `keys/${file}`);
}
// otherwise, delete the file
else {
fs.rmSync(`clavis-keys/${file}`, { recursive: true, force: true });
}
});
}
// Try again to get the json file starting with the uuid now that it should be in the keys folder
if (keyfile) {
res.sendFile(keyfile, { root: __dirname });
return;
}
// if the key is still not found, return 404
res.status(404).send("Key not found");
});
app.get("/", (req, res) => {
// if there is no uuid argument, redirect to the legacy viewer
if (req.url.includes("csv")) {
return res.redirect(`/legacy_viewer${req.url}`);
} else {
return res.redirect(`/viewer${req.url}`);
}
});
app.get("/version", (req, res) => {
let v = "Gitless";
const gitfile = ".git/FETCH_HEAD";
if (fs.existsSync(gitfile)) {
v = fs.readFileSync(gitfile).toString().split("\t")[0];
}
fs.stat("./server.js", function (err, stats) {
res.status(200).send(`<h3>${v}</h3><hr/>${stats.mtime.toUTCString()}`);
});
});
// app.use('/', express.static('legacy_editor'))
app.listen(port, console.log(`Server now running on port ${port}`));