forked from tradebyte/DevOps-Challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello.py
50 lines (41 loc) · 1.3 KB
/
hello.py
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
40
41
42
43
44
45
46
47
48
49
50
import redis
from redis.exceptions import ConnectionError
import tornado.ioloop
import tornado.web
import os
from sys import exit
try:
r = redis.Redis(
host=os.getenv("REDIS_HOST"),
port=int(os.getenv("REDIS_PORT")),
db=int(os.getenv("REDIS_DB")),
)
r.set("counter", 0)
except ConnectionError:
print("Redis server isn't running. Exiting...")
exit()
environment = os.getenv("ENVIRONMENT")
port = int(os.getenv("PORT"))
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render(
"index.html",
dict={"environment": environment, "counter": r.incr("counter", 1)},
)
class Application(tornado.web.Application):
def __init__(self):
handlers = [(r"/", MainHandler)]
settings = {
"template_path": os.path.join(
os.path.dirname(os.path.abspath(__file__)), "templates"
),
"static_path": os.path.join(
os.path.dirname(os.path.abspath(__file__)), "static"
),
}
tornado.web.Application.__init__(self, handlers, **settings)
if __name__ == "__main__":
app = Application()
app.listen(port)
print(f"App running: http://{os.getenv('HOST')}:{int(os.getenv('PORT'))}")
tornado.ioloop.IOLoop.current().start()