-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudflare_worker.js
More file actions
99 lines (89 loc) · 3.23 KB
/
cloudflare_worker.js
File metadata and controls
99 lines (89 loc) · 3.23 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* NY Gaming Data — Cloudflare Worker
*
* Paste this into the Cloudflare Worker editor and click Save & Deploy.
* Then: Settings → Variables → Secrets → add GITHUB_TOKEN = your PAT
* After adding the secret, click Save & Deploy again to bind it.
*/
addEventListener('fetch', function(event) {
event.respondWith(
handleRequest(event.request).catch(function(err) {
// Catch-all: always return JSON with CORS headers, even on crashes
return new Response(JSON.stringify({ error: 'Worker error: ' + err.message }), {
status: 500,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
},
});
})
);
});
async function handleRequest(request) {
var cors = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
};
// Handle CORS preflight
if (request.method === 'OPTIONS') {
return new Response(null, { status: 204, headers: cors });
}
if (request.method !== 'POST') {
return new Response(JSON.stringify({ error: 'Method not allowed' }), {
status: 405,
headers: Object.assign({ 'Content-Type': 'application/json' }, cors),
});
}
// Parse email from request body
var email;
try {
var body = await request.json();
email = (body.email || '').trim().toLowerCase();
} catch (e) {
return new Response(JSON.stringify({ error: 'Invalid JSON body' }), {
status: 400,
headers: Object.assign({ 'Content-Type': 'application/json' }, cors),
});
}
if (!email || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
return new Response(JSON.stringify({ error: 'Invalid email address' }), {
status: 400,
headers: Object.assign({ 'Content-Type': 'application/json' }, cors),
});
}
// Check that GITHUB_TOKEN secret is bound
if (typeof GITHUB_TOKEN === 'undefined') {
return new Response(JSON.stringify({ error: 'GITHUB_TOKEN secret not configured. Add it under Settings → Variables in the Cloudflare dashboard, then redeploy.' }), {
status: 500,
headers: Object.assign({ 'Content-Type': 'application/json' }, cors),
});
}
// Trigger GitHub Actions workflow
var ghRes = await fetch(
'https://api.github.com/repos/nosherzapoo/OSBdata/actions/workflows/ny-gaming-manual.yml/dispatches',
{
method: 'POST',
headers: {
'Authorization': 'Bearer ' + GITHUB_TOKEN,
'Accept': 'application/vnd.github.v3+json',
'Content-Type': 'application/json',
'User-Agent': 'ny-gaming-worker',
},
body: JSON.stringify({ ref: 'main', inputs: { recipient_email: email } }),
}
);
if (ghRes.status === 204) {
return new Response(JSON.stringify({ success: true }), {
status: 200,
headers: Object.assign({ 'Content-Type': 'application/json' }, cors),
});
}
var errBody = await ghRes.text();
return new Response(JSON.stringify({ error: 'GitHub API error ' + ghRes.status + ': ' + errBody }), {
status: 500,
headers: Object.assign({ 'Content-Type': 'application/json' }, cors),
});
}