-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
72 lines (60 loc) · 2.07 KB
/
server.py
File metadata and controls
72 lines (60 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from flask import Flask, request
import subprocess
from flask import Response
app = Flask(__name__, static_folder="static")
def run_db_command(args):
try:
result = subprocess.check_output(args)
return Response(f"<pre>{result.decode()}</pre>", mimetype="text/html")
except subprocess.CalledProcessError as e:
return Response(f"<pre>Error: {e.output.decode()}</pre>", mimetype="text/html")
@app.route("/select_all")
def select_all():
return run_db_command(["mydb.exe", "select"])
@app.route("/insert")
def insert():
args = ["mydb.exe", "insert"]
for key in request.args:
args.append(f"{key}={request.args.get(key)}")
return run_db_command(args)
@app.route("/update")
def update():
match_col = request.args.get("match_col")
match_val = request.args.get("match_val")
if not match_col or not match_val:
return "Missing match_col or match_val"
args = ["mydb.exe", "update", match_col, match_val]
for key in request.args:
if key not in ["match_col", "match_val"]:
args.append(f"{key}={request.args.get(key)}")
return run_db_command(args)
@app.route("/delete")
def delete():
col = request.args.get("col")
val = request.args.get("val")
if not col or not val:
return "Missing delete parameters"
return run_db_command(["mydb.exe", "delete", col, val])
from flask import send_file
@app.route("/export")
def export():
subprocess.call(["mydb.exe", "export", "data.csv"])
return send_file("data.csv", as_attachment=True)
@app.route("/")
def index():
return app.send_static_file("index.html")
from flask import jsonify
@app.route("/schema")
def get_schema():
try:
with open("schema.txt") as f:
line = f.readline().strip()
fields = []
for item in line.split(','):
name, typ = item.split(':')
fields.append({"name": name, "type": typ})
return jsonify(fields) # FIX: ensure it's JSON
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
app.run(debug=True)