-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
39 lines (29 loc) · 1.28 KB
/
app.py
File metadata and controls
39 lines (29 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from flask import Flask
from redis import Redis, RedisError
import os
import socket
# Connect to Redis
redis=Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)
app = Flask(__name__)
@app.route("/")
def hello():
try:
# Increment counter on web page access / refresh
visits = redis.incr("counter")
except RedisError:
# If redis database is not connected below message will appear on the web page
visits = "<i>Can not connect to Redis, counter disabled</i>"
html = "<body bgcolor={bg_color}>" \
"<h3> {name} ! </h3>" \
"<b>Hostname:</b> {hostname}<br/>" \
"<b>Visits:</b> {visits}" \
"</body>"
# Environment properties
# bg_color -> Background color of webpage
# name -> User defined text that will come on the web page
# "hostname" extracted from system where application runs
# "visits" keeps track of counter to display number of times page is accessed
return html.format(bg_color=os.getenv("BGCOLOR","Green"),name=os.getenv("NAME","Hello Docker world"), hostname=socket.gethostname(),visits=visits)
# "port" on which application can be accessed. Make sure on local machine this port 9000 is available
if __name__ == "__main__":
app.run(host='0.0.0.0', port=9000)