diff --git a/app.py b/app.py index 5b54fa8..5bdb812 100644 --- a/app.py +++ b/app.py @@ -4,11 +4,14 @@ import jinja_partials from jinja2_fragments import render_block from flask_htmx import HTMX import requests +from db import DataBase +from services import create_http_functions_view_model app = Flask(__name__) app.config.from_pyfile('config.py') jinja_partials.register_extensions(app) htmx = HTMX(app) +db = DataBase(app) API_URL = 'https://isolator.peterstockings.com/execute' @@ -62,11 +65,15 @@ def client(): @ app.route("/dashboard", methods=["GET"]) def dashboard(): - return render_template("dashboard.html") + http_functions = db.get_http_functions() + http_functions = create_http_functions_view_model(http_functions) + return render_template("dashboard.html", http_functions=http_functions) @ app.route("/dashboard/http_functions", methods=["GET"]) def dashboard_http_functions(): - return render_template("dashboard/http_functions.html") + 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) @ app.route("/dashboard/timer_functions", methods=["GET"]) def dashboard_timer_functions(): @@ -105,8 +112,13 @@ def execute_code(): @app.route('/f/', methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD']) def execute_http_function(function): try: + 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 = DEFAULT_SCRIPT + code = http_function['script_content'] + + print(code) request_obj = { 'method': request.method, diff --git a/db.py b/db.py new file mode 100644 index 0000000..2a15bf8 --- /dev/null +++ b/db.py @@ -0,0 +1,59 @@ +import os +import psycopg2 +import numpy as np +from psycopg2.extras import RealDictCursor +from datetime import datetime +from urllib.parse import urlparse +from flask import g + +class DataBase(): + def __init__(self, app): + db_url = urlparse(os.environ['DATABASE_URL']) + # if db_url is null then throw error + if not db_url: + raise Exception("No DATABASE_URL environment variable set") + + def getDB(self): + db = getattr(g, 'database', None) + if db is None: + db_url = urlparse(os.environ['DATABASE_URL']) + g.database = psycopg2.connect( + database=db_url.path[1:], + user=db_url.username, + password=db_url.password, + host=db_url.hostname, + port=db_url.port + ) + db = g.database + return db + + def close_connection(exception): + db = getattr(g, 'database', None) + if db is not None: + db.close() + + def execute(self, query, args=(), one=False, commit=False): + conn = self.getDB() + cur = conn.cursor(cursor_factory=RealDictCursor) + cur.execute(query, args) + rv = None + if cur.description is not None: + rv = cur.fetchall() + if commit: + try: + conn.commit() + except: + conn.rollback() + cur.close() + + return (rv[0] if rv else None) if one else rv + + def get_http_functions(self): + http_functions = self.execute( + 'SELECT id, NAME, script_content, invoked_count FROM http_functions', []) + 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) + return http_function diff --git a/services.py b/services.py new file mode 100644 index 0000000..ab0274d --- /dev/null +++ b/services.py @@ -0,0 +1,18 @@ +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: + name = function['name'] + function_view_model = { + "id": function['id'], + "name": name, + "script_content": function['script_content'], + "url": f"{base_url}{name}", + "invoke_count": function['invoked_count'] + } + view_model.append(function_view_model) + + return view_model diff --git a/templates/dashboard.html b/templates/dashboard.html index 336da2b..a1953a3 100644 --- a/templates/dashboard.html +++ b/templates/dashboard.html @@ -87,7 +87,7 @@ Toggle user menu
- {{ render_partial('dashboard/http_functions.html') }} + {{ render_partial('dashboard/http_functions.html', http_functions=http_functions) }}
diff --git a/templates/dashboard/http_functions.html b/templates/dashboard/http_functions.html index 7147c95..3007d23 100644 --- a/templates/dashboard/http_functions.html +++ b/templates/dashboard/http_functions.html @@ -20,12 +20,12 @@ + {% for function in http_functions %} HelloWorld + data-id="66">{{ function.name }} - https://function.peterstockings.com/f/helloworld + {{ function.url }} + data-id="70">Delete + + {% endfor %}