-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
server.py
36 lines (28 loc) · 1013 Bytes
/
server.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
from flask import Flask, request
import chdb
import os
# chdb API server example with GET/POST support, compatible with play app
# for a full server example see https://github.com/chdb-io/chdb-playground
app = Flask(__name__, static_folder="", static_url_path="")
@app.route('/', methods=["GET"])
def clickhouse():
query = request.args.get('query', default="", type=str)
format = request.args.get('default_format', default="JSONCompact", type=str)
if not query:
return "Query not found", 400
res = chdb.query(query, format)
return res.bytes()
@app.route('/', methods=["POST"])
def play():
query = request.data
format = request.args.get('default_format', default="JSONCompact", type=str)
if not query:
return "Query not found", 400
res = chdb.query(query, format)
return res.bytes()
@app.errorhandler(404)
def handle_404(e):
return "Not found", 404
host = os.getenv('HOST', '0.0.0.0')
port = os.getenv('PORT', 8123)
app.run(host=host, port=port)