Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

change: optimize image build time in getting-started guide #8

Merged
merged 1 commit into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/flask/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ FROM python:3-slim
WORKDIR /app
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
COPY . .
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["flask", "run"]
22 changes: 13 additions & 9 deletions docs/flask/app.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import logging as log
import os

import psycopg2
import redis
from flask import Flask, render_template_string

# HTML Jinja2 Template which will be shown in the browser
page_template = '''
page_template = """
<div style="margin: auto; text-align: center;">
<h1>{{ welcome_text }}</h1><br>
<h2>This is a change :)</h2>
Expand All @@ -17,32 +16,37 @@
{%- endfor %}
</ul>
</div>
'''
"""

# Defining the Flask Web App
app = Flask(__name__)
cache = redis.StrictRedis(host='cache', port=6379)
cache = redis.StrictRedis(host="cache", port=6379)


# The website root will show the page_template rendered with
# - visitor count fetched from Redis Cache
# - list of food fetched from Postgres DB
# - welcome text passed in as environment variable
@app.route('/')
@app.route("/")
def root():
visitors = cache_get_visitor_count()
food = db_get_squirrel_food()

return render_template_string(page_template, visitors=visitors, foods=food, welcome_text=os.getenv("WELCOME", "Hey Acorn user!"))
return render_template_string(
page_template,
visitors=visitors,
foods=food,
welcome_text=os.getenv("WELCOME", "Hey Acorn user!"),
)


# Fetch the squirrel food from the Postgres database
def db_get_squirrel_food():
conn = psycopg2.connect(
host="db",
database="acorn",
user=os.environ['PG_USER'],
password=os.environ['PG_PASS'],
user=os.environ["PG_USER"],
password=os.environ["PG_PASS"],
)

cur = conn.cursor()
Expand All @@ -53,4 +57,4 @@ def db_get_squirrel_food():

# Increment the visitor count in the Redis cache and return the new value
def cache_get_visitor_count():
return cache.incr('visitors')
return cache.incr("visitors")