Skip to content

Commit

Permalink
Add server example (#27)
Browse files Browse the repository at this point in the history
Basic example using chdb to serve GET/POST API requests, compatible with ClickHouse Play client queries. 

No action needed as this is not part of the compiled code.
  • Loading branch information
lmangani authored and auxten committed Jun 28, 2023
1 parent 3527e6a commit 73e095f
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions examples/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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/metrico/chdb-server

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.get_memview().tobytes()

@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.get_memview().tobytes()

@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)

0 comments on commit 73e095f

Please sign in to comment.