Skip to content

Commit e25edab

Browse files
committed
Add CORS handling to lambdas
1 parent e20e713 commit e25edab

File tree

6 files changed

+83
-3
lines changed

6 files changed

+83
-3
lines changed

lambdas/create_quiz/handler.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ def lambda_handler(event, context):
9191
except (KeyError, json.JSONDecodeError, ValueError, TypeError) as e:
9292
return {
9393
'statusCode': 400,
94+
'headers': {
95+
'Access-Control-Allow-Origin': '*',
96+
'Access-Control-Allow-Methods': '*',
97+
},
9498
'body': json.dumps({
9599
'message': 'Invalid input data',
96100
'error': str(e)
@@ -101,6 +105,10 @@ def lambda_handler(event, context):
101105
if not all(k in question for k in ('QuestionText', 'Options', 'CorrectAnswer', 'Trivia')):
102106
return {
103107
'statusCode': 400,
108+
'headers': {
109+
'Access-Control-Allow-Origin': '*',
110+
'Access-Control-Allow-Methods': '*',
111+
},
104112
'body': json.dumps({
105113
'message': 'Each question must contain QuestionText, Options, CorrectAnswer, and Trivia'
106114
})
@@ -138,6 +146,10 @@ def lambda_handler(event, context):
138146
print(f"Failed to publish to SNS: {sns_e}")
139147
return {
140148
'statusCode': 500,
149+
'headers': {
150+
'Access-Control-Allow-Origin': '*',
151+
'Access-Control-Allow-Methods': '*',
152+
},
141153
'body': json.dumps({
142154
'message': 'Error storing quiz data. It has been queued for retry.',
143155
'error': str(e)
@@ -146,5 +158,9 @@ def lambda_handler(event, context):
146158

147159
return {
148160
'statusCode': 200,
161+
'headers': {
162+
'Access-Control-Allow-Origin': '*',
163+
'Access-Control-Allow-Methods': '*',
164+
},
149165
'body': json.dumps({'QuizID': quiz_id})
150166
}

lambdas/get_leaderboard/handler.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ def lambda_handler(event, context):
99
except (KeyError, TypeError, ValueError) as e:
1010
return {
1111
'statusCode': 400,
12+
'headers': {
13+
'Access-Control-Allow-Origin': '*',
14+
'Access-Control-Allow-Methods': '*',
15+
},
1216
'body': json.dumps({'message': 'quiz_id is required and top should be an integer', 'error': str(e)})
1317
}
1418

@@ -32,10 +36,18 @@ def lambda_handler(event, context):
3236
]
3337
return {
3438
'statusCode': 200,
39+
'headers': {
40+
'Access-Control-Allow-Origin': '*',
41+
'Access-Control-Allow-Methods': '*',
42+
},
3543
'body': json.dumps(leaderboard)
3644
}
3745
except Exception as e:
3846
return {
3947
'statusCode': 500,
48+
'headers': {
49+
'Access-Control-Allow-Origin': '*',
50+
'Access-Control-Allow-Methods': '*',
51+
},
4052
'body': json.dumps({'message': 'Error retrieving leaderboard', 'error': str(e)})
4153
}

lambdas/get_quiz/handler.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ def lambda_handler(event, context):
2121
except (KeyError, TypeError) as e:
2222
return {
2323
'statusCode': 400,
24+
'headers': {
25+
'Access-Control-Allow-Origin': '*',
26+
'Access-Control-Allow-Methods': '*',
27+
},
2428
'body': json.dumps({'message': 'quiz_id is required', 'error': str(e)})
2529
}
2630

@@ -35,10 +39,18 @@ def lambda_handler(event, context):
3539
quiz = convert_decimal(quiz)
3640
return {
3741
'statusCode': 200,
42+
'headers': {
43+
'Access-Control-Allow-Origin': '*',
44+
'Access-Control-Allow-Methods': '*',
45+
},
3846
'body': json.dumps(quiz)
3947
}
4048
else:
4149
return {
4250
'statusCode': 404,
51+
'headers': {
52+
'Access-Control-Allow-Origin': '*',
53+
'Access-Control-Allow-Methods': '*',
54+
},
4355
'body': json.dumps({'message': 'Quiz not found'})
4456
}

lambdas/get_submission/handler.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ def lambda_handler(event, context):
2222
except (KeyError, TypeError) as e:
2323
return {
2424
'statusCode': 400,
25-
'headers': {'Content-Type': 'application/json'},
25+
'headers': {
26+
'Content-Type': 'application/json',
27+
'Access-Control-Allow-Origin': '*',
28+
'Access-Control-Allow-Methods': '*',
29+
},
2630
'body': json.dumps({'message': 'submission_id is required', 'error': str(e)})
2731
}
2832

@@ -36,12 +40,20 @@ def lambda_handler(event, context):
3640
submission = convert_decimal(submission)
3741
return {
3842
'statusCode': 200,
39-
'headers': {'Content-Type': 'application/json'},
43+
'headers': {
44+
'Content-Type': 'application/json',
45+
'Access-Control-Allow-Origin': '*',
46+
'Access-Control-Allow-Methods': '*',
47+
},
4048
'body': json.dumps(submission)
4149
}
4250
else:
4351
return {
4452
'statusCode': 404,
45-
'headers': {'Content-Type': 'application/json'},
53+
'headers': {
54+
'Content-Type': 'application/json',
55+
'Access-Control-Allow-Origin': '*',
56+
'Access-Control-Allow-Methods': '*',
57+
},
4658
'body': json.dumps({'message': 'Submission not found'})
4759
}

lambdas/list_quizzes/handler.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,20 @@ def lambda_handler(event, context):
1616

1717
return {
1818
'statusCode': 200,
19+
'headers': {
20+
'Access-Control-Allow-Origin': '*',
21+
'Access-Control-Allow-Methods': '*',
22+
},
1923
'body': json.dumps({'Quizzes': quizzes})
2024
}
2125

2226
except Exception as e:
2327
print(f"Error retrieving public quizzes: {e}")
2428
return {
2529
'statusCode': 500,
30+
'headers': {
31+
'Access-Control-Allow-Origin': '*',
32+
'Access-Control-Allow-Methods': '*',
33+
},
2634
'body': json.dumps({'message': 'Error retrieving public quizzes', 'error': str(e)})
2735
}

lambdas/submit_quiz/handler.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ def lambda_handler(event, context):
2222
except (KeyError, json.JSONDecodeError, ValueError, TypeError) as e:
2323
return {
2424
'statusCode': 400,
25+
'headers': {
26+
'Access-Control-Allow-Origin': '*',
27+
'Access-Control-Allow-Methods': '*',
28+
},
2529
'body': json.dumps({'message': 'Invalid input data', 'error': str(e)})
2630
}
2731

@@ -33,11 +37,19 @@ def lambda_handler(event, context):
3337
if 'Item' not in response:
3438
return {
3539
'statusCode': 400,
40+
'headers': {
41+
'Access-Control-Allow-Origin': '*',
42+
'Access-Control-Allow-Methods': '*',
43+
},
3644
'body': json.dumps({'message': f'QuizID "{quiz_id}" does not exist.'})
3745
}
3846
except Exception as e:
3947
return {
4048
'statusCode': 500,
49+
'headers': {
50+
'Access-Control-Allow-Origin': '*',
51+
'Access-Control-Allow-Methods': '*',
52+
},
4153
'body': json.dumps({'message': 'Error accessing the Quizzes table.', 'error': str(e)})
4254
}
4355

@@ -62,10 +74,18 @@ def lambda_handler(event, context):
6274
except Exception as e:
6375
return {
6476
'statusCode': 500,
77+
'headers': {
78+
'Access-Control-Allow-Origin': '*',
79+
'Access-Control-Allow-Methods': '*',
80+
},
6581
'body': json.dumps({'message': 'Error sending message to SQS.', 'error': str(e)})
6682
}
6783

6884
return {
6985
'statusCode': 200,
86+
'headers': {
87+
'Access-Control-Allow-Origin': '*',
88+
'Access-Control-Allow-Methods': '*',
89+
},
7090
'body': json.dumps({'message': 'Submission received', 'SubmissionID': message_body['SubmissionID']})
7191
}

0 commit comments

Comments
 (0)