Refactor solution

This commit is contained in:
Peter Stockings
2023-12-16 15:35:48 +11:00
parent 3545491b88
commit 6dda461404
5 changed files with 306 additions and 97 deletions

33
app.py
View File

@@ -10,7 +10,28 @@ app.config.from_pyfile('config.py')
jinja_partials.register_extensions(app)
htmx = HTMX(app)
API_URL = 'https://isolator.peterstockings.com/execute'
API_URL = 'https://isolator.peterstockings.com/execute'
DEFAULT_SCRIPT = """async (req) => {
console.log('hello world...')
return HtmlResponse(
`<div>
<h1>Method:${req.method}</h1>
<h3>Headers</h3>
<table>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
${Object.entries(req.headers).map(([key, value]) =>
`<tr>
<td>${key}</td>
<td>${value}</td>
</tr>`)}
</table>
</div>`)
}
"""
def map_isolator_response_to_flask_response(response):
"""
@@ -34,7 +55,11 @@ def map_isolator_response_to_flask_response(response):
@ app.route("/", methods=["GET"])
def index():
return render_template("index.html")
return render_template("home.html", script=DEFAULT_SCRIPT)
@ app.route("/client", methods=["GET"])
def client():
return render_template("client.html")
@app.route('/execute', methods=['POST'])
@@ -54,6 +79,10 @@ def execute_code():
response = requests.post(API_URL, json={'code': code, 'request': request_obj})
response_data = response.json()
# check if playground=true is in the query string
if request.args.get('playground') == 'true':
return response_data
# Map the Node.js response to Flask response
flask_response = map_isolator_response_to_flask_response(response_data)
return flask_response