Skip to content

Commit

Permalink
Allow CORS.
Browse files Browse the repository at this point in the history
  • Loading branch information
kj415j45 committed Sep 6, 2023
1 parent 3558f57 commit 5d9b7b0
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,34 @@ import log from './routes/log';

export default {
async fetch(request: any, env: Env, ctx: ExecutionContext): Promise<Response> {
try {
const reqPath = new URL(request.url).pathname;

// dynamic route
if (reqPath.startsWith('/log/') && reqPath.length > 1) {
return log(request, env, ctx);
}

// static route
switch (reqPath) {
case '':
case '/':
return index(request, env, ctx);
default:
return error(request, env, ctx);
}
} catch (e) {
return error(request, env, ctx);
}
const res = await handle(request, env, ctx);
res.headers.set('Access-Control-Allow-Origin', '*');
return res;
},

async scheduled(event: ScheduledEvent, env: Env, ctx: ExecutionContext) {
ctx.waitUntil(cleanup(env));
},
};

async function handle(request: any, env: Env, ctx: ExecutionContext): Promise<Response> {
try {
const reqPath = new URL(request.url).pathname;

// dynamic route
if (reqPath.startsWith('/log/') && reqPath.length > 1) {
return log(request, env, ctx);
}

// static route
switch (reqPath) {
case '':
case '/':
return index(request, env, ctx);
default:
return error(request, env, ctx);
}
} catch (e) {
return error(request, env, ctx);
}
}

0 comments on commit 5d9b7b0

Please sign in to comment.