Till Westmann | 5b431ca | 2015-10-01 19:16:11 -0700 | [diff] [blame^] | 1 | # Licensed to the Apache Software Foundation (ASF) under one |
| 2 | # or more contributor license agreements. See the NOTICE file |
| 3 | # distributed with this work for additional information |
| 4 | # regarding copyright ownership. The ASF licenses this file |
| 5 | # to you under the Apache License, Version 2.0 (the |
| 6 | # "License"); you may not use this file except in compliance |
| 7 | # with the License. You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, |
| 12 | # software distributed under the License is distributed on an |
| 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | # KIND, either express or implied. See the License for the |
| 15 | # specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
zheilbron | bebb220 | 2014-03-28 14:36:52 -0700 | [diff] [blame] | 18 | import tweetbook_bootstrap |
Eugenia Gabrielova | cacc9f8 | 2013-10-21 14:10:21 -0700 | [diff] [blame] | 19 | from urllib2 import URLError, urlopen |
| 20 | from urllib import urlencode |
| 21 | from json import loads, dumps |
| 22 | from bottle import route, run, template, static_file, request |
genia.likes.science@gmail.com | d42b402 | 2013-08-09 05:05:23 -0700 | [diff] [blame] | 23 | |
| 24 | # Core Routing |
| 25 | @route('/') |
| 26 | def jsontest(): |
zheilbron | bebb220 | 2014-03-28 14:36:52 -0700 | [diff] [blame] | 27 | return template('tweetbook') |
genia.likes.science@gmail.com | d42b402 | 2013-08-09 05:05:23 -0700 | [diff] [blame] | 28 | |
| 29 | @route('/static/<filename:path>') |
| 30 | def send_static(filename): |
| 31 | return static_file(filename, root='static') |
| 32 | |
| 33 | # API Helpers |
| 34 | def build_response(endpoint, data): |
| 35 | api_endpoint = "http://localhost:19002/" + endpoint |
Eugenia Gabrielova | cacc9f8 | 2013-10-21 14:10:21 -0700 | [diff] [blame] | 36 | |
genia.likes.science@gmail.com | 1b30f3d | 2013-08-17 23:53:37 -0700 | [diff] [blame] | 37 | try: |
Eugenia Gabrielova | cacc9f8 | 2013-10-21 14:10:21 -0700 | [diff] [blame] | 38 | # Encode data into url string |
| 39 | urlresponse = urlopen(api_endpoint + '?' + urlencode(data)) |
| 40 | |
| 41 | # There are some weird bits passed in from the Asterix JSON. |
| 42 | # We will remove them here before we pass the result string |
| 43 | # back to the frontend. |
| 44 | urlresult = "" |
| 45 | CHUNK = 16 * 1024 |
| 46 | while True: |
| 47 | chunk = urlresponse.read(CHUNK) |
| 48 | if not chunk: break |
| 49 | urlresult += chunk |
Eugenia Gabrielova | cacc9f8 | 2013-10-21 14:10:21 -0700 | [diff] [blame] | 50 | |
Zachary Heilbron | b221e42 | 2014-03-11 13:23:20 -0700 | [diff] [blame] | 51 | # Create JSON dump of resulting response |
Eugenia Gabrielova | cacc9f8 | 2013-10-21 14:10:21 -0700 | [diff] [blame] | 52 | return loads(urlresult) |
| 53 | |
| 54 | except ValueError, e: |
| 55 | pass |
| 56 | |
| 57 | except URLError, e: |
| 58 | |
| 59 | # Here we report possible errors in request fulfillment. |
| 60 | if hasattr(e, 'reason'): |
| 61 | print 'Failed to reach a server.' |
| 62 | print 'Reason: ', e.reason |
| 63 | |
| 64 | elif hasattr(e, 'code'): |
| 65 | print 'The server couldn\'t fulfill the request.' |
| 66 | print 'Error code: ', e.code |
genia.likes.science@gmail.com | d42b402 | 2013-08-09 05:05:23 -0700 | [diff] [blame] | 67 | |
| 68 | # API Endpoints |
| 69 | @route('/query') |
| 70 | def run_asterix_query(): |
| 71 | return (build_response("query", dict(request.query))) |
| 72 | |
| 73 | @route('/query/status') |
| 74 | def run_asterix_query_status(): |
| 75 | return (build_response("query/status", dict(request.query))) |
| 76 | |
| 77 | @route('/query/result') |
| 78 | def run_asterix_query_result(): |
| 79 | return (build_response("query/result", dict(request.query))) |
| 80 | |
genia.likes.science@gmail.com | d42b402 | 2013-08-09 05:05:23 -0700 | [diff] [blame] | 81 | @route('/ddl') |
| 82 | def run_asterix_ddl(): |
| 83 | return (build_response("ddl", dict(request.query))) |
| 84 | |
| 85 | @route('/update') |
| 86 | def run_asterix_update(): |
| 87 | return (build_response("update", dict(request.query))) |
| 88 | |
zheilbron | bebb220 | 2014-03-28 14:36:52 -0700 | [diff] [blame] | 89 | res = tweetbook_bootstrap.bootstrap() |
Eugenia Gabrielova | e11965b | 2013-10-29 19:07:42 -0700 | [diff] [blame] | 90 | run(host='localhost', port=8080, debug=True) |