This repository was archived by the owner on Dec 29, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
71 lines (52 loc) · 1.65 KB
/
main.py
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
import os
import select
import chdb
from flask import Flask, request
app = Flask(__name__, static_folder="public", static_url_path="")
def chdb_query_with_errmsg(query, format):
pipe_out, pipe_in = os.pipe()
stderr = os.dup(2)
os.dup2(pipe_in, 2)
# check if we have more to read from the pipe
def more_data():
r, _, _ = select.select([pipe_out], [], [], 0)
return bool(r)
# read the whole pipe
def read_pipe():
out = b''
while more_data():
out += os.read(pipe_out, 1024)
return out.decode(encoding='utf-8', errors='strict')
res = chdb.query(query, format)
os.dup2(stderr, 2)
result = res.get_memview().tobytes()
errmsg = read_pipe()
return result, errmsg
@app.route('/', methods=["GET"])
def clickhouse():
query = request.args.get('query', default="", type=str)
format = request.args.get('default_format', default="CSV", type=str)
if not query:
return app.send_static_file('play.html')
result, errmsg = chdb_query_with_errmsg(query, format)
if errmsg == '':
return result
else:
return errmsg
@app.route('/', methods=["POST"])
def play():
query = request.data
format = request.args.get('default_format', default="CSV", type=str)
if not query:
return app.send_static_file('play.html')
result, errmsg = chdb_query_with_errmsg(query, format)
if errmsg == '':
return result
else:
return errmsg
@app.errorhandler(404)
def handle_404(e):
return app.send_static_file('play.html')
host = os.getenv('HOST', '0.0.0.0')
port = os.getenv('PORT', 8123)
app.run(host=host, port=port)