lmangani commited on
Commit
482cd40
1 Parent(s): 8f3ac58

Update to latest chdb-server code

Browse files
Files changed (1) hide show
  1. main.py +71 -10
main.py CHANGED
@@ -1,33 +1,94 @@
1
- from flask import Flask, request
2
  import os
 
 
3
  import chdb
 
 
 
4
 
5
  app = Flask(__name__, static_folder="public", static_url_path="")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  @app.route('/', methods=["GET"])
 
8
  def clickhouse():
9
  query = request.args.get('query', default="", type=str)
10
- format = request.args.get('default_format', default="CSV", type=str)
11
  if not query:
 
12
  return app.send_static_file('play.html')
13
 
14
- res = chdb.query(query, format)
15
- return res.get_memview().tobytes()
 
 
 
16
 
17
  @app.route('/', methods=["POST"])
 
18
  def play():
19
- query = request.data
20
- format = request.args.get('default_format', default="CSV", type=str)
21
  if not query:
22
- return app.send_static_file('play.html')
 
 
 
 
 
 
 
23
 
24
- res = chdb.query(query, format)
25
- return res.get_memview().tobytes()
 
 
 
 
 
26
 
27
  @app.errorhandler(404)
28
  def handle_404(e):
29
  return app.send_static_file('play.html')
30
 
 
31
  host = os.getenv('HOST', '0.0.0.0')
32
  port = os.getenv('PORT', 7860)
33
- app.run(host=host, port=port)
 
 
 
1
  import os
2
+ import tempfile
3
+
4
  import chdb
5
+ from chdb import session as chs
6
+ from flask import Flask, request
7
+ from flask_httpauth import HTTPBasicAuth
8
 
9
  app = Flask(__name__, static_folder="public", static_url_path="")
10
+ auth = HTTPBasicAuth()
11
+ driver = chdb
12
+
13
+ # session support: basic username + password as unique datapath
14
+ @auth.verify_password
15
+ def verify(username, password):
16
+ if not (username and password):
17
+ print('stateless session')
18
+ globals()["driver"] = chdb
19
+ else:
20
+ path = globals()["path"] + "/" + str(hash(username + password))
21
+ print('stateful session ' + path)
22
+ globals()["driver"] = chs.Session(path)
23
+ return True
24
+
25
+ # run chdb.query(query, format), get result from return and collect stderr
26
+ def chdb_query_with_errmsg(query, format):
27
+ # Redirect stdout and stderr to the buffers
28
+ try:
29
+ new_stderr = tempfile.TemporaryFile()
30
+ old_stderr_fd = os.dup(2)
31
+ os.dup2(new_stderr.fileno(), 2)
32
+ # Call the function
33
+ output = driver.query(query, format).bytes()
34
+
35
+ new_stderr.flush()
36
+ new_stderr.seek(0)
37
+ errmsg = new_stderr.read()
38
+
39
+ # cleanup and recover
40
+ new_stderr.close()
41
+ os.dup2(old_stderr_fd, 2)
42
+ except Exception as e:
43
+ # An error occurred, print it to stderr
44
+ print(f"An error occurred: {e}")
45
+ return output, errmsg
46
+
47
 
48
  @app.route('/', methods=["GET"])
49
+ @auth.login_required
50
  def clickhouse():
51
  query = request.args.get('query', default="", type=str)
52
+ format = request.args.get('default_format', default="TSV", type=str)
53
  if not query:
54
+ # return "Ok", 200
55
  return app.send_static_file('play.html')
56
 
57
+ result, errmsg = chdb_query_with_errmsg(query, format)
58
+ if len(errmsg) == 0:
59
+ return result
60
+ return errmsg
61
+
62
 
63
  @app.route('/', methods=["POST"])
64
+ @auth.login_required
65
  def play():
66
+ query = request.data or None
67
+ format = request.args.get('default_format', default="TSV", type=str)
68
  if not query:
69
+ return "Ok", 200
70
+ # return app.send_static_file('play.html')
71
+
72
+ result, errmsg = chdb_query_with_errmsg(query, format)
73
+ if len(errmsg) == 0:
74
+ return result
75
+ return errmsg
76
+
77
 
78
+ @app.route('/play', methods=["GET"])
79
+ def handle_play():
80
+ return app.send_static_file('play.html')
81
+
82
+ @app.route('/ping', methods=["GET"])
83
+ def handle_ping():
84
+ return "Ok", 200
85
 
86
  @app.errorhandler(404)
87
  def handle_404(e):
88
  return app.send_static_file('play.html')
89
 
90
+
91
  host = os.getenv('HOST', '0.0.0.0')
92
  port = os.getenv('PORT', 7860)
93
+ path = os.getenv('DATA', '.chdb_data')
94
+ app.run(host=host, port=port)