forked from kriasoft/graphql-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
53 lines (45 loc) · 1.53 KB
/
main.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
/**
* Cloudflare Worker
*
* @see https://developers.cloudflare.com/workers/
* @copyright 2016-present Kriasoft (https://git.io/vMINh)
*/
import { parseHostname } from "./parse";
async function handleRequest(request: Request) {
const url = new URL(request.url);
const { pathname: path, hostname } = url;
const [env, ver] = parseHostname(hostname);
// GraphQL API and authentication
if (path.startsWith("/graphql") || path.startsWith("/auth")) {
url.hostname = [
`${GOOGLE_CLOUD_REGION}-`,
`${GOOGLE_CLOUD_PROJECT[env]}${ver ? `_${ver}` : ""}`,
".cloudfunctions.net",
].join("");
url.pathname = `${ver ? `/api_${ver}` : "/api"}${path}`;
return fetch(new Request(url.toString(), request));
}
// Configure access to search engines
// https://moz.com/learn/seo/robotstxt
if (path === "/robots.txt") {
return env === "prod"
? new Response("User-agent: *\nAllow: /")
: new Response("User-agent: *\nDisallow: /");
}
// Landing pages and blog
if (path === "/" || path.startsWith("/blog")) {
url.hostname = "example.webflow.io";
return fetch(new Request(url.toString(), request));
}
// Help pages
if (path === "/help" || path.startsWith("/help")) {
url.hostname = "intercom.help";
url.pathname = "/example-xxx/en/" + path.substring(6);
return fetch(new Request(url.toString(), request));
}
// TODO: Web app
return new Response(`Hello from ${url.pathname}!`);
}
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});