-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
58 lines (46 loc) · 1.71 KB
/
app.py
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 boto3
import time
from chalicelib.utils import logger
from chalice import Chalice, Response
from chalice.app import WebsocketEvent
from chalicelib.routes import websocket_handler
from chalicelib.middleware import handle_exceptions
from jinja2 import Environment, FileSystemLoader
app = Chalice(app_name='video-exploratorium-backend')
# Opt-in for experimental WebSocket feature
app.experimental_feature_flags.update([
'WEBSOCKETS'
])
# Initialize the boto3 session
app.websocket_api.session = boto3.Session()
# Set up Jinja2 environment for template rendering
template_env = Environment(loader=FileSystemLoader('chalicelib/templates'))
# WebSocket handlers
@app.on_ws_message()
def message(event: WebsocketEvent):
return websocket_handler(event, app)
@app.on_ws_connect()
def connect(event: WebsocketEvent):
connection_id = event.connection_id
print(f"Websocket Connection established: {connection_id}")
@app.on_ws_disconnect()
def disconnect(event: WebsocketEvent):
connection_id = event.connection_id
print(f"Websocket Connection closed: {connection_id}")
# Middleware for handling exceptions
@app.middleware('all')
def middleware_handler(event, get_response):
return handle_exceptions(event, get_response)
def render_template(template_name, context):
template = template_env.get_template(template_name)
return template.render(context)
# Serve the main index.html
@app.route('/')
def index():
return Response(render_template('index.html', {}), headers={'Content-Type': 'text/html'})
# Serve the about.html (if needed)
@app.route('/about')
def about():
return Response(render_template('about.html', {}), headers={'Content-Type': 'text/html'})
# AWS Lambda handler
lambda_handler = app