This repository has been archived by the owner on Sep 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rest-api.py
53 lines (47 loc) · 1.57 KB
/
rest-api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import json
import cherrypy
import DftTraffic
import os
import sys
import logging
class JourneyTime(object):
dft = None
env = None
def __init__(self):
self.dft = DftTraffic.DftTraffic()
self.dft.logger = logging.getLogger(__name__)
self.env = json.loads(os.getenv('VCAP_APPLICATION','{}'))
@cherrypy.tools.json_out()
@cherrypy.expose
def route(self,route_id=1):
cherrypy.response.headers['Access-Control-Allow-Origin'] = '*'
return self.dft.journey_times(route_id)
@cherrypy.tools.json_out()
@cherrypy.expose
def sections(self,search):
output = []
hostname = ''
results = self.dft.find_section(search)
if 'application_uris' in self.env:
hostname = self.env['application_uris'][0]
for result in results:
output.append({'path':"%s/route/%s" % (hostname,result),'description':results[result]})
cherrypy.response.headers['Access-Control-Allow-Origin'] = '*'
return output
current_dir = os.path.dirname(os.path.abspath(__file__))
conf = {
'global': {
'server.socket_host': '0.0.0.0',
'server.socket_port': int(os.getenv('VCAP_APP_PORT',8004)),
},
'/': {
'tools.staticdir.on': True,
'tools.staticdir.index': 'index.html',
'tools.staticdir.dir': os.path.join(current_dir, 'docs'),
'tools.staticdir.content_types': {
'yaml': 'application/x-yaml'
}
}
}
logging.basicConfig(stream = sys.stderr, level=logging.WARNING)
cherrypy.quickstart(JourneyTime(), '/', config=conf)