samarth upadhyay commited on
Commit
0e20d87
·
verified ·
1 Parent(s): 9304b86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -26
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import subprocess
2
  import sys
 
3
  from flask import Flask, request, jsonify
4
  from flask_cors import CORS
5
 
@@ -8,7 +9,7 @@ CORS(app)
8
 
9
  @app.route('/')
10
  def home():
11
- return jsonify({"status": "ok", "message": "API Running (Gunicorn Enabled)"})
12
 
13
  def run_command(command, input_text=None, timeout=15):
14
  """Helper to run shell commands safely"""
@@ -36,8 +37,6 @@ def run_python():
36
 
37
  if not code: return jsonify({'output': 'No code provided'}), 400
38
 
39
- # Run python using -c to avoid temp files
40
- # We pass the code as the argument to -c
41
  cmd = [sys.executable, "-c", code]
42
  output = run_command(cmd, input_text=user_input)
43
  return jsonify({'output': output})
@@ -47,34 +46,60 @@ def run_sql():
47
  data = request.json
48
  sql_code = data.get('code', '')
49
  db_type = data.get('type', 'sqlite').lower()
50
-
51
  if not sql_code: return jsonify({'output': 'No SQL code provided'}), 400
52
 
53
  output = ""
54
-
55
- if db_type == 'sqlite':
56
- # SQL via Stdin -> SQLite
57
- cmd = ["sqlite3", ":memory:"]
58
- output = run_command(cmd, input_text=sql_code)
59
-
60
- elif db_type in ['postgres', 'postgresql']:
61
- # SQL via Stdin -> Psql
62
- # We do not use -f anymore. Psql reads from stdin by default.
63
- cmd = ["psql", "-h", "localhost", "-U", "playground", "-d", "playground"]
64
- output = run_command(cmd, input_text=sql_code)
65
-
66
- elif db_type == 'mysql':
67
- # SQL via Stdin -> MySQL
68
- # -t forces table output
69
- cmd = ["mysql", "-h", "localhost", "-u", "playground", "playground", "-t"]
70
- output = run_command(cmd, input_text=sql_code)
71
-
72
- else:
73
- return jsonify({'output': 'Unsupported database type.'}), 400
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
  return jsonify({'output': output})
76
 
77
- # Note: app.run() is no longer needed here as Gunicorn handles it,
78
- # but we keep it for local debugging if you run python app.py directly.
79
  if __name__ == '__main__':
80
  app.run(host="0.0.0.0", port=7860)
 
1
  import subprocess
2
  import sys
3
+ import uuid
4
  from flask import Flask, request, jsonify
5
  from flask_cors import CORS
6
 
 
9
 
10
  @app.route('/')
11
  def home():
12
+ return jsonify({"status": "ok", "message": "API Running (Isolated Sessions Enabled)"})
13
 
14
  def run_command(command, input_text=None, timeout=15):
15
  """Helper to run shell commands safely"""
 
37
 
38
  if not code: return jsonify({'output': 'No code provided'}), 400
39
 
 
 
40
  cmd = [sys.executable, "-c", code]
41
  output = run_command(cmd, input_text=user_input)
42
  return jsonify({'output': output})
 
46
  data = request.json
47
  sql_code = data.get('code', '')
48
  db_type = data.get('type', 'sqlite').lower()
49
+
50
  if not sql_code: return jsonify({'output': 'No SQL code provided'}), 400
51
 
52
  output = ""
53
+ session_id = uuid.uuid4().hex[:8] # Random 8-char ID for isolation
54
+
55
+ try:
56
+ # 1. SQLITE (Already Isolated by :memory:)
57
+ if db_type == 'sqlite':
58
+ cmd = ["sqlite3", ":memory:"]
59
+ output = run_command(cmd, input_text=sql_code)
60
+
61
+ # 2. POSTGRESQL (Schema Isolation)
62
+ elif db_type in ['postgres', 'postgresql']:
63
+ schema_name = f"sess_{session_id}"
64
+
65
+ # A. Create Temp Schema
66
+ setup_cmd = ["psql", "-h", "localhost", "-U", "playground", "-d", "playground", "-c", f"CREATE SCHEMA {schema_name};"]
67
+ run_command(setup_cmd)
68
+
69
+ # B. Run User Code (Wrapped in search_path to use the schema)
70
+ # We modify the SQL to force it into the new schema
71
+ isolated_sql = f"SET search_path TO {schema_name};\n{sql_code}"
72
+
73
+ main_cmd = ["psql", "-h", "localhost", "-U", "playground", "-d", "playground"]
74
+ output = run_command(main_cmd, input_text=isolated_sql)
75
+
76
+ # C. Cleanup (Drop Schema)
77
+ cleanup_cmd = ["psql", "-h", "localhost", "-U", "playground", "-d", "playground", "-c", f"DROP SCHEMA {schema_name} CASCADE;"]
78
+ run_command(cleanup_cmd)
79
+
80
+ # 3. MYSQL (Database Isolation)
81
+ elif db_type == 'mysql':
82
+ db_name = f"sess_{session_id}"
83
+
84
+ # A. Create Temp Database
85
+ setup_cmd = ["mysql", "-h", "localhost", "-u", "playground", "-e", f"CREATE DATABASE {db_name};"]
86
+ run_command(setup_cmd)
87
+
88
+ # B. Run User Code (Against the new DB)
89
+ main_cmd = ["mysql", "-h", "localhost", "-u", "playground", "-D", db_name, "-t"]
90
+ output = run_command(main_cmd, input_text=sql_code)
91
+
92
+ # C. Cleanup (Drop Database)
93
+ cleanup_cmd = ["mysql", "-h", "localhost", "-u", "playground", "-e", f"DROP DATABASE {db_name};"]
94
+ run_command(cleanup_cmd)
95
+
96
+ else:
97
+ return jsonify({'output': 'Unsupported database type.'}), 400
98
+
99
+ except Exception as e:
100
+ output = f"Server Error: {str(e)}"
101
 
102
  return jsonify({'output': output})
103
 
 
 
104
  if __name__ == '__main__':
105
  app.run(host="0.0.0.0", port=7860)