genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 1 | from twisted.internet import reactor |
| 2 | from twisted.web import static, proxy, server |
| 3 | |
| 4 | import os |
| 5 | import json |
| 6 | import sys |
genia.likes.science@gmail.com | d42b402 | 2013-08-09 05:05:23 -0700 | [diff] [blame] | 7 | |
| 8 | from lib.requests.requests import requests |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 9 | |
| 10 | asterix_host = "localhost" |
| 11 | asterix_port = 19002 |
| 12 | |
| 13 | # First, let's get the path to this script, we'll need it to configure the demo. |
| 14 | base = os.path.dirname(os.path.realpath(__file__)) |
| 15 | print "Running Black Cherry from",base |
| 16 | |
| 17 | # First, we bootstrap our request query |
| 18 | print "Preparing DDL..\n" |
| 19 | query_statement = open("src/data/query.txt").read().split("load") |
| 20 | query_statement[1] = "load" + query_statement[1].replace("FULL_PATH",base + "/src/data") |
| 21 | |
| 22 | ddl = { |
| 23 | 'ddl': query_statement[0] |
| 24 | } |
| 25 | |
| 26 | load = { |
| 27 | "statements" : "use dataverse twitter; " + query_statement[1] |
| 28 | } |
| 29 | |
| 30 | http_header = { |
| 31 | 'content-type': 'application/json' |
| 32 | } |
| 33 | |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 34 | # Now we run our query |
| 35 | print "Running query...\n" |
| 36 | |
| 37 | ddl_url = "http://" + asterix_host + ":" + str(asterix_port) + "/ddl" |
| 38 | update_url = "http://" + asterix_host + ":" + str(asterix_port) + "/update" |
| 39 | requests.get(ddl_url, params=ddl) |
| 40 | response = requests.get(update_url, params=load, headers=http_header) |
| 41 | if (response.status_code != 200): |
| 42 | print "Oh no, something went wrong! Could not load dataset. Is Asterix running on this host?" |
| 43 | sys.exit() |
| 44 | |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 45 | # Now we obtain the path of this simple python server, and appends src to it (where Black Cherry lives) |
| 46 | path = os.path.dirname(os.path.realpath(__file__)) + "/src" |
| 47 | root = static.File(path) # will be served under '/' |
| 48 | |
| 49 | # reverse proxy, served under '/asterix', with same endpoints as Asterix REST API |
| 50 | # http://test.company.com/service1/v1/JsonService -> becomes http://localhost/svc/service1/v1/JsonService |
| 51 | root.putChild('asterix', proxy.ReverseProxyResource(asterix_host, asterix_port, '')) |
| 52 | |
genia.likes.science@gmail.com | d42b402 | 2013-08-09 05:05:23 -0700 | [diff] [blame] | 53 | print "Please open http://localhost:8080/cherry.html" |
| 54 | |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 55 | # magic |
| 56 | site = server.Site(root) |
| 57 | reactor.listenTCP(8080, site) |
| 58 | reactor.run() |