Skip to content

Commit

Permalink
fix: export fetch handler
Browse files Browse the repository at this point in the history
  • Loading branch information
rawkode committed Aug 4, 2023
1 parent 9bb297a commit 2880c52
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions projects/rawkode.link/src/index.ts
Expand Up @@ -14,37 +14,39 @@ interface Env {
RUDDERSTACK_AUTH: string;
}

const handler = async (
request: Request,
env: Env,
ctx: ExecutionContext,
): Promise<Response> => {
const requestUrl = new URL(request.url);
export default {
async fetch(
request: Request,
env: Env,
ctx: ExecutionContext,
): Promise<Response> {
const requestUrl = new URL(request.url);

console.log(`Request for ${requestUrl.host}`);
console.log(`Request for ${requestUrl.host}`);

const hostname = requestUrl.host;
const path = requestUrl.pathname.substring(1);
const hostname = requestUrl.host;
const path = requestUrl.pathname.substring(1);

console.log(`Handling request on domain '${hostname}' for '${path}'`);
console.log(`Handling request on domain '${hostname}' for '${path}'`);

ctx.waitUntil(logRequest(request, env, hostname, path));
ctx.waitUntil(logRequest(request, env, hostname, path));

if (!(hostname in redirects["domains"])) {
console.log("This domain is not managed by rawkode.link");
return Response.redirect(redirects.defaultRedirect);
}
if (!(hostname in redirects["domains"])) {
console.log("This domain is not managed by rawkode.link");
return Response.redirect(redirects.defaultRedirect);
}

const domain = redirects["domains"][hostname];
const domain = redirects["domains"][hostname];

if (path in domain["redirects"]) {
console.log(`Redirecting too '${domain["redirects"][path].to}'`);
return Response.redirect(domain["redirects"][path].to);
}
if (path in domain["redirects"]) {
console.log(`Redirecting too '${domain["redirects"][path].to}'`);
return Response.redirect(domain["redirects"][path].to);
}

console.log("Path not found, default redirect");
console.log("Path not found, default redirect");

return Response.redirect(domain.defaultRedirect);
return Response.redirect(domain.defaultRedirect);
},
};

const logRequest = async (
Expand Down Expand Up @@ -94,5 +96,3 @@ const logRequest = async (

console.debug(response);
};

export default handler;

0 comments on commit 2880c52

Please sign in to comment.