blob: d78514b2f52dbff288e5c1fb712ef0d76fa9c03f [file] [log] [blame]
genia.likes.science@gmail.comd42b4022013-08-09 05:05:23 -07001import black_cherry_bootstrap
Eugenia Gabrielovacacc9f82013-10-21 14:10:21 -07002from urllib2 import URLError, urlopen
3from urllib import urlencode
4from json import loads, dumps
5from bottle import route, run, template, static_file, request
genia.likes.science@gmail.comd42b4022013-08-09 05:05:23 -07006
7# Core Routing
8@route('/')
9def jsontest():
10 return template('cherry')
11
12@route('/static/<filename:path>')
13def send_static(filename):
14 return static_file(filename, root='static')
15
16# API Helpers
17def build_response(endpoint, data):
18 api_endpoint = "http://localhost:19002/" + endpoint
Eugenia Gabrielovacacc9f82013-10-21 14:10:21 -070019
genia.likes.science@gmail.com1b30f3d2013-08-17 23:53:37 -070020 try:
Eugenia Gabrielovacacc9f82013-10-21 14:10:21 -070021 # Encode data into url string
22 urlresponse = urlopen(api_endpoint + '?' + urlencode(data))
23
24 # There are some weird bits passed in from the Asterix JSON.
25 # We will remove them here before we pass the result string
26 # back to the frontend.
27 urlresult = ""
28 CHUNK = 16 * 1024
29 while True:
30 chunk = urlresponse.read(CHUNK)
31 if not chunk: break
32 urlresult += chunk
33 urlresult = ','.join(urlresult.split(']}{"results":['))
34
35 # Create JSON dump of resulting response
36 return loads(urlresult)
37
38 except ValueError, e:
39 pass
40
41 except URLError, e:
42
43 # Here we report possible errors in request fulfillment.
44 if hasattr(e, 'reason'):
45 print 'Failed to reach a server.'
46 print 'Reason: ', e.reason
47
48 elif hasattr(e, 'code'):
49 print 'The server couldn\'t fulfill the request.'
50 print 'Error code: ', e.code
genia.likes.science@gmail.comd42b4022013-08-09 05:05:23 -070051
52# API Endpoints
53@route('/query')
54def run_asterix_query():
55 return (build_response("query", dict(request.query)))
56
57@route('/query/status')
58def run_asterix_query_status():
59 return (build_response("query/status", dict(request.query)))
60
61@route('/query/result')
62def run_asterix_query_result():
63 return (build_response("query/result", dict(request.query)))
64
genia.likes.science@gmail.comd42b4022013-08-09 05:05:23 -070065@route('/ddl')
66def run_asterix_ddl():
67 return (build_response("ddl", dict(request.query)))
68
69@route('/update')
70def run_asterix_update():
71 return (build_response("update", dict(request.query)))
72
73res = black_cherry_bootstrap.bootstrap()
Eugenia Gabrielovacacc9f82013-10-21 14:10:21 -070074run(host='localhost', port=8080, debug=True)