From 5cc29309d5f617e8e9a0bdcfa58138d010c96c43 Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Mon, 18 Dec 2023 15:15:14 +1100 Subject: [PATCH] Add json object for environment info for each function, this is mutable from custom code --- app.py | 24 ++++++++------ db.py | 12 +++---- services.py | 6 ++-- templates/dashboard/http_functions/edit.html | 34 ++++++++++++++++++-- templates/dashboard/http_functions/new.html | 27 +++++++++++++++- 5 files changed, 79 insertions(+), 24 deletions(-) diff --git a/app.py b/app.py index 3c7f296..8b18bb3 100644 --- a/app.py +++ b/app.py @@ -1,3 +1,4 @@ +import json import os from flask import Flask, Response, jsonify, redirect, render_template, request, url_for import jinja_partials @@ -92,12 +93,15 @@ def create_http_function(): try: name = request.json.get('name') script_content = request.json.get('script_content') - db.create_new_http_function(name, script_content) + environment_info = json.dumps(eval(request.json.get('environment_info'))) + + db.create_new_http_function(name, script_content, environment_info) http_functions = db.get_http_functions() http_functions = create_http_functions_view_model(http_functions) return render_template("dashboard/http_functions.html", http_functions=http_functions) except Exception as e: + print(e) return render_template("dashboard/http_functions/new.html", name=name, script=script_content) @ app.route("/dashboard/http_functions/edit_form", methods=["GET"]) @@ -107,19 +111,20 @@ def get_http_function_edit_form(): if not http_function: return jsonify({'error': 'Function not found'}), 404 script = http_function['script_content'] - return render_template("dashboard/http_functions/edit.html", name=name, script=script) + environment_info = json.dumps(http_function['environment_info']) + return render_template("dashboard/http_functions/edit.html", name=name, script=script, environment_info=environment_info) @ app.route("/dashboard/http_functions/edit", methods=["POST"]) def edit_http_function(): try: name = request.json.get('name') script_content = request.json.get('script_content') - db.edit_http_function(name, script_content) + environment_info = json.dumps(eval(request.json.get('environment_info'))) - http_functions = db.get_http_functions() - http_functions = create_http_functions_view_model(http_functions) - return render_template("dashboard/http_functions.html", http_functions=http_functions) + db.edit_http_function(name, script_content, environment_info) + return render_template("dashboard/http_functions/edit.html", name=name, script=script_content, environment_info=environment_info) except Exception as e: + print(e) return render_template("dashboard/http_functions/edit.html", name=name, script=script_content) @ app.route("/dashboard/http_functions/delete", methods=["DELETE"]) @@ -174,10 +179,9 @@ def execute_http_function(function): http_function = db.get_http_function(function) if not http_function: return jsonify({'error': 'Function not found'}), 404 - # TODO: Get code from database based on function name + code = http_function['script_content'] - - print(code) + environment = http_function['environment_info'] request_obj = { 'method': request.method, @@ -203,7 +207,7 @@ def execute_http_function(function): request_obj['text'] = request.data.decode('utf-8') # 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}) response_data = response.json() # Map the Node.js response to Flask response diff --git a/db.py b/db.py index 77335ea..ad57dce 100644 --- a/db.py +++ b/db.py @@ -48,21 +48,21 @@ class DataBase(): def get_http_functions(self): http_functions = self.execute( - 'SELECT id, NAME, script_content, invoked_count FROM http_functions', []) + 'SELECT id, NAME, script_content, invoked_count, environment_info FROM http_functions ORDER by id DESC', []) return http_functions def get_http_function(self, name): http_function = self.execute( - 'SELECT id, NAME, script_content, invoked_count FROM http_functions WHERE NAME=%s', [name], one=True) + 'SELECT id, NAME, script_content, invoked_count, environment_info FROM http_functions WHERE NAME=%s', [name], one=True) return http_function - def create_new_http_function(self, name, script_content): + def create_new_http_function(self, name, script_content, environment_info): self.execute( - 'INSERT INTO http_functions (NAME, script_content) VALUES (%s, %s)', [name, script_content], commit=True) + 'INSERT INTO http_functions (NAME, script_content, environment_info) VALUES (%s, %s, %s)', [name, script_content, environment_info], commit=True) - def edit_http_function(self, name, script_content): + def edit_http_function(self, name, script_content, environment_info): self.execute( - 'UPDATE http_functions SET script_content=%s WHERE NAME=%s', [script_content, name], commit=True) + 'UPDATE http_functions SET script_content=%s, environment_info=%s WHERE NAME=%s', [script_content, environment_info, name], commit=True) def delete_http_function(self, name): self.execute( diff --git a/services.py b/services.py index 6d8f908..5c71bbd 100644 --- a/services.py +++ b/services.py @@ -8,15 +8,13 @@ def create_http_function_view_model(http_function): "name": name, "script_content": http_function['script_content'], "url": f"{base_url}{name}", - "invoke_count": http_function['invoked_count'] + "invoke_count": http_function['invoked_count'], + "environment_info": http_function['environment_info'] } return function_view_model def create_http_functions_view_model(http_functions): - # Base URL for the function invocation - base_url = "https://function.peterstockings.com/f/" - view_model = [] for function in http_functions: function_view_model = create_http_function_view_model(function) diff --git a/templates/dashboard/http_functions/edit.html b/templates/dashboard/http_functions/edit.html index 3dda615..0ccdca4 100644 --- a/templates/dashboard/http_functions/edit.html +++ b/templates/dashboard/http_functions/edit.html @@ -9,9 +9,10 @@
+

Code

+ +
+ +
{{ script }}
+ +
+

Environment

+ + +
+ +
+ +
{{ environment_info }}
+ +
-
+
@@ -154,13 +181,14 @@ document.querySelector('#add-http-function').addEventListener('click', () => { let name = '{{ name }}'; let script_content = editor.getValue().trim(); + let environment_info = editor_environment.getValue().trim(); fetch("{{ url_for('edit_http_function') }}", { method: 'POST', headers: { 'Content-Type': 'application/json', }, - body: JSON.stringify({ name, script_content }), + body: JSON.stringify({ name, script_content, environment_info }), }) .then(response => response.text()) .then(text => { diff --git a/templates/dashboard/http_functions/new.html b/templates/dashboard/http_functions/new.html index 9cf7f9e..d0a9305 100644 --- a/templates/dashboard/http_functions/new.html +++ b/templates/dashboard/http_functions/new.html @@ -10,6 +10,8 @@
+

Code

+ @@ -39,6 +41,28 @@
{{ script }}
+
+

Environment

+ + +
+ +
+ +
{}
+ + +