-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.mjs
166 lines (152 loc) · 4.86 KB
/
server.mjs
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 express from "express";
import expressStaticGzip from "express-static-gzip";
import { createProxyMiddleware } from "http-proxy-middleware";
import https from "https";
import path from "path";
const app = express();
const __dirname = path.resolve(path.dirname("."));
// New http-proxy-middleware adds may add a slash in the end of the path
// which is recognized as a different path by some servers
// and causes 404 (e.g. /bimtrack/token)
function removeTrailingSlashFromPath(proxyReq) {
if (proxyReq.path.endsWith("/")) {
proxyReq.path = proxyReq.path.slice(0, -1);
}
}
app.use(
"/bimtrack/token",
createProxyMiddleware({
target: "https://auth.bimtrackapp.co/connect/token",
on: {
proxyReq: (proxyReq) => removeTrailingSlashFromPath(proxyReq),
},
changeOrigin: true,
})
);
app.use(
"/bimtrack/bcf/2.1",
createProxyMiddleware({
router: (req) => {
const url = new URL(`https://explorer.novorender.com/${req.url}`);
const server = url.searchParams.get("server") ?? "";
return server + "/bcf/2.1";
},
on: {
proxyReq: (proxyReq) => removeTrailingSlashFromPath(proxyReq),
},
changeOrigin: true,
})
);
app.use(
"/xsitemanage",
createProxyMiddleware({
target: "https://api.prod.xsitemanage.com",
pathRewrite: {
"^/xsitemanage/": "",
},
on: {
proxyReq: (proxyReq) => removeTrailingSlashFromPath(proxyReq),
},
changeOrigin: true,
})
);
app.use(
"/ditio-file",
createProxyMiddleware({
target: "https://ditio-api-v3.azurewebsites.net/api/file/",
pathRewrite: {
"^/ditio-file": "",
},
changeOrigin: true,
})
);
app.use(
"/ditio",
createProxyMiddleware({
target: "https://ditio-api-v3.azurewebsites.net",
pathRewrite: {
"^/ditio": "",
},
on: {
proxyReq: (proxyReq) => removeTrailingSlashFromPath(proxyReq),
},
changeOrigin: true,
})
);
app.use(
"/ditio-machines",
createProxyMiddleware({
target: "https://ditio-report-api.azurewebsites.net/api",
pathRewrite: {
"^/ditio-machines": "",
},
on: {
proxyReq: (proxyReq) => removeTrailingSlashFromPath(proxyReq),
},
changeOrigin: true,
})
);
app.use("/omega365", async (req, res) => {
try {
const url = new URL("https://nyeveier.pims365.no" + req.originalUrl.replace(/^\/omega365/, ""));
const omegaRes = await fetch(url, {
headers: {
authorization: req.headers.authorization,
},
});
if (omegaRes.ok) {
const cookieHeaders = omegaRes.headers.getSetCookie();
cookieHeaders.forEach((header) => res.append("set-cookie", header));
const json = await omegaRes.json();
res.json(json);
} else {
if (omegaRes.status >= 500) {
res.status(omegaRes.status).send();
} else {
res.status(401).send();
}
}
} catch (e) {
console.warn(e);
res.status(500).send();
}
});
app.use("/*", (_req, res, next) => {
res.header("Cross-Origin-Opener-Policy", "same-origin");
res.header("Cross-Origin-Embedder-Policy", "require-corp");
res.header("Cross-Origin-Resource-Policy", "cross-origin");
next();
});
app.use("/", express.static(path.resolve(__dirname, "dist")));
app.use(
"/",
expressStaticGzip(path.resolve(__dirname, "dist"), {
enableBrotli: true,
orderPreference: ["br", "gz"],
})
);
app.get("/config.json", (_req, res) => {
res.json({
bimCollabClientSecret: process.env.BIMCOLLAB_CLIENT_SECRET ?? "",
bimCollabClientId: process.env.BIMCOLLAB_CLIENT_ID ?? "",
bimTrackClientSecret: process.env.BIMTRACK_CLIENT_SECRET ?? "",
bimTrackClientId: process.env.BIMTRACK_CLIENT_ID ?? "",
jiraClientId: process.env.JIRA_CLIENT_ID ?? "",
jiraClientSecret: process.env.JIRA_CLIENT_SECRET ?? "",
xsiteManageClientId: process.env.XSITEMANAGE_CLIENT_ID ?? "",
novorenderClientId: process.env.NOVORENDER_CLIENT_ID ?? "",
novorenderClientSecret: process.env.NOVORENDER_CLIENT_SECRET ?? "",
dataV2ServerUrl: process.env.DATA_V2_SERVER_URL ?? "",
authServerUrl: process.env.AUTH_SERVER_URL ?? "",
assetsUrl: process.env.ASSETS_URL ?? "",
mixpanelToken: process.env.MIXPANEL_TOKEN ?? "",
});
});
app.get("/*", function (_req, res) {
res.sendFile(path.resolve(__dirname, "dist/index.html"));
});
if (process.env.PORT) {
app.listen(process.env.PORT || 80, () => console.log("Server is listening on port 80 or whatever Azure wants"));
} else {
https.createServer({}, app).listen(443);
}