1
- import logging as log
2
1
import os
3
2
4
3
import psycopg2
5
4
import redis
6
5
from flask import Flask , render_template_string
7
6
8
7
# HTML Jinja2 Template which will be shown in the browser
9
- page_template = '''
8
+ page_template = """
10
9
<div style="margin: auto; text-align: center;">
11
10
<h1>{{ welcome_text }}</h1><br>
12
11
<h2>This is a change :)</h2>
17
16
{%- endfor %}
18
17
</ul>
19
18
</div>
20
- '''
19
+ """
21
20
22
21
# Defining the Flask Web App
23
22
app = Flask (__name__ )
24
- cache = redis .StrictRedis (host = ' cache' , port = 6379 )
23
+ cache = redis .StrictRedis (host = " cache" , port = 6379 )
25
24
26
25
27
26
# The website root will show the page_template rendered with
28
27
# - visitor count fetched from Redis Cache
29
28
# - list of food fetched from Postgres DB
30
29
# - welcome text passed in as environment variable
31
- @app .route ('/' )
30
+ @app .route ("/" )
32
31
def root ():
33
32
visitors = cache_get_visitor_count ()
34
33
food = db_get_squirrel_food ()
35
34
36
- return render_template_string (page_template , visitors = visitors , foods = food , welcome_text = os .getenv ("WELCOME" , "Hey Acorn user!" ))
35
+ return render_template_string (
36
+ page_template ,
37
+ visitors = visitors ,
38
+ foods = food ,
39
+ welcome_text = os .getenv ("WELCOME" , "Hey Acorn user!" ),
40
+ )
37
41
38
42
39
43
# Fetch the squirrel food from the Postgres database
40
44
def db_get_squirrel_food ():
41
45
conn = psycopg2 .connect (
42
46
host = "db" ,
43
47
database = "acorn" ,
44
- user = os .environ [' PG_USER' ],
45
- password = os .environ [' PG_PASS' ],
48
+ user = os .environ [" PG_USER" ],
49
+ password = os .environ [" PG_PASS" ],
46
50
)
47
51
48
52
cur = conn .cursor ()
@@ -53,4 +57,4 @@ def db_get_squirrel_food():
53
57
54
58
# Increment the visitor count in the Redis cache and return the new value
55
59
def cache_get_visitor_count ():
56
- return cache .incr (' visitors' )
60
+ return cache .incr (" visitors" )
0 commit comments