forked from labex-labs/python-cheatsheet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.ts
More file actions
425 lines (375 loc) · 12.7 KB
/
worker.ts
File metadata and controls
425 lines (375 loc) · 12.7 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/// <reference types="@cloudflare/workers-types" />
const SUPPORTED_LOCALES = ['en', 'zh', 'es', 'fr', 'de', 'ja', 'ru', 'ko', 'pt'] as const
interface QuizRecordRequest {
quizId: string
pagePath: string
userId?: number
}
interface UserInfo {
id?: number
name?: string
nick_name?: string
email?: string
[key: string]: unknown
}
interface UserData {
user?: UserInfo
[key: string]: unknown
}
/**
* Normalize path to English version by removing language prefix
* This ensures all language versions of the same page share the same quiz data
*/
function normalizePathToEnglish(path: string): string {
const segments = path.split('/').filter(Boolean)
if (segments.length > 0 && SUPPORTED_LOCALES.includes(segments[0] as typeof SUPPORTED_LOCALES[number])) {
segments.shift()
return segments.length > 0 ? '/' + segments.join('/') : '/'
}
return path
}
export default {
async fetch(request: Request, env: { PYTHONCHEATSHEET_QUIZ_KV: KVNamespace; ASSETS?: { fetch: (_req: Request) => Promise<Response> } }): Promise<Response> {
const url = new URL(request.url)
// Handle redirects for custom domains
if (url.hostname === 'pythoncheatsheet.org' || url.hostname === 'www.pythoncheatsheet.org') {
const targetPath = url.pathname === '/' ? '' : url.pathname
const targetUrl = `https://labex.io/pythoncheatsheet${targetPath}${url.search}`
return Response.redirect(targetUrl, 301)
}
// Handle API routes
if (url.pathname.startsWith('/pythoncheatsheet/api/')) {
return handleAPI(request, env)
}
// For all other requests, let Cloudflare handle static assets
// If ASSETS is available (for Workers with assets), use it; otherwise pass through
if (env.ASSETS) {
return env.ASSETS.fetch(request)
}
// Fallback: return the request as-is (Cloudflare will handle it)
return fetch(request)
},
}
async function handleAPI(request: Request, env: { PYTHONCHEATSHEET_QUIZ_KV: KVNamespace }): Promise<Response> {
const url = new URL(request.url)
// Handle CORS preflight
if (request.method === 'OPTIONS') {
return new Response(null, {
status: 204,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Credentials': 'true',
},
})
}
// Handle POST /pythoncheatsheet/api/quiz/record
if (url.pathname === '/pythoncheatsheet/api/quiz/record' && request.method === 'POST') {
return handleRecordQuiz(request, env)
}
// Handle GET /pythoncheatsheet/api/quiz/stats
if (url.pathname === '/pythoncheatsheet/api/quiz/stats' && request.method === 'GET') {
return handleGetStats(request, env)
}
// Handle GET /pythoncheatsheet/api/quiz/user-status
if (url.pathname === '/pythoncheatsheet/api/quiz/user-status' && request.method === 'GET') {
return handleGetUserStatus(request, env)
}
// Handle GET /pythoncheatsheet/api/user/me
if (url.pathname === '/pythoncheatsheet/api/user/me' && request.method === 'GET') {
return handleUserMe(request, env)
}
return new Response(
JSON.stringify({ error: 'Not found' }),
{
status: 404,
headers: { 'Content-Type': 'application/json' },
}
)
}
async function getUserFromCookies(request: Request, env: { PYTHONCHEATSHEET_QUIZ_KV: KVNamespace }): Promise<UserInfo | null> {
try {
const cookies = request.headers.get('Cookie')
if (!cookies) {
return null
}
// Hash the cookies to use as a cache key
const encoder = new TextEncoder()
const data = encoder.encode(cookies)
const hashBuffer = await crypto.subtle.digest('SHA-256', data)
const hashArray = Array.from(new Uint8Array(hashBuffer))
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('')
// Key format: pythoncheatsheet:session:${hashHex}
const cacheKey = `pythoncheatsheet:session:${hashHex}`
// Try to get from KV first
const cachedUser = await env.PYTHONCHEATSHEET_QUIZ_KV.get(cacheKey)
if (cachedUser) {
try {
return JSON.parse(cachedUser) as UserInfo
} catch (e) {
console.error('Error parsing cached user:', e)
}
}
const response = await fetch('https://labex.io/api/v2/users/me', {
method: 'GET',
headers: {
'Cookie': cookies,
'User-Agent': request.headers.get('User-Agent') || 'Cloudflare Worker',
'Content-Type': 'application/json',
},
})
if (response.ok) {
const userData = await response.json() as UserData
const user = userData.user || null
if (user) {
// Cache the user info in KV with a 10-minute expiration
// We only cache if we got a valid user
await env.PYTHONCHEATSHEET_QUIZ_KV.put(cacheKey, JSON.stringify(user), { expirationTtl: 600 })
}
return user
}
return null
} catch (error) {
console.error('Error fetching user from cookies:', error)
return null
}
}
async function handleRecordQuiz(request: Request, env: { PYTHONCHEATSHEET_QUIZ_KV: KVNamespace }): Promise<Response> {
try {
const body = await request.json() as QuizRecordRequest
const { quizId, pagePath } = body
if (!quizId || !pagePath) {
return new Response(
JSON.stringify({ error: 'Missing required fields: quizId and pagePath' }),
{
status: 400,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
}
)
}
const normalizedPath = normalizePathToEnglish(pagePath)
// Generate unique key: combine pythoncheatsheet prefix, normalized pagePath and quizId
// Format: pythoncheatsheet:quiz:${normalizedPath}:${quizId}
const key = `pythoncheatsheet:quiz:${normalizedPath}:${quizId}`
// Get current count from KV
const currentCount = await env.PYTHONCHEATSHEET_QUIZ_KV.get(key)
const count = currentCount ? parseInt(currentCount, 10) : 0
// Increment count
const newCount = count + 1
// Save to KV
await env.PYTHONCHEATSHEET_QUIZ_KV.put(key, newCount.toString())
// Record user completion status if user is authenticated
const user = await getUserFromCookies(request, env)
if (user && user.id) {
// Format: pythoncheatsheet:user-quiz:${userId}:${normalizedPath}:${quizId}
const userKey = `pythoncheatsheet:user-quiz:${user.id}:${normalizedPath}:${quizId}`
await env.PYTHONCHEATSHEET_QUIZ_KV.put(userKey, 'true')
}
return new Response(
JSON.stringify({
success: true,
quizId,
pagePath: normalizedPath,
count: newCount,
...(user && user.id && { userId: user.id }),
}),
{
status: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
'Cache-Control': 'no-store, no-cache, must-revalidate',
},
}
)
} catch (error) {
console.error('Error recording quiz completion:', error)
return new Response(
JSON.stringify({ error: 'Internal server error' }),
{
status: 500,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Cache-Control': 'no-store, no-cache, must-revalidate',
},
}
)
}
}
async function handleGetStats(request: Request, env: { PYTHONCHEATSHEET_QUIZ_KV: KVNamespace }): Promise<Response> {
try {
const url = new URL(request.url)
const quizId = url.searchParams.get('quizId')
const pagePath = url.searchParams.get('pagePath')
if (!quizId || !pagePath) {
return new Response(
JSON.stringify({ error: 'Missing required query parameters: quizId and pagePath' }),
{
status: 400,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
}
)
}
const normalizedPath = normalizePathToEnglish(pagePath)
// Generate unique key with pythoncheatsheet prefix
// Format: pythoncheatsheet:quiz:${normalizedPath}:${quizId}
const key = `pythoncheatsheet:quiz:${normalizedPath}:${quizId}`
const countStr = await env.PYTHONCHEATSHEET_QUIZ_KV.get(key)
const count = countStr ? parseInt(countStr, 10) : 0
return new Response(
JSON.stringify({
success: true,
quizId,
pagePath: normalizedPath,
count,
}),
{
status: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
'Cache-Control': 'public, max-age=60, s-maxage=60',
},
}
)
} catch (error) {
console.error('Error fetching quiz stats:', error)
return new Response(
JSON.stringify({ error: 'Internal server error' }),
{
status: 500,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Cache-Control': 'no-store, no-cache, must-revalidate',
},
}
)
}
}
async function handleGetUserStatus(request: Request, env: { PYTHONCHEATSHEET_QUIZ_KV: KVNamespace }): Promise<Response> {
try {
const url = new URL(request.url)
const quizId = url.searchParams.get('quizId')
const pagePath = url.searchParams.get('pagePath')
if (!quizId || !pagePath) {
return new Response(
JSON.stringify({ error: 'Missing required query parameters: quizId and pagePath' }),
{
status: 400,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
}
)
}
// Get user from cookies using LabEx API
const user = await getUserFromCookies(request, env)
if (!user || !user.id) {
return new Response(
JSON.stringify({ error: 'Unauthorized' }),
{
status: 401,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
}
)
}
const userId = user.id
const normalizedPath = normalizePathToEnglish(pagePath)
// Generate unique key for user completion status with pythoncheatsheet prefix
// Format: pythoncheatsheet:user-quiz:${userId}:${normalizedPath}:${quizId}
const key = `pythoncheatsheet:user-quiz:${userId}:${normalizedPath}:${quizId}`
const completedStr = await env.PYTHONCHEATSHEET_QUIZ_KV.get(key)
const completed = completedStr === 'true'
return new Response(
JSON.stringify({
success: true,
quizId,
pagePath: normalizedPath,
userId,
completed,
}),
{
status: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
'Cache-Control': 'private, no-cache, must-revalidate',
},
}
)
} catch (error) {
console.error('Error fetching user quiz status:', error)
return new Response(
JSON.stringify({ error: 'Internal server error' }),
{
status: 500,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Cache-Control': 'no-store, no-cache, must-revalidate',
},
}
)
}
}
async function handleUserMe(request: Request, env: { PYTHONCHEATSHEET_QUIZ_KV: KVNamespace }): Promise<Response> {
try {
const user = await getUserFromCookies(request, env)
if (!user) {
return new Response(
JSON.stringify({ error: 'Unauthorized' }),
{
status: 401,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': 'true',
},
}
)
}
return new Response(
JSON.stringify({ user }),
{
status: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': 'true',
},
}
)
} catch (error) {
console.error('Error in handleUserMe:', error)
return new Response(
JSON.stringify({ error: 'Internal server error' }),
{
status: 500,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': 'true',
},
}
)
}
}