Update http functions overview page to show functions in searchable nested directory
This commit is contained in:
278
routes/http.py
278
routes/http.py
@@ -7,7 +7,7 @@ from datetime import datetime, timezone, timedelta
|
||||
import json
|
||||
from services import create_http_function_view_model, create_http_functions_view_model
|
||||
|
||||
'''
|
||||
"""
|
||||
CREATE TABLE http_function_versions (
|
||||
id SERIAL PRIMARY KEY,
|
||||
http_function_id INT NOT NULL,
|
||||
@@ -66,7 +66,7 @@ AFTER INSERT OR UPDATE
|
||||
ON http_functions
|
||||
FOR EACH ROW
|
||||
EXECUTE PROCEDURE fn_http_functions_versioning();
|
||||
'''
|
||||
"""
|
||||
|
||||
DEFAULT_SCRIPT = """async (req) => {
|
||||
console.log(`Method:${req.method}`)
|
||||
@@ -104,18 +104,52 @@ DEFAULT_PYTHON_SCRIPT = """def main(request, environment):
|
||||
return {"body": "Hello from Python!"}
|
||||
"""
|
||||
|
||||
http = Blueprint('http', __name__)
|
||||
http = Blueprint("http", __name__)
|
||||
|
||||
|
||||
def group_functions_by_path(functions):
|
||||
grouped = {}
|
||||
for function in functions:
|
||||
# Use the explicit path column
|
||||
full_path = function.get("path", "") or ""
|
||||
path_parts = [p for p in full_path.split("/") if p]
|
||||
|
||||
current_level = grouped
|
||||
for part in path_parts:
|
||||
if part not in current_level:
|
||||
current_level[part] = {}
|
||||
current_level = current_level[part]
|
||||
|
||||
function["_is_function"] = True
|
||||
current_level[function["name"]] = function
|
||||
return grouped
|
||||
|
||||
@http.route("/overview", methods=["GET"])
|
||||
@login_required
|
||||
def overview():
|
||||
user_id = current_user.id
|
||||
http_functions = db.get_http_functions_for_user(user_id)
|
||||
http_functions = create_http_functions_view_model(http_functions)
|
||||
if htmx:
|
||||
return render_block(environment, "dashboard/http_functions/overview.html", "page", http_functions=http_functions)
|
||||
return render_template("dashboard/http_functions/overview.html", http_functions=http_functions)
|
||||
search_query = request.args.get("q", "")
|
||||
|
||||
http_functions = db.get_http_functions_for_user(user_id, search_query)
|
||||
http_functions_view_model = create_http_functions_view_model(http_functions)
|
||||
grouped_functions = group_functions_by_path(http_functions_view_model)
|
||||
|
||||
if request.headers.get('HX-Target') == 'function-list':
|
||||
return render_block(environment, "dashboard/http_functions/overview.html", "function_list", http_functions=grouped_functions)
|
||||
|
||||
if htmx:
|
||||
return render_block(
|
||||
environment,
|
||||
"dashboard/http_functions/overview.html",
|
||||
"page",
|
||||
http_functions=grouped_functions,
|
||||
search_query=search_query
|
||||
)
|
||||
return render_template(
|
||||
"dashboard/http_functions/overview.html",
|
||||
http_functions=grouped_functions,
|
||||
search_query=search_query
|
||||
)
|
||||
|
||||
@http.route("/new", methods=["GET", "POST"])
|
||||
@login_required
|
||||
@@ -123,36 +157,52 @@ def new():
|
||||
user_id = current_user.id
|
||||
if request.method == "GET":
|
||||
context = {
|
||||
'user_id': user_id,
|
||||
'name': 'foo',
|
||||
'script': DEFAULT_SCRIPT,
|
||||
'default_python_script': DEFAULT_PYTHON_SCRIPT,
|
||||
'environment_info': DEFAULT_ENVIRONMENT,
|
||||
'is_public': False,
|
||||
'log_request': True,
|
||||
'log_response': False
|
||||
"user_id": user_id,
|
||||
"name": "foo",
|
||||
"path": "",
|
||||
"script": DEFAULT_SCRIPT,
|
||||
"default_python_script": DEFAULT_PYTHON_SCRIPT,
|
||||
"environment_info": DEFAULT_ENVIRONMENT,
|
||||
"is_public": False,
|
||||
"log_request": True,
|
||||
"log_response": False,
|
||||
}
|
||||
if htmx:
|
||||
return render_block(environment, 'dashboard/http_functions/new.html', 'page', **context)
|
||||
return render_block(
|
||||
environment, "dashboard/http_functions/new.html", "page", **context
|
||||
)
|
||||
return render_template("dashboard/http_functions/new.html", **context)
|
||||
try:
|
||||
name = request.json.get('name')
|
||||
script_content = request.json.get('script_content')
|
||||
environment_info = request.json.get('environment_info')
|
||||
is_public = request.json.get('is_public')
|
||||
log_request = request.json.get('log_request')
|
||||
log_response = request.json.get('log_response')
|
||||
runtime = request.json.get('runtime', 'node')
|
||||
|
||||
db.create_new_http_function(user_id, name, script_content, environment_info, is_public, log_request, log_response, runtime)
|
||||
name = request.json.get("name")
|
||||
path = request.json.get("path", "")
|
||||
script_content = request.json.get("script_content")
|
||||
environment_info = request.json.get("environment_info")
|
||||
is_public = request.json.get("is_public")
|
||||
log_request = request.json.get("log_request")
|
||||
log_response = request.json.get("log_response")
|
||||
runtime = request.json.get("runtime", "node")
|
||||
|
||||
return jsonify({
|
||||
"status": "success",
|
||||
"message": "Http function created successfully"
|
||||
}), 200
|
||||
db.create_new_http_function(
|
||||
user_id,
|
||||
name,
|
||||
path,
|
||||
script_content,
|
||||
environment_info,
|
||||
is_public,
|
||||
log_request,
|
||||
log_response,
|
||||
runtime,
|
||||
)
|
||||
|
||||
return (
|
||||
jsonify(
|
||||
{"status": "success", "message": "Http function created successfully"}
|
||||
),
|
||||
200,
|
||||
)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return { "status": "error", "message": str(e) }
|
||||
return {"status": "error", "message": str(e)}
|
||||
|
||||
|
||||
@http.route("/edit/<int:function_id>", methods=["POST"])
|
||||
@@ -160,20 +210,33 @@ def new():
|
||||
def edit(function_id):
|
||||
try:
|
||||
user_id = current_user.id
|
||||
name = request.json.get('name')
|
||||
script_content = request.json.get('script_content')
|
||||
environment_info = request.json.get('environment_info')
|
||||
is_public = request.json.get('is_public')
|
||||
log_request = request.json.get('log_request')
|
||||
log_response = request.json.get('log_response')
|
||||
runtime = request.json.get('runtime', 'node')
|
||||
|
||||
updated_version = db.edit_http_function(user_id, function_id, name, script_content, environment_info, is_public, log_request, log_response, runtime)
|
||||
name = request.json.get("name")
|
||||
path = request.json.get("path", "")
|
||||
script_content = request.json.get("script_content")
|
||||
environment_info = request.json.get("environment_info")
|
||||
is_public = request.json.get("is_public")
|
||||
log_request = request.json.get("log_request")
|
||||
log_response = request.json.get("log_response")
|
||||
runtime = request.json.get("runtime", "node")
|
||||
|
||||
return { "status": "success", "message": f'{name} updated' }
|
||||
updated_version = db.edit_http_function(
|
||||
user_id,
|
||||
function_id,
|
||||
name,
|
||||
path,
|
||||
script_content,
|
||||
environment_info,
|
||||
is_public,
|
||||
log_request,
|
||||
log_response,
|
||||
runtime,
|
||||
)
|
||||
|
||||
return {"status": "success", "message": f"{name} updated"}
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return { "status": "error", "message": str(e) }
|
||||
return {"status": "error", "message": str(e)}
|
||||
|
||||
|
||||
@http.route("/delete/<int:function_id>", methods=["DELETE"])
|
||||
@login_required
|
||||
@@ -182,11 +245,15 @@ def delete(function_id):
|
||||
user_id = current_user.id
|
||||
|
||||
db.execute(
|
||||
'DELETE FROM http_functions WHERE user_id=%s AND id=%s', [user_id, function_id], commit=True)
|
||||
|
||||
return { "status": "success", "message": f'Function deleted' }
|
||||
"DELETE FROM http_functions WHERE user_id=%s AND id=%s",
|
||||
[user_id, function_id],
|
||||
commit=True,
|
||||
)
|
||||
|
||||
return {"status": "success", "message": f"Function deleted"}
|
||||
except Exception as e:
|
||||
return jsonify({"status": 'error', "message": str(e)}), 500
|
||||
return jsonify({"status": "error", "message": str(e)}), 500
|
||||
|
||||
|
||||
@http.route("/logs/<int:function_id>", methods=["GET"])
|
||||
@login_required
|
||||
@@ -194,12 +261,27 @@ def logs(function_id):
|
||||
user_id = current_user.id
|
||||
http_function = db.get_http_function_by_id(user_id, function_id)
|
||||
if not http_function:
|
||||
return jsonify({'error': 'Function not found'}), 404
|
||||
name = http_function['name']
|
||||
return jsonify({"error": "Function not found"}), 404
|
||||
name = http_function["name"]
|
||||
http_function_invocations = db.get_http_function_invocations(function_id)
|
||||
if htmx:
|
||||
return render_block(environment, 'dashboard/http_functions/logs.html', 'page', user_id=user_id, function_id=function_id, name=name, http_function_invocations=http_function_invocations)
|
||||
return render_template("dashboard/http_functions/logs.html", user_id=user_id, name=name, function_id=function_id, http_function_invocations=http_function_invocations)
|
||||
return render_block(
|
||||
environment,
|
||||
"dashboard/http_functions/logs.html",
|
||||
"page",
|
||||
user_id=user_id,
|
||||
function_id=function_id,
|
||||
name=name,
|
||||
http_function_invocations=http_function_invocations,
|
||||
)
|
||||
return render_template(
|
||||
"dashboard/http_functions/logs.html",
|
||||
user_id=user_id,
|
||||
name=name,
|
||||
function_id=function_id,
|
||||
http_function_invocations=http_function_invocations,
|
||||
)
|
||||
|
||||
|
||||
@http.route("/client/<int:function_id>", methods=["GET"])
|
||||
@login_required
|
||||
@@ -207,49 +289,70 @@ def client(function_id):
|
||||
user_id = current_user.id
|
||||
http_function = db.get_http_function_by_id(user_id, function_id)
|
||||
if not http_function:
|
||||
return jsonify({'error': 'Function not found'}), 404
|
||||
|
||||
return jsonify({"error": "Function not found"}), 404
|
||||
|
||||
http_function = create_http_function_view_model(http_function)
|
||||
if htmx:
|
||||
return render_block(environment, 'dashboard/http_functions/client.html', 'page', function_id=function_id, **http_function)
|
||||
return render_template("dashboard/http_functions/client.html", function_id=function_id, **http_function)
|
||||
return render_block(
|
||||
environment,
|
||||
"dashboard/http_functions/client.html",
|
||||
"page",
|
||||
function_id=function_id,
|
||||
**http_function,
|
||||
)
|
||||
return render_template(
|
||||
"dashboard/http_functions/client.html", function_id=function_id, **http_function
|
||||
)
|
||||
|
||||
@http.route('/history/<int:function_id>')
|
||||
|
||||
@http.route("/history/<int:function_id>")
|
||||
@login_required
|
||||
def history(function_id):
|
||||
# Fetch the http function to verify ownership
|
||||
http_function = db.execute("""
|
||||
http_function = db.execute(
|
||||
"""
|
||||
SELECT id, name, script_content AS code, version_number
|
||||
FROM http_functions
|
||||
WHERE id = %s AND user_id = %s
|
||||
""", [function_id, current_user.id], one=True)
|
||||
""",
|
||||
[function_id, current_user.id],
|
||||
one=True,
|
||||
)
|
||||
|
||||
if not http_function:
|
||||
flash('Http function not found', 'error')
|
||||
return redirect(url_for('http.overview'))
|
||||
flash("Http function not found", "error")
|
||||
return redirect(url_for("http.overview"))
|
||||
|
||||
# Fetch all versions
|
||||
versions = db.execute("""
|
||||
versions = db.execute(
|
||||
"""
|
||||
SELECT version_number, script_content AS script, updated_at AS versioned_at
|
||||
FROM http_functions_versions
|
||||
WHERE http_function_id = %s
|
||||
ORDER BY version_number DESC
|
||||
""", [function_id])
|
||||
""",
|
||||
[function_id],
|
||||
)
|
||||
|
||||
# Convert datetime objects to ISO format strings
|
||||
for version in versions:
|
||||
version['versioned_at'] = version['versioned_at'].isoformat() if version['versioned_at'] else None
|
||||
version["versioned_at"] = (
|
||||
version["versioned_at"].isoformat() if version["versioned_at"] else None
|
||||
)
|
||||
|
||||
args = {
|
||||
'user_id': current_user.id,
|
||||
'function_id': function_id,
|
||||
'http_function': http_function,
|
||||
'versions': versions
|
||||
"user_id": current_user.id,
|
||||
"function_id": function_id,
|
||||
"http_function": http_function,
|
||||
"versions": versions,
|
||||
}
|
||||
|
||||
if htmx:
|
||||
return render_block(environment, 'dashboard/http_functions/history.html', 'page', **args)
|
||||
return render_template('dashboard/http_functions/history.html', **args)
|
||||
return render_block(
|
||||
environment, "dashboard/http_functions/history.html", "page", **args
|
||||
)
|
||||
return render_template("dashboard/http_functions/history.html", **args)
|
||||
|
||||
|
||||
@http.route("/editor/<int:function_id>", methods=["GET"])
|
||||
@login_required
|
||||
@@ -257,27 +360,30 @@ def editor(function_id):
|
||||
user_id = current_user.id
|
||||
http_function = db.get_http_function_by_id(user_id, function_id)
|
||||
if not http_function:
|
||||
return jsonify({'error': 'Function not found'}), 404
|
||||
|
||||
return jsonify({"error": "Function not found"}), 404
|
||||
|
||||
# Create a view model with all necessary data for the editor
|
||||
editor_data = {
|
||||
'id': http_function['id'],
|
||||
'name': http_function['name'],
|
||||
'script_content': http_function['script_content'],
|
||||
'environment_info': json.dumps(http_function['environment_info'], indent=2),
|
||||
'is_public': http_function['is_public'],
|
||||
'log_request': http_function['log_request'],
|
||||
'log_response': http_function['log_response'],
|
||||
'version_number': http_function['version_number'],
|
||||
'runtime': http_function.get('runtime', 'node'),
|
||||
'user_id': user_id,
|
||||
'function_id': function_id,
|
||||
"id": http_function["id"],
|
||||
"name": http_function["name"],
|
||||
"path": http_function.get("path", ""),
|
||||
"script_content": http_function["script_content"],
|
||||
"environment_info": json.dumps(http_function["environment_info"], indent=2),
|
||||
"is_public": http_function["is_public"],
|
||||
"log_request": http_function["log_request"],
|
||||
"log_response": http_function["log_response"],
|
||||
"version_number": http_function["version_number"],
|
||||
"runtime": http_function.get("runtime", "node"),
|
||||
"user_id": user_id,
|
||||
"function_id": function_id,
|
||||
# Add new URLs for navigation
|
||||
'cancel_url': url_for('http.overview'),
|
||||
'edit_url': url_for('http.editor', function_id=function_id),
|
||||
"cancel_url": url_for("http.overview"),
|
||||
"edit_url": url_for("http.editor", function_id=function_id),
|
||||
}
|
||||
|
||||
if htmx:
|
||||
return render_block(environment, "dashboard/http_functions/editor.html", "page", **editor_data)
|
||||
|
||||
return render_template("dashboard/http_functions/editor.html", **editor_data)
|
||||
return render_block(
|
||||
environment, "dashboard/http_functions/editor.html", "page", **editor_data
|
||||
)
|
||||
|
||||
return render_template("dashboard/http_functions/editor.html", **editor_data)
|
||||
|
||||
Reference in New Issue
Block a user