-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflask_app.py
58 lines (48 loc) · 1.47 KB
/
flask_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
import yaml
from flask import Flask, render_template, request, jsonify
from ttp import ttp
app = Flask(__name__, static_url_path="/static")
app.json.sort_keys = False
app.config.update(
TITLE="TTP",
SUBTITLE="Playground for TTP templates",
GITHUB="https://github.com/infrastructureAsCode-ch/ttp101/",
data_textarea="RAW",
template_textarea="TTP",
rendered_textarea="JSON",
)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/examples")
def examples():
try:
with open("examples.yaml", "r") as f:
data = yaml.safe_load(f)
resp = jsonify(data)
except Exception as e:
resp = jsonify({"error": f"Error {type(e).__name__}", "msg": str(e)})
resp.status_code = 400
finally:
return resp
@app.route("/rend", methods=["POST"])
def rend():
data = request.get_json()
if not isinstance(data, dict):
resp = jsonify({"error": "Invalid JSON"})
resp.status_code = 400
return resp
ttp_template = data.get("template", "")
raw_data = data.get("data")
try:
parser = ttp(data=raw_data, template=ttp_template)
parser.parse()
output = parser.result(format="json")
resp = jsonify({"result": output})
except Exception as e:
resp = jsonify({"error": f"Error {type(e).__name__}", "msg": str(e)})
resp.status_code = 400
finally:
return resp
if __name__ == "__main__":
app.run(debug=True)