/
__init__.py
76 lines (62 loc) · 1.83 KB
/
__init__.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import collections
import os
import urlparse
import web
from musashi import analytics
from musashi import api_handler
APP_ROOT = os.path.dirname(__file__)
render = web.template.render(
os.path.join(APP_ROOT, 'templates'))
db = None
if 'HEROKU_SHARED_POSTGRESQL_AQUA_URL' in os.environ:
db_url = urlparse.urlparse('SHARED_DATABASE_URL')
db_params = {
'dbn': db_url.scheme,
'db': db_url.path[1:],
'user': db_url.username,
'pw': db_url.password,
'host': db_url.hostname,
'port': db_url.port
}
db = web.database(**db_params)
else:
db = web.database(dbn='postgres', user='tester',
pw='testing', db='musashi-dev')
urls = (
'/', 'index',
'/analyze', 'analyze',
'/api/(.*)', 'api',
'/(favicon.ico)', 'static',
'/s/(.*)', 'static'
)
class index(object):
def GET(self):
tracks = db.select('tracks', order='sequence')
tracks_dict = collections.defaultdict(list)
for track in tracks:
tracks_dict[track.sequence].append(track)
return render.index(tracks_dict)
analyze = analytics.Analyzer
analyze.db = db
analyze.render = render
api = api_handler.Api
api.db = db
class static(object):
content_types = {
'.js': 'text/javascript',
'.css': 'text/css',
'.ico': 'image/x-icon'
}
def GET(self, path):
to_serve = os.path.join(APP_ROOT, 'static',
os.path.normpath(path))
_, ext = os.path.splitext(to_serve)
try:
web.header('Content-type', self.content_types[ext])
except KeyError:
web.header('Content-type', 'text/plain')
return open(to_serve).read()
app = web.application(urls, globals())
run = app.wsgifunc()
if __name__ == '__main__':
app.run()