blob: 7dbf84b08b42595e1b43193724dad23b98b0f8ce [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
Eugenia Gabrielovacacc9f82013-10-21 14:10:21 -070033
Zachary Heilbronb221e422014-03-11 13:23:20 -070034 # Create JSON dump of resulting response
Eugenia Gabrielovacacc9f82013-10-21 14:10:21 -070035 return loads(urlresult)
36
37 except ValueError, e:
38 pass
39
40 except URLError, e:
41
42 # Here we report possible errors in request fulfillment.
43 if hasattr(e, 'reason'):
44 print 'Failed to reach a server.'
45 print 'Reason: ', e.reason
46
47 elif hasattr(e, 'code'):
48 print 'The server couldn\'t fulfill the request.'
49 print 'Error code: ', e.code
genia.likes.science@gmail.comd42b4022013-08-09 05:05:23 -070050
51# API Endpoints
52@route('/query')
53def run_asterix_query():
54 return (build_response("query", dict(request.query)))
55
56@route('/query/status')
57def run_asterix_query_status():
58 return (build_response("query/status", dict(request.query)))
59
60@route('/query/result')
61def run_asterix_query_result():
62 return (build_response("query/result", dict(request.query)))
63
genia.likes.science@gmail.comd42b4022013-08-09 05:05:23 -070064@route('/ddl')
65def run_asterix_ddl():
66 return (build_response("ddl", dict(request.query)))
67
68@route('/update')
69def run_asterix_update():
70 return (build_response("update", dict(request.query)))
71
72res = black_cherry_bootstrap.bootstrap()
Eugenia Gabrielovae11965b2013-10-29 19:07:42 -070073run(host='localhost', port=8080, debug=True)