Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,9 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import threading
|
4 |
-
from flask import Flask
|
5 |
from psycopg2 import connect
|
|
|
6 |
|
7 |
# Fetch environment variables
|
8 |
DB_NAME = os.getenv("DB_NAME")
|
@@ -22,10 +23,85 @@ def get_db_connection():
|
|
22 |
port=DB_PORT
|
23 |
)
|
24 |
|
25 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
server_threads = {}
|
27 |
|
28 |
-
# Flask app template for HTTP servers
|
29 |
def create_server(name, port):
|
30 |
app = Flask(name)
|
31 |
|
@@ -35,70 +111,58 @@ def create_server(name, port):
|
|
35 |
|
36 |
return app
|
37 |
|
38 |
-
# Function to start a server
|
39 |
def start_server(name, port):
|
40 |
if name in server_threads:
|
41 |
return f"Server {name} is already running!"
|
42 |
-
|
43 |
-
app = create_server(name, port)
|
44 |
|
|
|
|
|
45 |
def run():
|
46 |
app.run(host="0.0.0.0", port=port, debug=False, use_reloader=False)
|
47 |
|
48 |
thread = threading.Thread(target=run, daemon=True)
|
49 |
thread.start()
|
50 |
server_threads[name] = thread
|
51 |
-
|
52 |
-
# Update database status
|
53 |
-
conn = get_db_connection()
|
54 |
-
with conn.cursor() as cursor:
|
55 |
-
cursor.execute(
|
56 |
-
"INSERT INTO servers (name, port, status) VALUES (%s, %s, %s) ON CONFLICT (name) DO UPDATE SET status = EXCLUDED.status",
|
57 |
-
(name, port, "running")
|
58 |
-
)
|
59 |
-
conn.commit()
|
60 |
-
conn.close()
|
61 |
-
|
62 |
return f"Server {name} started on port {port}!"
|
63 |
|
64 |
-
# Function to stop a server (dummy implementation for now)
|
65 |
def stop_server(name):
|
66 |
if name not in server_threads:
|
67 |
return f"Server {name} is not running!"
|
68 |
|
69 |
-
# Terminate the thread (not directly supported, requires external handling)
|
70 |
server_threads.pop(name)
|
71 |
return f"Server {name} stopped!"
|
72 |
|
73 |
# Gradio UI
|
74 |
-
def manage_servers(password, action, name, port):
|
75 |
-
if password != APP_PASSWORD:
|
76 |
-
return "Invalid password!"
|
77 |
-
|
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 |
-
# Gradio Interface
|
86 |
with gr.Blocks() as server_manager:
|
87 |
gr.Markdown("# Server Manager")
|
|
|
88 |
with gr.Row():
|
89 |
password = gr.Textbox(label="Password", type="password")
|
|
|
90 |
with gr.Row():
|
91 |
-
action = gr.Radio(["start", "stop"], label="Action")
|
92 |
-
name = gr.Textbox(label="Server Name")
|
93 |
-
port = gr.Number(label="Port")
|
|
|
94 |
with gr.Row():
|
95 |
submit = gr.Button("Submit")
|
|
|
96 |
output = gr.Textbox(label="Output")
|
97 |
-
|
98 |
submit.click(
|
99 |
manage_servers,
|
100 |
inputs=[password, action, name, port],
|
101 |
outputs=output
|
102 |
)
|
103 |
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import threading
|
4 |
+
from flask import Flask, jsonify
|
5 |
from psycopg2 import connect
|
6 |
+
import psycopg2.extras
|
7 |
|
8 |
# Fetch environment variables
|
9 |
DB_NAME = os.getenv("DB_NAME")
|
|
|
23 |
port=DB_PORT
|
24 |
)
|
25 |
|
26 |
+
# Database operation functions
|
27 |
+
def get_db_stats():
|
28 |
+
conn = get_db_connection()
|
29 |
+
cursor = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
|
30 |
+
|
31 |
+
# Example: Get database size (this is a placeholder query, you can adapt it to your use case)
|
32 |
+
cursor.execute("SELECT pg_size_pretty(pg_database_size(current_database())) AS size")
|
33 |
+
db_stats = cursor.fetchone()
|
34 |
+
|
35 |
+
conn.close()
|
36 |
+
return db_stats['size']
|
37 |
+
|
38 |
+
def create_table(name):
|
39 |
+
conn = get_db_connection()
|
40 |
+
cursor = conn.cursor()
|
41 |
+
|
42 |
+
# Create a basic table (customize the structure as needed)
|
43 |
+
cursor.execute(f"""
|
44 |
+
CREATE TABLE IF NOT EXISTS {name} (
|
45 |
+
id SERIAL PRIMARY KEY,
|
46 |
+
data TEXT
|
47 |
+
);
|
48 |
+
""")
|
49 |
+
conn.commit()
|
50 |
+
conn.close()
|
51 |
+
return f"Table {name} created successfully!"
|
52 |
+
|
53 |
+
def delete_table(name):
|
54 |
+
conn = get_db_connection()
|
55 |
+
cursor = conn.cursor()
|
56 |
+
|
57 |
+
# Delete table
|
58 |
+
cursor.execute(f"DROP TABLE IF EXISTS {name};")
|
59 |
+
conn.commit()
|
60 |
+
conn.close()
|
61 |
+
return f"Table {name} deleted successfully!"
|
62 |
+
|
63 |
+
# Flask app for HTTP access to the database
|
64 |
+
def create_http_server():
|
65 |
+
app = Flask(__name__)
|
66 |
+
|
67 |
+
@app.route("/db-stats", methods=["GET"])
|
68 |
+
def db_stats():
|
69 |
+
db_stats = get_db_stats()
|
70 |
+
return jsonify({"db_size": db_stats})
|
71 |
+
|
72 |
+
@app.route("/create-table/<name>", methods=["POST"])
|
73 |
+
def create_table_route(name):
|
74 |
+
result = create_table(name)
|
75 |
+
return jsonify({"message": result})
|
76 |
+
|
77 |
+
@app.route("/delete-table/<name>", methods=["DELETE"])
|
78 |
+
def delete_table_route(name):
|
79 |
+
result = delete_table(name)
|
80 |
+
return jsonify({"message": result})
|
81 |
+
|
82 |
+
return app
|
83 |
+
|
84 |
+
# Manage server and database operations in the Gradio UI
|
85 |
+
def manage_servers(password, action, name=None, port=None):
|
86 |
+
if password != APP_PASSWORD:
|
87 |
+
return "Invalid password!"
|
88 |
+
|
89 |
+
if action == "start":
|
90 |
+
return start_server(name, port)
|
91 |
+
elif action == "stop":
|
92 |
+
return stop_server(name)
|
93 |
+
elif action == "db-stats":
|
94 |
+
return get_db_stats()
|
95 |
+
elif action == "create-table" and name:
|
96 |
+
return create_table(name)
|
97 |
+
elif action == "delete-table" and name:
|
98 |
+
return delete_table(name)
|
99 |
+
else:
|
100 |
+
return "Invalid action!"
|
101 |
+
|
102 |
+
# Start and stop server functions (same as before)
|
103 |
server_threads = {}
|
104 |
|
|
|
105 |
def create_server(name, port):
|
106 |
app = Flask(name)
|
107 |
|
|
|
111 |
|
112 |
return app
|
113 |
|
|
|
114 |
def start_server(name, port):
|
115 |
if name in server_threads:
|
116 |
return f"Server {name} is already running!"
|
|
|
|
|
117 |
|
118 |
+
app = create_server(name, port)
|
119 |
+
|
120 |
def run():
|
121 |
app.run(host="0.0.0.0", port=port, debug=False, use_reloader=False)
|
122 |
|
123 |
thread = threading.Thread(target=run, daemon=True)
|
124 |
thread.start()
|
125 |
server_threads[name] = thread
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
return f"Server {name} started on port {port}!"
|
127 |
|
|
|
128 |
def stop_server(name):
|
129 |
if name not in server_threads:
|
130 |
return f"Server {name} is not running!"
|
131 |
|
|
|
132 |
server_threads.pop(name)
|
133 |
return f"Server {name} stopped!"
|
134 |
|
135 |
# Gradio UI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
with gr.Blocks() as server_manager:
|
137 |
gr.Markdown("# Server Manager")
|
138 |
+
|
139 |
with gr.Row():
|
140 |
password = gr.Textbox(label="Password", type="password")
|
141 |
+
|
142 |
with gr.Row():
|
143 |
+
action = gr.Radio(["start", "stop", "db-stats", "create-table", "delete-table"], label="Action")
|
144 |
+
name = gr.Textbox(label="Server or Table Name", optional=True)
|
145 |
+
port = gr.Number(label="Port", optional=True)
|
146 |
+
|
147 |
with gr.Row():
|
148 |
submit = gr.Button("Submit")
|
149 |
+
|
150 |
output = gr.Textbox(label="Output")
|
151 |
+
|
152 |
submit.click(
|
153 |
manage_servers,
|
154 |
inputs=[password, action, name, port],
|
155 |
outputs=output
|
156 |
)
|
157 |
|
158 |
+
# Start HTTP server for database operations
|
159 |
+
def start_http_server():
|
160 |
+
http_server = create_http_server()
|
161 |
+
http_thread = threading.Thread(target=http_server.run, kwargs={"host": "0.0.0.0", "port": 5000}, daemon=True)
|
162 |
+
http_thread.start()
|
163 |
+
|
164 |
+
# Start everything
|
165 |
+
if __name__ == "__main__":
|
166 |
+
start_http_server()
|
167 |
+
server_manager.launch()
|
168 |
+
|