-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
60 lines (49 loc) · 1.54 KB
/
app.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
51
52
53
54
55
56
57
58
59
60
import os
import socket
import sys
import time
from flask import Flask, abort, json, request
from markupsafe import escape
from redis import Redis, RedisError
from fix_proxy import add_wsgi_proxy
# Connect to Redis
redis = Redis(host="redis", db=0)
app = Flask(__name__)
# Wrapping app.wsgi_app instead of app means that app still points at your Flask application, not at the middleware.
app.wsgi_app = add_wsgi_proxy(app.wsgi_app)
@app.route("/")
def hello(more=""):
info = json.dumps(request.environ, indent=2, default=repr)
try:
visits = redis.incr("counter")
except RedisError:
visits = "<i>cannot connect to Redis, counter disabled</i>"
tmpl = (
"<h3>Hello {name}!</h3>"
"<b>Hostname:</b> {hostname}<br/>"
"<b>Path:</b> /{more}<br/>"
"<b>Visits:</b> {visits}<br/>"
"<b>File:</b> {file}<br/>"
"<b>Modified:</b> {date}<br/>"
"<b>Python:</b> {version}<br/>"
"<b>Environ:</b> <pre>{environ}</pre>"
)
return tmpl.format(
name=os.getenv("APP_NAME", "World"),
hostname=socket.gethostname(),
more=escape(more),
visits=visits,
file=__file__,
date=time.ctime(os.path.getmtime(__file__)),
version=escape(sys.version),
environ=escape(info),
)
@app.route("/<path:more>")
def other(more):
if more.endswith("favicon.ico"):
abort(404)
return hello(more)
if __name__ == "__main__":
app.run(
host=os.getenv("FLASK_HOST", "0.0.0.0"), port=int(os.getenv("FLASK_PORT", 5080))
)