49 lines
1.6 KiB
JavaScript
49 lines
1.6 KiB
JavaScript
addEventListener('fetch', event => {
|
|
event.respondWith(handleRequest(event.request));
|
|
});
|
|
|
|
async function handleRequest(request) {
|
|
const url = new URL(request.url);
|
|
|
|
// Define the base hostname and path for the mappings
|
|
const baseHostname = 'YOURWORKER.YOURUSER.workers.dev';
|
|
const baseMappingPath = '/examplebase/';
|
|
const testFilesPath = '/testfiles/';
|
|
|
|
// Check if the path matches the desired format
|
|
// Add ifs as needed
|
|
if (url.hostname === baseHostname && (url.pathname.startsWith(baseMappingPath) || url.pathname.startsWith(testFilesPath))) {
|
|
const subpath = url.pathname.startsWith(baseMappingPath)
|
|
? url.pathname.substring(baseMappingPath.length) // Extract the subpath for mappings
|
|
: url.pathname.substring(testFilesPath.length); // Extract the subpath for test files
|
|
|
|
// Define the mapping of subpaths to redirect URLs
|
|
const baseTargetUrls = {
|
|
'x86_64.iso': 'https://example.com/x86_64.iso',
|
|
'i686.iso': 'https://example.com/i686.iso',
|
|
// Add more mappings here if needed
|
|
};
|
|
|
|
// Check if the subpath exists in the mapping
|
|
if (baseTargetUrls[subpath]) {
|
|
const targetUrl = baseTargetUrls[subpath];
|
|
return fetch(targetUrl, request);
|
|
}
|
|
|
|
// Check if the subpath exists in test files
|
|
const testFileUrls = {
|
|
'example': 'https://example.com'
|
|
// Add more test file mappings here if needed
|
|
};
|
|
|
|
if (testFileUrls[subpath]) {
|
|
const testFileUrl = testFileUrls[subpath];
|
|
return fetch(testFileUrl, request);
|
|
}
|
|
}
|
|
|
|
// Return a 404 response for other requests
|
|
return new Response('Not Found', { status: 404 });
|
|
}
|
|
|