Skip to content

Commit

Permalink
Separate lambda handler from legacy app
Browse files Browse the repository at this point in the history
  • Loading branch information
hammady committed May 18, 2024
1 parent 0a05283 commit b486139
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
13 changes: 1 addition & 12 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
from flask import Flask, jsonify, render_template, abort
from qusasat import Qusasat
import base64


app = Flask(__name__)
qusasat = Qusasat(categories_file='./data/categories.csv', quotes_file='./data/qusasat.csv')

# Load the only image we have as base64 data in memory
with open('static/paper.png', 'rb') as file:
file_data = file.read()
base64_data = base64.b64encode(file_data)
base64_string = base64_data.decode('utf-8')

def lambda_root(event, context):
return root_html()

@app.route('/')
def root_html():
global base64_string
quote = qusasat.get_random_quote()
return render_template('quote.html',
category=quote['category'],
quote=quote['quote'],
background_image_url=f'data:image/png;base64,{base64_string}')
background_image_url='/static/paper.png')

@app.route('/.json')
def root_json():
Expand Down
19 changes: 19 additions & 0 deletions handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from flask import render_template
from qusasat import Qusasat
import base64

# Load data
qusasat = Qusasat(categories_file='./data/categories.csv', quotes_file='./data/qusasat.csv')
# Load the only image we have as base64 data in memory
with open('static/paper.png', 'rb') as file:
file_data = file.read()
base64_data = base64.b64encode(file_data)
base64_string = base64_data.decode('utf-8')

def run(event, context):
global qusasat, base64_string
quote = qusasat.get_random_quote()
return render_template('quote.html',
category=quote['category'],
quote=quote['quote'],
background_image_url=f'data:image/png;base64,{base64_string}')
6 changes: 5 additions & 1 deletion serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ provider:
runtime: python3.9
region: us-east-1
deploymentMethod: direct
environment:
TELEGRAM_BOT_TOKEN: ${env:TELEGRAM_BOT_TOKEN}
TELEGRAM_CHAT_ID: ${env:TELEGRAM_CHAT_ID}
MESSAGES_SIGNATURE: ${env:MESSAGES_SIGNATURE}
httpApi:
cors: true
logs:
Expand All @@ -21,7 +25,7 @@ functions:
rate: cron(0 17 * * ? *)
enabled: true
httpHandler:
handler: app.lambda_root
handler: handler.run
events:
- httpApi:
path: /
Expand Down

0 comments on commit b486139

Please sign in to comment.