Add json object for environment info for each function, this is mutable from custom code
This commit is contained in:
24
app.py
24
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
|
||||
|
||||
12
db.py
12
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(
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -9,9 +9,10 @@
|
||||
|
||||
<div>
|
||||
<div class="flex space-x-2 p-2 border-b border-gray-200 dark:border-gray-800">
|
||||
<h1 class="font-semibold text-lg text-gray-400 font-mono flex items-center" data-id="52">Code</h1>
|
||||
|
||||
<button
|
||||
class="inline-flex items-center justify-center rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 hover:bg-accent hover:text-accent-foreground h-10 px-4 py-2 text-gray-600 dark:text-gray-400"
|
||||
class="inline-flex items-center justify-center rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 hover:bg-accent hover:text-accent-foreground h-10 px-4 py-2 text-gray-600 dark:text-gray-400 justify-between"
|
||||
id="executeBtn">
|
||||
<span class="sr-only" data-id="4">Bold</span>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
|
||||
@@ -32,10 +33,36 @@
|
||||
</svg>
|
||||
</button>
|
||||
|
||||
<!-- Spacer Div -->
|
||||
<div class="flex-auto"></div>
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
<div id="editor" class="relative rounded-lg shadow-lg p-4 mb-4">{{ script }}</div>
|
||||
|
||||
|
||||
<div class="flex space-x-2 p-2 border-b border-gray-200 dark:border-gray-800 justify-between">
|
||||
<h1 class="font-semibold text-lg text-gray-400 font-mono flex items-center" data-id="52">Environment</h1>
|
||||
|
||||
<!-- Spacer Div -->
|
||||
<div class="flex-auto"></div>
|
||||
|
||||
</div>
|
||||
|
||||
<div id="editor-environment" class="relative rounded-lg shadow-lg p-4 mb-2">{{ environment_info }}</div>
|
||||
<script>
|
||||
var editor_environment = ace.edit("editor-environment");
|
||||
editor_environment.setOptions({
|
||||
maxLines: 15, //editor_environment.session.getLength()
|
||||
minLines: 5
|
||||
});
|
||||
editor_environment.setTheme("ace/theme/github_dark");
|
||||
editor_environment.session.setMode("ace/mode/json");
|
||||
|
||||
//editor_environment.session.$worker.send("changeOptions", [{ asi: true }]);
|
||||
</script>
|
||||
|
||||
<div class="flex">
|
||||
<button
|
||||
class="bg-transparent hover:bg-blue-500 text-blue-700 font-semibold hover:text-white py-2 px-4 border border-blue-500 hover:border-transparent rounded flex mr-2"
|
||||
@@ -60,9 +87,9 @@
|
||||
|
||||
<span>Delete</span>
|
||||
</button>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
<div id="output">
|
||||
@@ -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 => {
|
||||
|
||||
@@ -10,6 +10,8 @@
|
||||
|
||||
<div>
|
||||
<div class="flex space-x-2 p-2 border-b border-gray-200 dark:border-gray-800">
|
||||
<h1 class="font-semibold text-lg text-gray-400 font-mono flex items-center" data-id="52">Code</h1>
|
||||
|
||||
<input type="text" id="function-name"
|
||||
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500 max-w-fit"
|
||||
placeholder="foo" required="" value="{{ name }}">
|
||||
@@ -39,6 +41,28 @@
|
||||
|
||||
<div id="editor" class="relative rounded-lg shadow-lg p-4 mb-4">{{ script }}</div>
|
||||
|
||||
<div class="flex space-x-2 p-2 border-b border-gray-200 dark:border-gray-800 justify-between">
|
||||
<h1 class="font-semibold text-lg text-gray-400 font-mono flex items-center" data-id="52">Environment</h1>
|
||||
|
||||
<!-- Spacer Div -->
|
||||
<div class="flex-auto"></div>
|
||||
|
||||
</div>
|
||||
|
||||
<div id="editor-environment" class="relative rounded-lg shadow-lg p-4 mb-2">{}</div>
|
||||
<script>
|
||||
var editor_environment = ace.edit("editor-environment");
|
||||
editor_environment.setOptions({
|
||||
maxLines: 15, //editor_environment.session.getLength()
|
||||
minLines: 5
|
||||
});
|
||||
editor_environment.setTheme("ace/theme/github_dark");
|
||||
editor_environment.session.setMode("ace/mode/json");
|
||||
|
||||
//editor_environment.session.$worker.send("changeOptions", [{ asi: true }]);
|
||||
</script>
|
||||
|
||||
|
||||
<button
|
||||
class="bg-transparent hover:bg-blue-500 text-blue-700 font-semibold hover:text-white py-2 px-4 border border-blue-500 hover:border-transparent rounded flex"
|
||||
id="add-http-function">
|
||||
@@ -141,13 +165,14 @@
|
||||
document.querySelector('#add-http-function').addEventListener('click', () => {
|
||||
let name = document.querySelector('#function-name').value;
|
||||
let script_content = editor.getValue().trim();
|
||||
let environment_info = editor_environment.getValue().trim();
|
||||
|
||||
fetch("{{ url_for('create_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 => {
|
||||
|
||||
Reference in New Issue
Block a user