summaryrefslogtreecommitdiff
path: root/app.py
blob: 0358b963756f3556010f9fda5227ef6e5b46a56c (plain)
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
76
77
78
79
#!/usr/bin/env python2

import db, os, json, mimetypes
from config import config

class JSONApplication(object):
	def list(self, environ, start_response, path):
		root = os.path.join(config.get('music_root'), '/'.join(path[1:]))
		if root[-1] == '/':
			root = root[:-1]
		session = db.Session()
		try:
			directory = db.Directory.get(session, root)
			directories = directory.children
			tracks = directory.tracks
			contents = json.dumps([x.dict() for x in directories] +
				[x.dict() for x in tracks])
		finally:
			session.close()
		start_response('200 OK', [('Content-Type', 'application/json'), ('Content-Length', str(len(contents)))])
		return [contents]

	handlers = {
		'list': list,
	}

	def __call__(self, environ, start_response, path):
		module = path[0]
		if module in self.handlers:
			return self.handlers[module](self, environ, start_response, path)

class Application(object):
	def __init__(self):
		self.jsonapp = JSONApplication()

	def json(self, environ, start_response, path):
		path.pop(0)
		return self.jsonapp(environ, start_response, path)

	def static(self, environ, start_response, path):
		filename = os.path.join('static', *path[1:])

		if not os.path.exists(filename) or '..' in path:
			start_response('404 Not Found', [])
			return []

		mime = mimetypes.guess_type(filename, strict = False)[0] or 'application/octet-stream'
		start_response('200 OK', [('Content-Type', mime)])
		return open(filename, 'rb')

	handlers = {
		'json': json,
		'static': static,
	}

	def __call__(self, environ, start_response):
		path = environ['PATH_INFO'].split('/')[1:]
		module = path[0] or None
		if not module:
			module = 'static'
			path = ['static', 'index.html']

		if module in self.handlers:
			return self.handlers[module](self, environ, start_response, path)

		#start_response('200 OK', [('Content-Type', 'text/html')])
		#return open('static/index.html', 'r')

if __name__ == '__main__':
	import sys
	if len(sys.argv) == 3:
		from flup.server.fcgi import WSGIServer
		WSGIServer(Application(), bindAddress = (sys.argv[1], int(sys.argv[2]))).run()
	else:
		from wsgiref.simple_server import make_server, WSGIServer
		# enable IPv6
		WSGIServer.address_family |= 10
		httpd = make_server('', 8000, Application())
		httpd.serve_forever()