forked from harish2704/sql-chart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
27 lines (24 loc) · 1015 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
import falcon
import sqlalchemy
import json
import sys
import datetime
import os
db_url = os.environ[ 'DB_CONNECTION' ] if 'DB_CONNECTION' in os.environ else 'postgresql://postgres@localhost/postgres'
eng = sqlalchemy.create_engine(db_url, pool_recycle=3600)
conn = eng.connect()
print "Connected to db"
class QResource:
def on_get(self, req, resp):
"""Handles GET requests"""
sql = req.params['q']
result = conn.execute(sql)
data = [(dict(row.items())) for row in result]
resp.set_header('Access-Control-Allow-Origin', '*')
resp.set_header('Access-Control-Allow-Headers', 'Content-Type,Authorization')
resp.set_header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
resp.set_header('content-type', 'application/json; charset=UTF-8')
resp.body = json.dumps( data, default=str )
api = falcon.API()
api.add_static_route('/', os.path.dirname(os.path.realpath(__file__)) + '/static' )
api.add_route('/api/data', QResource())