-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_handler.py
More file actions
58 lines (50 loc) · 1.55 KB
/
lambda_handler.py
File metadata and controls
58 lines (50 loc) · 1.55 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
import json
import os
import traceback
import main
def lambda_handler(event, context):
body = {}
try:
# If GET and path is /
if event['path'] == "/" and event['httpMethod'] == "GET":
body.update(main.info())
# if POST
elif event['httpMethod'] == 'POST':
# if /start
if event['path'] == "/start":
game_state = json.loads(event['body'])
main.start(game_state)
# if move
elif event['path'] == "/move":
game_state = json.loads(event['body'])
body = main.move(game_state)
# if end
elif event['path'] == "/end":
game_state = json.loads(event['body'])
main.end(game_state)
# if command not recognized
else:
raise Exception("Unknown Command")
return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps(body)
}
# if unknown command
except Exception as e:
json_region = os.environ['AWS_REGION']
return {
"statusCode": 404,
"headers": {
"Content-Type": "application/json"
},
'body': json.dumps({
"error": str(e),
"errorTraceback": traceback.format_exc(),
"Region": json_region,
"Path": event.get('path', 'unknown'),
"event": event,
})
}