Add code editor with that executes script on isolator
This commit is contained in:
62
app.py
62
app.py
@@ -1,33 +1,69 @@
|
||||
import os
|
||||
from flask import Flask, render_template
|
||||
from flask import Flask, Response, jsonify, render_template, request
|
||||
import jinja_partials
|
||||
from jinja2_fragments import render_block
|
||||
from flask_htmx import HTMX
|
||||
import minify_html
|
||||
import requests
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config.from_pyfile('config.py')
|
||||
jinja_partials.register_extensions(app)
|
||||
htmx = HTMX(app)
|
||||
|
||||
@app.after_request
|
||||
def response_minify(response):
|
||||
"""
|
||||
minify html response to decrease site traffic
|
||||
"""
|
||||
if response.content_type == u'text/html; charset=utf-8':
|
||||
response.set_data(
|
||||
minify_html.minify(response.get_data(
|
||||
as_text=True), minify_js=True, remove_processing_instructions=True)
|
||||
)
|
||||
API_URL = 'https://isolator.peterstockings.com/execute'
|
||||
|
||||
def map_isolator_response_to_flask_response(response):
|
||||
"""
|
||||
Maps a Node.js response to a Flask response.
|
||||
|
||||
:param nodejs_response: The response from Node.js, expected to be a dictionary
|
||||
with keys 'body', 'headers', and 'status'.
|
||||
:return: Flask Response object
|
||||
"""
|
||||
result = response.get('result', {})
|
||||
body = result.get('body', '')
|
||||
headers = result.get('headers', {})
|
||||
status = result.get('status', 200)
|
||||
|
||||
|
||||
# Convert body to JSON if it's a dictionary
|
||||
body = str(body)
|
||||
|
||||
return Response(response=body, status=status, headers=headers)
|
||||
|
||||
return response
|
||||
return response
|
||||
|
||||
@ app.route("/", methods=["GET"])
|
||||
def index():
|
||||
return render_template("index.html")
|
||||
|
||||
|
||||
@app.route('/execute', methods=['POST'])
|
||||
def execute_code():
|
||||
try:
|
||||
# Extract code and convert request to a format acceptable by Node.js app
|
||||
code = request.json.get('code')
|
||||
request_obj = {
|
||||
'method': request.method,
|
||||
'headers': dict(request.headers),
|
||||
'body': request.json,
|
||||
'url': request.url,
|
||||
# Add other request properties as needed
|
||||
}
|
||||
|
||||
# Call the Node.js API
|
||||
response = requests.post(API_URL, json={'code': code, 'request': request_obj})
|
||||
response_data = response.json()
|
||||
|
||||
# Map the Node.js response to Flask response
|
||||
flask_response = map_isolator_response_to_flask_response(response_data)
|
||||
return flask_response
|
||||
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Bind to PORT if defined, otherwise default to 5000.
|
||||
port = int(os.environ.get('PORT', 5000))
|
||||
|
||||
Reference in New Issue
Block a user