From 6dda461404ebfd8d105016ecae881dd223bc7739 Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Sat, 16 Dec 2023 15:35:48 +1100 Subject: [PATCH] Refactor solution --- app.py | 33 ++++++++- templates/Index.html | 95 ------------------------- templates/base.html | 32 +++++++++ templates/client.html | 156 ++++++++++++++++++++++++++++++++++++++++++ templates/home.html | 87 +++++++++++++++++++++++ 5 files changed, 306 insertions(+), 97 deletions(-) delete mode 100644 templates/Index.html create mode 100644 templates/base.html create mode 100644 templates/client.html create mode 100644 templates/home.html diff --git a/app.py b/app.py index 5d0bd2a..dc3a151 100644 --- a/app.py +++ b/app.py @@ -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( + `
+

Method:${req.method}

+

Headers

+ + + + + + ${Object.entries(req.headers).map(([key, value]) => + ` + + + `)} +
KeyValue
${key}${value}
+
`) +} +""" 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 diff --git a/templates/Index.html b/templates/Index.html deleted file mode 100644 index 5c0b94b..0000000 --- a/templates/Index.html +++ /dev/null @@ -1,95 +0,0 @@ - - - - - - - - Function - - - - - - - - - - - -
-

Function

-
Create your own javascript HTTP handler
- -
async (req) => { - console.log('hello world...') - return HtmlResponse(`

${req.method}

`) - }
- - - -
- -
- - - - \ No newline at end of file diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..d159fab --- /dev/null +++ b/templates/base.html @@ -0,0 +1,32 @@ + + + + + + + + Function + + + + + + + + + + + +
+ + {% block content %} + + {% endblock %} + + \ No newline at end of file diff --git a/templates/client.html b/templates/client.html new file mode 100644 index 0000000..7f4688c --- /dev/null +++ b/templates/client.html @@ -0,0 +1,156 @@ +{% extends 'base.html' %} + +{% block content %} + +
+
+
+
    + + + +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ +
+
{
+
+
}
+
+ + +
+
+
+
+
+
Response +
+
    + + +
+
+
+
+
+
+
+ +
+
{}
+
+ + +
+
+
+
+
+ + + + + + + + +
KeyValue
+
+
+
+
+
+ +{% endblock %} \ No newline at end of file diff --git a/templates/home.html b/templates/home.html new file mode 100644 index 0000000..42facfa --- /dev/null +++ b/templates/home.html @@ -0,0 +1,87 @@ +{% extends 'base.html' %} + +{% block content %} + +

Function

+
Create your own javascript HTTP handler
+ +
{{ script }} +
+ + + +
+ +
+ + + + + + +{% endblock %} \ No newline at end of file