-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.ts
66 lines (55 loc) · 2.2 KB
/
server.ts
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
/* tslint:disable no-console */
import * as express from "express";
import * as http from "http";
import * as path from "path";
import * as proxy from "http-proxy-middleware";
import { URL } from "whatwg-url";
import { FRONTEND_URL, PORT } from "./src/helpers/config";
const app = express();
function isWikiData(url) {
return url.startsWith("https://www.wikidata.org/") || url.startsWith("http://www.wikidata.org/");
}
export const dataProxy = proxy({
changeOrigin: true,
onProxyReq: (proxyReq, req) => {
if (isWikiData(req.url) && req.headers.Accept !== "text/n3") {
proxyReq.setHeader("Accept", "text/n3");
}
},
onProxyRes: (proxyRes) => {
const loc = proxyRes.headers.location;
if ([301, 302, 303, 307, 308].includes(proxyRes.statusCode) && loc) {
proxyRes.headers.location = `${FRONTEND_URL}proxy?iri=${encodeURIComponent(loc)}`;
}
delete proxyRes.headers["strict-transport-security"];
delete proxyRes.headers["access-control-allow-origin"];
},
pathRewrite: (reqPath) => {
const iri = new URL(reqPath, "http://example.com").searchParams.get("iri");
const url = new URL(decodeURIComponent(iri));
return url.toString().slice(url.origin.length);
},
router: (req: http.IncomingMessage) => {
const iri = new URL(req.url, "http://example.com").searchParams.get("iri");
const url = decodeURIComponent(iri);
if (url.startsWith("http://dbpedia.org/")) {
return "http://dbpedia.org";
} else if (isWikiData(url)) {
return "https://www.wikidata.org";
} else if (url.startsWith("https://argu.co/")) {
return "https://argu.co";
} else if (url.startsWith("http://umbel.org/")) {
return "http://umbel.org";
}
throw new Error("Domain not whitelisted for poxy (contact us to get whitelisted)");
},
target: "http://dbpedia.org/",
xfwd: true,
});
app.use(express.static(__dirname + "/dist"));
app.get("/proxy", dataProxy);
app.get("*", (_request, response) => {
response.sendFile(path.resolve(__dirname, "dist", "index.html"));
});
app.listen(PORT);
console.log(`Listening to ${PORT}`);