genia.likes.science@gmail.com | d42b402 | 2013-08-09 05:05:23 -0700 | [diff] [blame] | 1 | import black_cherry_bootstrap |
Eugenia Gabrielova | cacc9f8 | 2013-10-21 14:10:21 -0700 | [diff] [blame^] | 2 | from urllib2 import URLError, urlopen |
| 3 | from urllib import urlencode |
| 4 | from json import loads, dumps |
| 5 | from bottle import route, run, template, static_file, request |
genia.likes.science@gmail.com | d42b402 | 2013-08-09 05:05:23 -0700 | [diff] [blame] | 6 | |
| 7 | # Core Routing |
| 8 | @route('/') |
| 9 | def jsontest(): |
| 10 | return template('cherry') |
| 11 | |
| 12 | @route('/static/<filename:path>') |
| 13 | def send_static(filename): |
| 14 | return static_file(filename, root='static') |
| 15 | |
| 16 | # API Helpers |
| 17 | def build_response(endpoint, data): |
| 18 | api_endpoint = "http://localhost:19002/" + endpoint |
Eugenia Gabrielova | cacc9f8 | 2013-10-21 14:10:21 -0700 | [diff] [blame^] | 19 | |
genia.likes.science@gmail.com | 1b30f3d | 2013-08-17 23:53:37 -0700 | [diff] [blame] | 20 | try: |
Eugenia Gabrielova | cacc9f8 | 2013-10-21 14:10:21 -0700 | [diff] [blame^] | 21 | # 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.com | d42b402 | 2013-08-09 05:05:23 -0700 | [diff] [blame] | 51 | |
| 52 | # API Endpoints |
| 53 | @route('/query') |
| 54 | def run_asterix_query(): |
| 55 | return (build_response("query", dict(request.query))) |
| 56 | |
| 57 | @route('/query/status') |
| 58 | def run_asterix_query_status(): |
| 59 | return (build_response("query/status", dict(request.query))) |
| 60 | |
| 61 | @route('/query/result') |
| 62 | def run_asterix_query_result(): |
| 63 | return (build_response("query/result", dict(request.query))) |
| 64 | |
genia.likes.science@gmail.com | d42b402 | 2013-08-09 05:05:23 -0700 | [diff] [blame] | 65 | @route('/ddl') |
| 66 | def run_asterix_ddl(): |
| 67 | return (build_response("ddl", dict(request.query))) |
| 68 | |
| 69 | @route('/update') |
| 70 | def run_asterix_update(): |
| 71 | return (build_response("update", dict(request.query))) |
| 72 | |
| 73 | res = black_cherry_bootstrap.bootstrap() |
Eugenia Gabrielova | cacc9f8 | 2013-10-21 14:10:21 -0700 | [diff] [blame^] | 74 | run(host='localhost', port=8080, debug=True) |