From 223afec05d0e80c04937530c6ba5cc78dc3d3a48 Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Tue, 19 Dec 2023 21:20:11 +1100 Subject: [PATCH] Refactor all function editor front end html + js into a single jinja template (also slight improvement to look of response/logs) --- app.py | 32 ++- templates/dashboard/http_functions/edit.html | 188 +-------------- templates/dashboard/http_functions/new.html | 173 +------------- templates/function_editor.html | 228 +++++++++++++++++++ templates/home.html | 126 +--------- 5 files changed, 244 insertions(+), 503 deletions(-) create mode 100644 templates/function_editor.html diff --git a/app.py b/app.py index a89e5ba..def2cb9 100644 --- a/app.py +++ b/app.py @@ -16,27 +16,21 @@ db = DataBase(app) API_URL = 'https://isolator.peterstockings.com/execute' +DEFAULT_FUNCTION_NAME = 'foo' + DEFAULT_SCRIPT = """async (req) => { - console.log('hello world...') + console.log(`Hello ${environment.name}`) return HtmlResponse( `

Method:${req.method}

-

Headers

- - - - - - ${Object.entries(req.headers).map(([key, value]) => - ` - - - `)} -
KeyValue
${key}${value}
-
${JSON.stringify(req.body, null, 2)}
+

Hello ${environment.name}

`) }""" +DEFAULT_ENVIRONMENT = """{ + "name": "Peter" +}""" + def map_isolator_response_to_flask_response(response): """ Maps a Node.js response to a Flask response. @@ -59,7 +53,7 @@ def map_isolator_response_to_flask_response(response): @ app.route("/", methods=["GET"]) def home(): - return render_template("home.html", script=DEFAULT_SCRIPT) + return render_template("home.html", name='Try me', script=DEFAULT_SCRIPT, environment_info=DEFAULT_ENVIRONMENT) @ app.route("/client/", methods=["GET"]) def client(function): @@ -84,9 +78,7 @@ def dashboard_http_functions(): @ app.route("/dashboard/http_functions/add_form", methods=["GET"]) def get_http_function_add_form(): - script=DEFAULT_SCRIPT - name = "foo" - return render_template("dashboard/http_functions/new.html", name=name, script=script) + return render_template("dashboard/http_functions/new.html", name=DEFAULT_FUNCTION_NAME, script=DEFAULT_SCRIPT, environment_info=DEFAULT_ENVIRONMENT) @ app.route("/dashboard/http_functions/create", methods=["POST"]) def create_http_function(): @@ -168,9 +160,11 @@ def execute_code(): 'url': request.url, # Add other request properties as needed } + environment = request.json.get('environment_info') + environment_json = json.loads(environment) # Call the Node.js API - response = requests.post(API_URL, json={'code': code, 'request': request_obj}) + response = requests.post(API_URL, json={'code': code, 'request': request_obj, 'environment': environment_json}) response_data = response.json() # check if playground=true is in the query string diff --git a/templates/dashboard/http_functions/edit.html b/templates/dashboard/http_functions/edit.html index 8712484..14fadf9 100644 --- a/templates/dashboard/http_functions/edit.html +++ b/templates/dashboard/http_functions/edit.html @@ -42,190 +42,4 @@ -
-
-

Code

- - - - - -
- - -
- -
{{ script }}
- - -
-

Environment

- - -
- -
- -
{{ environment_info }}
- - -
- - - -
- - -
- -
- -
- - \ No newline at end of file +{{ render_partial('function_editor.html', name=name, script=script, environment_info=environment_info, is_edit=True) }} \ No newline at end of file diff --git a/templates/dashboard/http_functions/new.html b/templates/dashboard/http_functions/new.html index 1d0639b..d7cacbb 100644 --- a/templates/dashboard/http_functions/new.html +++ b/templates/dashboard/http_functions/new.html @@ -8,175 +8,4 @@ -
-
-

Code

- - - - - -
- -
{{ script }}
- -
-

Environment

- - -
- -
- -
{}
- - - - - -
- -
- -
- - \ No newline at end of file +{{ render_partial('function_editor.html', name=name, script=script, environment_info=environment_info, is_add=True) }} \ No newline at end of file diff --git a/templates/function_editor.html b/templates/function_editor.html new file mode 100644 index 0000000..b8633b9 --- /dev/null +++ b/templates/function_editor.html @@ -0,0 +1,228 @@ +
+
+

Code

+ + {% if is_add|default(false, true) %} + + {% endif %} + + + + + + + + +
+ + +
+ +
{{ script }}
+ + + +
+

Environment

+ + +
+ +
+ +
{{ environment_info }}
+ + +
+ {% if is_edit|default(false, true) %} + + + + {% endif %} + + {% if is_add|default(false, true) %} + + + {% endif %} + +
+ +
+ +
+ +
\ No newline at end of file diff --git a/templates/home.html b/templates/home.html index d9fd4e6..5d1817c 100644 --- a/templates/home.html +++ b/templates/home.html @@ -32,130 +32,6 @@

-
- - -
- -
{{ script }} -
- - -
- -
- - - - +{{ render_partial('function_editor.html', name=name, script=script, environment_info=environment_info) }} {% endblock %} \ No newline at end of file