forked from shantanupanda/ws_web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
40 lines (31 loc) · 1000 Bytes
/
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
import requests
import os
import json
from flask import Flask, request, jsonify
app = Flask(__name__)
app.debug = True
def request_wants_json():
best = request.accept_mimetypes \
.best_match(['application/json', 'text/html'])
return best == 'application/json' and \
request.accept_mimetypes[best] > \
request.accept_mimetypes['text/html']
def counter_tpl(counter):
alot = ''
if counter > 10 ** 9:
alot = '<img src="http://cdn.thewritepractice.com/wp-content/uploads/2012/05/Alot-vs-a-lot1-600x450.png" />'
return """<html>
<h1>page views</h1>
%s
<h2>%s</h2>
</html>""" % (alot, counter)
@app.route("/")
def index():
count_resp = requests.get('http://api:5002/').text
counter = int(json.loads(count_resp).get("counter", -1))
#counter *= 5 * 10 ** 8
if request_wants_json():
return jsonify(counter=counter)
return counter_tpl(counter)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5001)