Skip to content

Commit

Permalink
Serve background as inline data and serve using AWS Lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
hammady committed May 18, 2024
1 parent 47f720f commit 3a0e4a4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
16 changes: 15 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down
10 changes: 10 additions & 0 deletions 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
httpApi:
cors: true
logs:
httpApi: true

functions:
cronHandler:
Expand All @@ -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:
Expand Down
13 changes: 12 additions & 1 deletion templates/quote.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
<html>
<head>
<meta charset="utf-8"></meta>
<title>Qusasat</title>
<style>
.main {
height: 300px;
width:100%;
background-color:#FFFFFF;
color:#000000;
background-image: url('{{ background_image_url }}');
background-repeat: repeat;
}
</style>
</head>
<body>
<center>
<div style="height:300px; width:100%; background-color:#FFFFFF; color:#000000; background-image:url('/static/paper.png')">
<div class="main">
<font size="4">
<br/>
<b><i><center>
Expand Down

0 comments on commit 3a0e4a4

Please sign in to comment.