-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
67 lines (56 loc) · 2.37 KB
/
Copy pathworker.js
File metadata and controls
67 lines (56 loc) · 2.37 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* Cloudflare Worker: cvroast-dev
*
* Routes /api/roast (POST/OPTIONS) to the Claude-powered CV scorer.
* Everything else falls through to static assets (index.html, images, etc.).
*
* Required bindings:
* - Secret: ANTHROPIC_API_KEY
* - KV namespace: RATE_LIMIT
*/
import { handleRoast, handleFetchJd, handleCors } from './functions/api/roast.js';
import { handleSignup, handleSignupCors } from './functions/api/signup.js';
import { handleStats, handleStatsCors } from './functions/api/stats.js';
import { handleFeedback, handleFeedbackCors } from './functions/api/feedback.js';
export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);
if (url.pathname === '/api/feedback') {
if (request.method === 'OPTIONS') return handleFeedbackCors();
if (request.method === 'POST') return handleFeedback(request, env);
return new Response('Method not allowed', { status: 405 });
}
if (url.pathname === '/api/roast') {
if (request.method === 'OPTIONS') return handleCors();
if (request.method === 'POST') return handleRoast(request, env);
return new Response('Method not allowed', { status: 405 });
}
if (url.pathname === '/api/fetch-jd') {
if (request.method === 'OPTIONS') return handleCors();
if (request.method === 'POST') return handleFetchJd(request);
return new Response('Method not allowed', { status: 405 });
}
if (url.pathname === '/api/signup') {
if (request.method === 'OPTIONS') return handleSignupCors();
if (request.method === 'POST') return handleSignup(request, env);
return new Response('Method not allowed', { status: 405 });
}
if (url.pathname === '/api/stats') {
if (request.method === 'OPTIONS') return handleStatsCors();
if (request.method === 'GET') return handleStats(request, env);
return new Response('Method not allowed', { status: 405 });
}
const response = await env.ASSETS.fetch(request);
if (url.pathname === '/feedback' || url.pathname === '/feedback.html') {
ctx.waitUntil(incrementPageView(env, 'feedback'));
}
return response;
},
};
async function incrementPageView(env, page) {
if (!env.RATE_LIMIT) return;
const key = `views:${page}`;
const raw = await env.RATE_LIMIT.get(key);
const count = (parseInt(raw, 10) || 0) + 1;
await env.RATE_LIMIT.put(key, String(count));
}