blob: 3acddcc92a6c820c828d005bcd7efdafe157a556 [file] [log] [blame]
Till Westmann5b431ca2015-10-01 19:16:11 -07001# 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
zheilbronbebb2202014-03-28 14:36:52 -070018import tweetbook_bootstrap
Eugenia Gabrielovacacc9f82013-10-21 14:10:21 -070019from urllib2 import URLError, urlopen
20from urllib import urlencode
21from json import loads, dumps
22from bottle import route, run, template, static_file, request
genia.likes.science@gmail.comd42b4022013-08-09 05:05:23 -070023
24# Core Routing
25@route('/')
26def jsontest():
zheilbronbebb2202014-03-28 14:36:52 -070027 return template('tweetbook')
genia.likes.science@gmail.comd42b4022013-08-09 05:05:23 -070028
29@route('/static/<filename:path>')
30def send_static(filename):
31 return static_file(filename, root='static')
32
33# API Helpers
34def build_response(endpoint, data):
35 api_endpoint = "http://localhost:19002/" + endpoint
Eugenia Gabrielovacacc9f82013-10-21 14:10:21 -070036
genia.likes.science@gmail.com1b30f3d2013-08-17 23:53:37 -070037 try:
Eugenia Gabrielovacacc9f82013-10-21 14:10:21 -070038 # 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 Gabrielovacacc9f82013-10-21 14:10:21 -070050
Zachary Heilbronb221e422014-03-11 13:23:20 -070051 # Create JSON dump of resulting response
Eugenia Gabrielovacacc9f82013-10-21 14:10:21 -070052 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.comd42b4022013-08-09 05:05:23 -070067
68# API Endpoints
69@route('/query')
70def run_asterix_query():
71 return (build_response("query", dict(request.query)))
72
73@route('/query/status')
74def run_asterix_query_status():
75 return (build_response("query/status", dict(request.query)))
76
77@route('/query/result')
78def run_asterix_query_result():
79 return (build_response("query/result", dict(request.query)))
80
genia.likes.science@gmail.comd42b4022013-08-09 05:05:23 -070081@route('/ddl')
82def run_asterix_ddl():
83 return (build_response("ddl", dict(request.query)))
84
85@route('/update')
86def run_asterix_update():
87 return (build_response("update", dict(request.query)))
88
zheilbronbebb2202014-03-28 14:36:52 -070089res = tweetbook_bootstrap.bootstrap()
Eugenia Gabrielovae11965b2013-10-29 19:07:42 -070090run(host='localhost', port=8080, debug=True)