-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (67 loc) · 2.12 KB
/
index.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
import proxy from "@fly/fetch/proxy";
import Url from "url-parse";
const adminTrafficRate = parseFloat(app.config.adminTrafficRate || 0.0);
const admin = {
name: "admin",
test: request => {
const referer = request.headers.get("referer");
// The adminbackend should handle any asset requests originating from an index served by
// adminBackend (Lambda indexes will request from s3) as bundle hash chunks may not match
// between EKS and Lambda
const isAssetsRequest = request.url.includes("/assets");
const isAutoassignRequest = request.url.includes("/autoassign");
const isAdminRequest = referer && referer.includes("/admin");
const isAdminRouted = Math.random() < adminTrafficRate;
return (
isAutoassignRequest || isAdminRequest || isAssetsRequest || isAdminRouted
);
},
respond: request => {
return proxy(`https://${app.config.adminBackend}`, {
headers: {
host: app.config.adminBackend,
"x-forwarded-host": request.headers.get("hostname")
}
})(request);
}
};
const texter = {
name: "texter",
test: _request => true,
respond: request => {
return proxy(`https://${app.config.texterBackend}`, {
headers: {
host: app.config.texterBackend,
"x-forwarded-host": request.headers.get("hostname")
}
})(request);
}
};
const backends = [admin, texter];
fly.http.respondWith(async request => {
const { isMaintenanceMode } = app.config;
const booleanTrue = isMaintenanceMode && isMaintenanceMode === true;
const textTrue = isMaintenanceMode && isMaintenanceMode === "true";
if (booleanTrue || textTrue) {
return new Response(
"<h1>Spoke is down for maintenance at the moment.</h1>",
{
status: 503
}
);
}
// Force HTTPS
const url = new Url(request.url);
if (url.protocol === "http:" && url.hostname !== "localhost") {
url.set("protocol", "https:");
return new Response("Redirecting", {
status: 302,
headers: { Location: url.toString() }
});
}
for (let backend of backends) {
if (backend.test(request)) {
return backend.respond(request);
}
}
});