diff --git a/app.py b/app.py index aec324b..a6ff3a7 100644 --- a/app.py +++ b/app.py @@ -106,7 +106,7 @@ def dashboard_http_functions(): @ app.route("/dashboard/http_functions/add_form", methods=["GET"]) @login_required def get_http_function_add_form(): - return render_template("dashboard/http_functions/new.html", name=DEFAULT_FUNCTION_NAME, script=DEFAULT_SCRIPT, environment_info=DEFAULT_ENVIRONMENT, is_public=False) + return render_template("dashboard/http_functions/new.html", name=DEFAULT_FUNCTION_NAME, script=DEFAULT_SCRIPT, environment_info=DEFAULT_ENVIRONMENT, is_public=False, log_request=True, log_response=False) @ app.route("/dashboard/http_functions/create", methods=["POST"]) @login_required @@ -117,8 +117,10 @@ def create_http_function(): script_content = request.json.get('script_content') environment_info = json.dumps(eval(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') - db.create_new_http_function(user_id, name, script_content, environment_info, is_public) + db.create_new_http_function(user_id, name, script_content, environment_info, is_public, log_request, log_response) http_functions = db.get_http_functions_for_user(user_id) http_functions = create_http_functions_view_model(http_functions) @@ -138,7 +140,9 @@ def get_http_function_edit_form(): script = http_function['script_content'] environment_info = json.dumps(http_function['environment_info'], indent=2) is_public = http_function['is_public'] - return render_template("dashboard/http_functions/edit.html", user_id=user_id, name=name, script=script, environment_info=environment_info, is_public=is_public) + log_request = http_function['log_request'] + log_response = http_function['log_response'] + return render_template("dashboard/http_functions/edit.html", user_id=user_id, name=name, script=script, environment_info=environment_info, is_public=is_public, log_request=log_request, log_response=log_response) @ app.route("/dashboard/http_functions/edit", methods=["POST"]) @login_required @@ -149,8 +153,10 @@ def edit_http_function(): script_content = request.json.get('script_content') environment_info = json.dumps(eval(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') - db.edit_http_function(user_id, name, script_content, environment_info, is_public) + db.edit_http_function(user_id, name, script_content, environment_info, is_public, log_request, log_response) return { "status": "success", "message": f'{name} updated' } except Exception as e: print(e) @@ -231,6 +237,8 @@ def execute_http_function(user_id, function): code = http_function['script_content'] environment = http_function['environment_info'] is_public = http_function['is_public'] + log_request = http_function['log_request'] + log_response = http_function['log_response'] # Check if the function is public, if not check if the user is authenticated and owns the function if not is_public: @@ -268,7 +276,12 @@ def execute_http_function(user_id, function): response_data = response.json() db.update_http_function_environment_info_and_invoked_count(user_id, function, response_data['environment']) - db.add_http_function_invocation(http_function['id'], response_data['status'], request_data, response_data['result'], response_data['logs']) + db.add_http_function_invocation( + http_function['id'], + response_data['status'], + request_data if log_request else {}, + response_data['result'] if log_response else {}, + response_data['logs']) # Map the Node.js response to Flask response flask_response = map_isolator_response_to_flask_response(response_data) diff --git a/db.py b/db.py index 2f8d42e..024ef78 100644 --- a/db.py +++ b/db.py @@ -49,25 +49,25 @@ class DataBase(): def get_http_functions_for_user(self, user_id): http_functions = self.execute( - 'SELECT id, user_id, NAME, script_content, invoked_count, environment_info, is_public FROM http_functions WHERE user_id=%s ORDER by id DESC', [user_id]) + 'SELECT id, user_id, NAME, script_content, invoked_count, environment_info, is_public, log_request, log_response FROM http_functions WHERE user_id=%s ORDER by id DESC', [user_id]) return http_functions def get_http_function(self, user_id, name): http_function = self.execute( - 'SELECT id, user_id, NAME, script_content, invoked_count, environment_info, is_public FROM http_functions WHERE user_id=%s AND NAME=%s', [user_id, name], one=True) + 'SELECT id, user_id, NAME, script_content, invoked_count, environment_info, is_public, log_request, log_response FROM http_functions WHERE user_id=%s AND NAME=%s', [user_id, name], one=True) return http_function - def create_new_http_function(self, user_id, name, script_content, environment_info, is_public=False): + def create_new_http_function(self, user_id, name, script_content, environment_info, is_public, log_request, log_response): self.execute( - 'INSERT INTO http_functions (user_id, NAME, script_content, environment_info, is_public) VALUES (%s, %s, %s, %s, %s)', - [user_id, name, script_content, environment_info, is_public], + 'INSERT INTO http_functions (user_id, NAME, script_content, environment_info, is_public, log_request, log_response) VALUES (%s, %s, %s, %s, %s, %s, %s)', + [user_id, name, script_content, environment_info, is_public, log_request, log_response], commit=True ) - - def edit_http_function(self, user_id, name, script_content, environment_info, is_public): + + def edit_http_function(self, user_id, name, script_content, environment_info, is_public, log_request, log_response): self.execute( - 'UPDATE http_functions SET script_content=%s, environment_info=%s, is_public=%s WHERE user_id=%s AND NAME=%s', - [script_content, environment_info, is_public, user_id, name], + 'UPDATE http_functions SET script_content=%s, environment_info=%s, is_public=%s, log_request=%s, log_response=%s WHERE user_id=%s AND NAME=%s', + [script_content, environment_info, is_public, log_request, log_response, user_id, name], commit=True ) diff --git a/services.py b/services.py index 4b363fe..5fa94ce 100644 --- a/services.py +++ b/services.py @@ -6,7 +6,9 @@ def create_http_function_view_model(http_function): "script_content": http_function['script_content'], "invoked_count": http_function['invoked_count'], "environment_info": http_function['environment_info'], - "is_public": http_function['is_public'] + "is_public": http_function['is_public'], + "log_request": http_function['log_request'], + "log_response": http_function['log_response'], } return function_view_model diff --git a/templates/dashboard/http_functions/edit.html b/templates/dashboard/http_functions/edit.html index 232bbe7..a78acb3 100644 --- a/templates/dashboard/http_functions/edit.html +++ b/templates/dashboard/http_functions/edit.html @@ -43,4 +43,4 @@ {{ render_partial('function_editor.html', name=name, script=script, environment_info=environment_info, -is_public=is_public, is_edit=True) }} \ No newline at end of file +is_public=is_public, log_request=log_request, log_response=log_response, 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 e5b678b..388df63 100644 --- a/templates/dashboard/http_functions/new.html +++ b/templates/dashboard/http_functions/new.html @@ -9,4 +9,4 @@ {{ render_partial('function_editor.html', name=name, script=script, environment_info=environment_info, -is_public=is_public, is_add=True) }} \ No newline at end of file +is_public=is_public, log_request=log_request, log_response=log_response, is_add=True) }} \ No newline at end of file diff --git a/templates/function_editor.html b/templates/function_editor.html index 26f2c77..f4268b9 100644 --- a/templates/function_editor.html +++ b/templates/function_editor.html @@ -164,6 +164,46 @@ +