-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
redirect.js
29 lines (25 loc) · 881 Bytes
/
redirect.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
export async function onRequestGet({ request }) {
const url = new URL(request.url);
const base = `${url.protocol}//${url.hostname}/`;
const redirectStatus = 301;
const api = 'https://api.2fa.directory/frontend/v1/';
try {
const country = request.cf?.country?.toLowerCase();
const res = await fetch(`${api}${country}/categories.json`, {
method: 'HEAD',
cf: {
cacheTtlByStatus: {
"200": 60 * 60 * 24 * 14, // Cache request 2 weeks
"404": 60 * 60 * 24 * 7, // Cache request 1 week
},
}
});
let uri = res.status === 200 ? `${base}${country}/` : `${base}int/`
const params = url.searchParams.toString();
if (params) uri += `?${params}`;
return Response.redirect(uri, redirectStatus);
} catch (e) {
console.error(e);
return Response.redirect(`${base}502/`, redirectStatus);
}
}