Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
import threading
|
| 4 |
-
from flask import Flask, request
|
| 5 |
from psycopg2 import connect, sql
|
| 6 |
import psycopg2.extras
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Fetch environment variables
|
| 9 |
DB_NAME = os.getenv("DB_NAME")
|
|
@@ -23,152 +23,64 @@ def get_db_connection():
|
|
| 23 |
port=DB_PORT
|
| 24 |
)
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
# Flask app template for HTTP servers
|
| 30 |
-
def create_server(name, port):
|
| 31 |
-
app = Flask(name)
|
| 32 |
-
|
| 33 |
-
@app.route("/")
|
| 34 |
-
def home():
|
| 35 |
-
return f"Server {name} is running on port {port}!"
|
| 36 |
-
|
| 37 |
-
return app
|
| 38 |
-
|
| 39 |
-
# Function to start a server
|
| 40 |
-
def start_server(name, port):
|
| 41 |
-
if name in server_threads:
|
| 42 |
-
return f"Server {name} is already running!"
|
| 43 |
-
|
| 44 |
-
app = create_server(name, port)
|
| 45 |
-
|
| 46 |
-
def run():
|
| 47 |
-
app.run(host="0.0.0.0", port=port, debug=False, use_reloader=False)
|
| 48 |
-
|
| 49 |
-
thread = threading.Thread(target=run, daemon=True)
|
| 50 |
-
thread.start()
|
| 51 |
-
server_threads[name] = thread
|
| 52 |
-
|
| 53 |
-
# Update database status
|
| 54 |
-
conn = get_db_connection()
|
| 55 |
-
with conn.cursor() as cursor:
|
| 56 |
-
cursor.execute(
|
| 57 |
-
"INSERT INTO servers (name, port, status) VALUES (%s, %s, %s) ON CONFLICT (name) DO UPDATE SET status = EXCLUDED.status",
|
| 58 |
-
(name, port, "running")
|
| 59 |
-
)
|
| 60 |
-
conn.commit()
|
| 61 |
-
conn.close()
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
def stop_server(name):
|
| 67 |
-
if name not in server_threads:
|
| 68 |
-
return f"Server {name} is not running!"
|
| 69 |
-
|
| 70 |
-
# Terminate the thread (not directly supported, requires external handling)
|
| 71 |
-
server_threads.pop(name)
|
| 72 |
-
return f"Server {name} stopped!"
|
| 73 |
-
|
| 74 |
-
# Gradio UI for managing servers
|
| 75 |
-
def manage_servers(password, action, name, port):
|
| 76 |
if password != APP_PASSWORD:
|
| 77 |
-
return "Invalid password!"
|
| 78 |
-
if action == "start":
|
| 79 |
-
return start_server(name, port)
|
| 80 |
-
elif action == "stop":
|
| 81 |
-
return stop_server(name)
|
| 82 |
-
else:
|
| 83 |
-
return "Invalid action!"
|
| 84 |
|
| 85 |
-
#
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
return "Invalid password!"
|
| 89 |
|
| 90 |
-
#
|
| 91 |
-
conn =
|
| 92 |
-
|
| 93 |
-
|
| 94 |
try:
|
| 95 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
cursor.execute(command)
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
| 99 |
except Exception as e:
|
| 100 |
-
result = f"Error executing command: {e}"
|
| 101 |
finally:
|
| 102 |
-
conn
|
| 103 |
-
|
| 104 |
-
return result
|
| 105 |
-
|
| 106 |
-
# Gradio UI for managing servers and interacting with DB
|
| 107 |
-
def get_db_stats():
|
| 108 |
-
conn = get_db_connection()
|
| 109 |
-
cursor = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
|
| 110 |
-
|
| 111 |
-
# CockroachDB query to get database size (total used space in bytes)
|
| 112 |
-
cursor.execute("""
|
| 113 |
-
SELECT
|
| 114 |
-
db_name,
|
| 115 |
-
SUM(range_size) AS total_size_bytes
|
| 116 |
-
FROM crdb_internal.database_statistics
|
| 117 |
-
WHERE db_name = current_database()
|
| 118 |
-
GROUP BY db_name;
|
| 119 |
-
""")
|
| 120 |
-
db_stats = cursor.fetchone()
|
| 121 |
|
| 122 |
-
|
| 123 |
-
if db_stats:
|
| 124 |
-
return f"Database {db_stats['db_name']} size: {db_stats['total_size_bytes']} bytes"
|
| 125 |
-
else:
|
| 126 |
-
return "Unable to fetch database size"
|
| 127 |
|
| 128 |
-
# Gradio
|
| 129 |
-
|
| 130 |
-
|
|
|
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
password = gr.Textbox(label="Password", type="password")
|
| 135 |
-
with gr.Row():
|
| 136 |
-
action = gr.Radio(["start", "stop"], label="Action")
|
| 137 |
-
name = gr.Textbox(label="Server Name")
|
| 138 |
-
port = gr.Number(label="Port")
|
| 139 |
-
with gr.Row():
|
| 140 |
-
submit = gr.Button("Submit")
|
| 141 |
-
output = gr.Textbox(label="Output")
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
inputs=[password, action, name, port],
|
| 147 |
-
outputs=output
|
| 148 |
-
)
|
| 149 |
-
|
| 150 |
-
# Gradio UI for viewing DB stats and executing commands
|
| 151 |
-
with gr.Row():
|
| 152 |
-
command_input = gr.Textbox(label="Database Command", placeholder="Enter SQL command")
|
| 153 |
-
command_submit = gr.Button("Execute Command")
|
| 154 |
-
command_output = gr.Textbox(label="Command Output")
|
| 155 |
-
|
| 156 |
-
# Submit action for executing DB commands
|
| 157 |
-
command_submit.click(
|
| 158 |
-
execute_db_command,
|
| 159 |
-
inputs=[password, command_input],
|
| 160 |
-
outputs=command_output
|
| 161 |
-
)
|
| 162 |
-
|
| 163 |
-
# Show the database size
|
| 164 |
-
with gr.Row():
|
| 165 |
-
db_stats_output = gr.Textbox(label="Database Stats")
|
| 166 |
-
gr.Button("Show Database Stats").click(
|
| 167 |
-
get_db_stats,
|
| 168 |
-
outputs=db_stats_output
|
| 169 |
-
)
|
| 170 |
|
| 171 |
-
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
-
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
from flask import Flask, request, jsonify
|
|
|
|
|
|
|
| 3 |
from psycopg2 import connect, sql
|
| 4 |
import psycopg2.extras
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import threading
|
| 7 |
|
| 8 |
# Fetch environment variables
|
| 9 |
DB_NAME = os.getenv("DB_NAME")
|
|
|
|
| 23 |
port=DB_PORT
|
| 24 |
)
|
| 25 |
|
| 26 |
+
# Create Flask app
|
| 27 |
+
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# API endpoint for running SQL commands
|
| 30 |
+
@app.route("/run_sql", methods=["POST"])
|
| 31 |
+
def run_sql():
|
| 32 |
+
# Get the password and command from the request
|
| 33 |
+
password = request.json.get("password")
|
| 34 |
+
command = request.json.get("command")
|
| 35 |
|
| 36 |
+
# Check if the password is correct
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
if password != APP_PASSWORD:
|
| 38 |
+
return jsonify({"error": "Invalid password!"}), 401
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
# Validate SQL command input
|
| 41 |
+
if not command:
|
| 42 |
+
return jsonify({"error": "No SQL command provided!"}), 400
|
|
|
|
| 43 |
|
| 44 |
+
# Execute the SQL command
|
| 45 |
+
conn = None
|
| 46 |
+
result = None
|
|
|
|
| 47 |
try:
|
| 48 |
+
# Connect to the database
|
| 49 |
+
conn = get_db_connection()
|
| 50 |
+
cursor = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
|
| 51 |
+
|
| 52 |
+
# Execute the SQL command (this uses parameterized queries for security)
|
| 53 |
cursor.execute(command)
|
| 54 |
+
if command.strip().lower().startswith("select"):
|
| 55 |
+
result = cursor.fetchall() # If the command is a SELECT, fetch the results
|
| 56 |
+
else:
|
| 57 |
+
conn.commit() # For non-SELECT commands, commit the changes
|
| 58 |
+
result = {"message": "Command executed successfully!"}
|
| 59 |
except Exception as e:
|
| 60 |
+
result = {"error": f"Error executing command: {str(e)}"}
|
| 61 |
finally:
|
| 62 |
+
if conn:
|
| 63 |
+
conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
return jsonify(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
# Define Gradio interface
|
| 68 |
+
def gradio_interface():
|
| 69 |
+
def greet(name):
|
| 70 |
+
return f"Hello {name}!"
|
| 71 |
|
| 72 |
+
interface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 73 |
+
interface.launch(share=True, inline=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
# Starting Flask app in a separate thread
|
| 76 |
+
def run_flask():
|
| 77 |
+
app.run(host="0.0.0.0", port=5050)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
# Run Flask and Gradio in parallel using threading
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
flask_thread = threading.Thread(target=run_flask)
|
| 82 |
+
flask_thread.daemon = True # Make sure it will close when the main thread exits
|
| 83 |
+
flask_thread.start()
|
| 84 |
|
| 85 |
+
# Run Gradio in the main thread
|
| 86 |
+
gradio_interface()
|