Add support to switch between deno and nodejs function executor
This commit is contained in:
14
app.py
14
app.py
@@ -46,7 +46,8 @@ app.register_blueprint(auth, url_prefix='/auth')
|
||||
|
||||
# Swith to inter app routing, which results in speed up from ~400ms to ~270ms
|
||||
# https://stackoverflow.com/questions/76886643/linking-two-not-exposed-dokku-apps
|
||||
API_URL = os.environ.get('API_URL', 'http://isolator.web:5000/execute') # 'https://isolator.peterstockings.com/execute' 'http://127.0.0.1:5000/execute'
|
||||
NODE_API_URL = os.environ.get('NODE_API_URL', 'http://isolator.web:5000/execute')
|
||||
DENO_API_URL = os.environ.get('DENO_API_URL', 'http://deno-isolator.web:5000/execute')
|
||||
|
||||
|
||||
def map_isolator_response_to_flask_response(response):
|
||||
@@ -82,6 +83,9 @@ async def execute_code():
|
||||
try:
|
||||
# Extract code and convert request to a format acceptable by Node.js app
|
||||
code = request.json.get('code')
|
||||
runtime = request.json.get('runtime', 'node') # Default to node
|
||||
api_url = DENO_API_URL if runtime == 'deno' else NODE_API_URL
|
||||
|
||||
request_obj = {
|
||||
'method': request.method,
|
||||
'headers': dict(request.headers),
|
||||
@@ -92,9 +96,9 @@ async def execute_code():
|
||||
environment = request.json.get('environment_info')
|
||||
environment_json = json.loads(environment)
|
||||
|
||||
# Call the Node.js API asynchronously
|
||||
# Call the selected isolator API asynchronously
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.post(API_URL, json={'code': code, 'request': request_obj, 'environment': environment_json, 'name': "anonymous"}) as response:
|
||||
async with session.post(api_url, json={'code': code, 'request': request_obj, 'environment': environment_json, 'name': "anonymous"}) as response:
|
||||
response_data = await response.json()
|
||||
|
||||
# check if playground=true is in the query string
|
||||
@@ -122,6 +126,7 @@ async def execute_http_function(user_id, function):
|
||||
|
||||
code = http_function['script_content']
|
||||
environment_info = http_function['environment_info']
|
||||
runtime = http_function.get('runtime', 'node') # Default to node
|
||||
|
||||
# Ensure environment is a dictionary
|
||||
if isinstance(environment_info, str) and environment_info:
|
||||
@@ -176,8 +181,9 @@ async def execute_http_function(user_id, function):
|
||||
request_data['text'] = request.data.decode('utf-8')
|
||||
|
||||
# Call the Node.js API asynchronously
|
||||
api_url = DENO_API_URL if runtime == 'deno' else NODE_API_URL
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.post(API_URL, json={'code': code, 'request': request_data, 'environment': environment, 'name': function_name}) as response:
|
||||
async with session.post(api_url, json={'code': code, 'request': request_data, 'environment': environment, 'name': function_name}) as response:
|
||||
response_data = await response.json()
|
||||
|
||||
db.update_http_function_environment_info_and_invoked_count(user_id, function_name, response_data['environment'])
|
||||
|
||||
Reference in New Issue
Block a user