-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
86 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,29 @@ | ||
export async function onRequest({env, request}) { | ||
const base = env.base || 'https://2fa.pages.dev/' | ||
const country = request.cf?.country.toLowerCase() || "int"; | ||
let uri = `${base}${country}` | ||
const res = await fetch(uri, { | ||
cf: { | ||
cacheTtl: 86400, cacheEverything: true | ||
export async function onRequestGet({request}) { | ||
const url = new URL(request.url); | ||
const base = `${url.protocol}//${url.hostname}/`; | ||
const redirectStatus = 302; | ||
try { | ||
const country = request.cf?.country?.toLowerCase() || 'int'; | ||
let uri = `${base}${country}/`; | ||
|
||
const res = await fetch(uri, { | ||
cf: { | ||
cacheTtlByStatus: { | ||
'200': 60 * 60 * 24 * 7, // Cache request 1 week | ||
'404': 60 * 60 * 24, // Cache request 1 day | ||
}, | ||
}, | ||
}); | ||
|
||
// Redirect to /int/ if that page works | ||
if (res.status !== 200) { | ||
const int = await fetch(`${base}int/`); | ||
uri = int.status === 200 ? `${base}/int/`:`${base}/503/`; | ||
} | ||
}) | ||
if (res.status !== 200) uri = `${base}/int` | ||
|
||
return Response.redirect(uri, 302); | ||
return Response.redirect(uri, redirectStatus); | ||
} catch (e) { | ||
console.error(e); | ||
return Response.redirect(`${base}/502/`, redirectStatus); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{{ define "main" }} | ||
<main> | ||
<h1 class="display-1">404</h1> | ||
<p class="note">Not Found</p> | ||
</main> | ||
<style> | ||
main { | ||
display: grid; | ||
justify-content: center; | ||
align-content: center; | ||
height: 100%; | ||
width: 100%; | ||
text-align: center; | ||
} | ||
|
||
.note { | ||
font-size: 2em; | ||
} | ||
</style> | ||
{{ end }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{{ define "main" }} | ||
<main> | ||
<h1 class="display-1">502</h1> | ||
<p class="note">Bad Gateway</p> | ||
</main> | ||
<style> | ||
main { | ||
display: grid; | ||
justify-content: center; | ||
align-content: center; | ||
height: 100%; | ||
width: 100%; | ||
text-align: center; | ||
} | ||
|
||
.note { | ||
font-size: 2em; | ||
} | ||
</style> | ||
{{ end }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{{ define "main" }} | ||
<main> | ||
<h1 class="display-1">503</h1> | ||
<p class="note">Service Unavailable</p> | ||
</main> | ||
<style> | ||
main { | ||
display: grid; | ||
justify-content: center; | ||
align-content: center; | ||
height: 100%; | ||
width: 100%; | ||
text-align: center; | ||
} | ||
|
||
.note { | ||
font-size: 2em; | ||
} | ||
</style> | ||
{{ end }} |