From 3a0e4a4300ad6c8ad16efdf5d615d65e0383d57f Mon Sep 17 00:00:00 2001 From: Hossam Hammady Date: Sat, 18 May 2024 11:24:00 -0400 Subject: [PATCH] Serve background as inline data and serve using AWS Lambda --- app.py | 16 +++++++++++++++- serverless.yml | 10 ++++++++++ templates/quote.html | 13 ++++++++++++- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index 8939036..0e06a99 100755 --- a/app.py +++ b/app.py @@ -1,14 +1,28 @@ 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']) + return render_template('quote.html', + category=quote['category'], + quote=quote['quote'], + background_image_url=f'data:image/png;base64,{base64_string}') @app.route('/.json') def root_json(): diff --git a/serverless.yml b/serverless.yml index 776d69e..9e169ee 100644 --- a/serverless.yml +++ b/serverless.yml @@ -7,6 +7,10 @@ provider: runtime: python3.9 region: us-east-1 deploymentMethod: direct + httpApi: + cors: true + logs: + httpApi: true functions: cronHandler: @@ -16,6 +20,12 @@ functions: # Everyday at 5pm UTC rate: cron(0 17 * * ? *) enabled: true + httpHandler: + handler: app.lambda_root + events: + - httpApi: + path: / + method: 'get' # Install nodejs 19 then run: npm install plugins: diff --git a/templates/quote.html b/templates/quote.html index 9589e33..e1296c7 100644 --- a/templates/quote.html +++ b/templates/quote.html @@ -1,10 +1,21 @@ + Qusasat +
-
+