-
Notifications
You must be signed in to change notification settings - Fork 0
/
microservice.py
46 lines (35 loc) · 1.21 KB
/
microservice.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
#!/usr/bin/env python
"""Implements Unix Fortune using 3 microservices and an API"""
import sys
from flask import Flask, request, render_template_string
import requests
FETCH_URL = "http://localhost:9001/fetch"
RENDER_URL = "http://localhost:9002/render"
role = sys.argv[1]
app = Flask(role)
@app.route("/")
def main():
fortunes = []
for _ in range(0, 3):
fortunes.append(requests.get(FETCH_URL).content)
return requests.post(RENDER_URL, data={'fortunes': fortunes}).content
@app.route("/fetch")
def fetch():
return requests.get("http://mcscope.com/fortune").content
@app.route("/render", methods=["POST"])
def render():
fortune_template = "<div style='margin:100px;'> {{fortune}} </div>"
fortune_divs = [render_template_string(fortune_template, fortune=fortune)
for fortune in request.form.getlist("fortunes")]
body_template = """
<html>
<body>
<div style="width:50%; margin:auto; margin-top:100px;">
{{"".join(fortune_divs)}}
</div>
</body>
</html>
"""
return render_template_string(body_template, fortune_divs=fortune_divs)
if __name__ == '__main__':
app.run(port=int(sys.argv[2]), debug=True)