From b4861394bda344de107d9c16212ad902cd66e398 Mon Sep 17 00:00:00 2001 From: Hossam Hammady Date: Sat, 18 May 2024 12:10:34 -0400 Subject: [PATCH] Separate lambda handler from legacy app --- app.py | 13 +------------ handler.py | 19 +++++++++++++++++++ serverless.yml | 6 +++++- 3 files changed, 25 insertions(+), 13 deletions(-) create mode 100755 handler.py diff --git a/app.py b/app.py index 0e06a99..9e78c4e 100755 --- a/app.py +++ b/app.py @@ -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(): diff --git a/handler.py b/handler.py new file mode 100755 index 0000000..baa1475 --- /dev/null +++ b/handler.py @@ -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}') diff --git a/serverless.yml b/serverless.yml index 781ec72..028f07a 100644 --- a/serverless.yml +++ b/serverless.yml @@ -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: @@ -21,7 +25,7 @@ functions: rate: cron(0 17 * * ? *) enabled: true httpHandler: - handler: app.lambda_root + handler: handler.run events: - httpApi: path: /