Proxy /api to chookspace /api
This commit is contained in:
82
src/index.ts
82
src/index.ts
@@ -58,6 +58,49 @@ export default {
|
|||||||
const repo = parts[1];
|
const repo = parts[1];
|
||||||
let filePath = parts.slice(2).join('/');
|
let filePath = parts.slice(2).join('/');
|
||||||
|
|
||||||
|
if (user == "api") {
|
||||||
|
// Proxy a call to the Gitea API, so CORS can be satisfied
|
||||||
|
let apiHeaders = new Headers(request.headers);
|
||||||
|
if (!apiHeaders.has("Authorisation")) {
|
||||||
|
apiHeaders.append("Authorisation", "token 7d2625bcbbb397735bf59f88409e7b86a52087b6");
|
||||||
|
}
|
||||||
|
apiHeaders.set('User-Agent', 'Chsp-Worker-Proxy/1.0');
|
||||||
|
const apiPath = parts.slice(1).join('/');
|
||||||
|
const apiBase = `https://chookspace.com/api/${apiPath}`;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const rawResponse = await fetch(apiBase, {
|
||||||
|
method: request.method,
|
||||||
|
headers: apiHeaders,
|
||||||
|
redirect: 'follow'
|
||||||
|
});
|
||||||
|
console.info("Response from Chookspace was", rawResponse.status);
|
||||||
|
|
||||||
|
// 4. If success (2xx), proxy it
|
||||||
|
if (rawResponse.status >= 200 && rawResponse.status < 400) {
|
||||||
|
console.info("Proxying page");
|
||||||
|
const mimeType = getMimeType(filePath);
|
||||||
|
|
||||||
|
// Create new headers to override Content-Type
|
||||||
|
const newHeaders = new Headers(rawResponse.headers);
|
||||||
|
newHeaders.set('Content-Type', mimeType);
|
||||||
|
|
||||||
|
return new Response(rawResponse.body, {
|
||||||
|
status: rawResponse.status,
|
||||||
|
statusText: rawResponse.statusText,
|
||||||
|
headers: newHeaders
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
console.info("Redirecting to Chookspace normally...");
|
||||||
|
const redirectUrl = new URL(url.toString());
|
||||||
|
redirectUrl.hostname = 'chookspace.com';
|
||||||
|
return Response.redirect(redirectUrl.toString(), 302);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// 2. Handle index.html default
|
// 2. Handle index.html default
|
||||||
if (!filePath || filePath === '') {
|
if (!filePath || filePath === '') {
|
||||||
filePath = 'index.html';
|
filePath = 'index.html';
|
||||||
@@ -104,6 +147,45 @@ export default {
|
|||||||
console.error('Fetch error:', e);
|
console.error('Fetch error:', e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Also check the main branch
|
||||||
|
// URL structure: https://chookspace.com/(user)/(repo)/raw/branch/main/chookspace/(file)
|
||||||
|
const mainRawBase = `https://chookspace.com/${user}/${repo}/raw/branch/main/chookspace`;
|
||||||
|
const mainRawUrl = `${mainRawBase}/${filePath}`;
|
||||||
|
|
||||||
|
console.log("Raw URL: ", mainRawUrl);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Prepare headers for the fetch request
|
||||||
|
const fetchHeaders = new Headers(request.headers);
|
||||||
|
fetchHeaders.set('User-Agent', 'Chsp-Worker-Proxy/1.0');
|
||||||
|
|
||||||
|
const rawResponse = await fetch(mainRawUrl, {
|
||||||
|
method: request.method,
|
||||||
|
headers: fetchHeaders,
|
||||||
|
redirect: 'follow'
|
||||||
|
});
|
||||||
|
console.info("Response from Chookspace was", rawResponse.status);
|
||||||
|
|
||||||
|
// 4. If success (2xx), proxy it
|
||||||
|
if (rawResponse.status >= 200 && rawResponse.status < 400) {
|
||||||
|
console.info("Proxying page");
|
||||||
|
const mimeType = getMimeType(filePath);
|
||||||
|
|
||||||
|
// Create new headers to override Content-Type
|
||||||
|
const newHeaders = new Headers(rawResponse.headers);
|
||||||
|
newHeaders.set('Content-Type', mimeType);
|
||||||
|
|
||||||
|
return new Response(rawResponse.body, {
|
||||||
|
status: rawResponse.status,
|
||||||
|
statusText: rawResponse.statusText,
|
||||||
|
headers: newHeaders
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// Fetch failed (network error etc), proceed to fallback
|
||||||
|
console.error('Fetch error:', e);
|
||||||
|
}
|
||||||
|
|
||||||
// 5. If not found or error, redirect to chookspace site
|
// 5. If not found or error, redirect to chookspace site
|
||||||
console.info("Redirecting to Chookspace normally...");
|
console.info("Redirecting to Chookspace normally...");
|
||||||
const redirectUrl = new URL(url.toString());
|
const redirectUrl = new URL(url.toString());
|
||||||
|
|||||||
Reference in New Issue
Block a user