mirror of
https://github.com/harish2704/sql-chart.git
synced 2026-09-10 06:21:07 +00:00
Fix / updates
This commit is contained in:
+2
-2
@@ -1,3 +1,3 @@
|
|||||||
falcon==1.4.1
|
falcon
|
||||||
gunicorn==19.9.0
|
gunicorn
|
||||||
sqlalchemy
|
sqlalchemy
|
||||||
|
|||||||
@@ -1,26 +1,27 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
import falcon
|
import falcon
|
||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
import json
|
import json
|
||||||
import sys
|
from sqlalchemy import text
|
||||||
import datetime
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
db_url = os.environ[ 'DB_CONNECTION' ] if 'DB_CONNECTION' in os.environ else 'postgresql://postgres@localhost/postgres'
|
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)
|
eng = sqlalchemy.create_engine(db_url, pool_recycle=3600)
|
||||||
conn = eng.connect()
|
conn = eng.connect()
|
||||||
print "Connected to db"
|
print("Connected to db")
|
||||||
|
|
||||||
class QResource:
|
class QResource:
|
||||||
def on_get(self, req, resp):
|
def on_get(self, req, resp):
|
||||||
"""Handles GET requests"""
|
"""Handles GET requests"""
|
||||||
sql = req.params['q']
|
sql = req.params['q']
|
||||||
result = conn.execute(sql)
|
result = conn.execute(text(sql))
|
||||||
data = [(dict(row.items())) for row in result]
|
# __import__('ipdb').set_trace()
|
||||||
|
data = [row._asdict() for row in result]
|
||||||
resp.set_header('Access-Control-Allow-Origin', '*')
|
resp.set_header('Access-Control-Allow-Origin', '*')
|
||||||
resp.set_header('Access-Control-Allow-Headers', 'Content-Type,Authorization')
|
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('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
|
||||||
resp.set_header('content-type', 'application/json; charset=UTF-8')
|
resp.set_header('content-type', 'application/json; charset=UTF-8')
|
||||||
resp.body = json.dumps( data, default=str )
|
resp.text = json.dumps( data, default=str )
|
||||||
|
|
||||||
|
|
||||||
class RedirectingResource:
|
class RedirectingResource:
|
||||||
@@ -31,3 +32,11 @@ api = falcon.API()
|
|||||||
api.add_route('/', RedirectingResource())
|
api.add_route('/', RedirectingResource())
|
||||||
api.add_static_route('/', os.path.dirname(os.path.realpath(__file__)) + '/static' )
|
api.add_static_route('/', os.path.dirname(os.path.realpath(__file__)) + '/static' )
|
||||||
api.add_route('/api/data', QResource())
|
api.add_route('/api/data', QResource())
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
from wsgiref.simple_server import make_server
|
||||||
|
with make_server('', 8000, api) as httpd:
|
||||||
|
print('Serving on port 8000...')
|
||||||
|
# Serve until process is killed
|
||||||
|
httpd.serve_forever()
|
||||||
|
|||||||
Reference in New Issue
Block a user