|
@@ -38,6 +38,13 @@ class HTTPException(Exception):
|
|
|
self.code = code
|
|
self.code = code
|
|
|
self.body = body
|
|
self.body = body
|
|
|
|
|
|
|
|
|
|
+BASE_HEADERS = [
|
|
|
|
|
+ ('Access-Control-Allow-Origin', '*'),
|
|
|
|
|
+ ('Access-Control-Allow-Headers', 'X-Requested-With, X-Request'),
|
|
|
|
|
+]
|
|
|
|
|
+DEFAULT_HEADERS = BASE_HEADERS + [('Content-type', 'application/json')]
|
|
|
|
|
+ERROR_HEADERS = BASE_HEADERS + [('Content-type', 'text/plain')]
|
|
|
|
|
+
|
|
|
def application(environ, start_response):
|
|
def application(environ, start_response):
|
|
|
try:
|
|
try:
|
|
|
split = environ['PATH_INFO'][1:].split('/')
|
|
split = environ['PATH_INFO'][1:].split('/')
|
|
@@ -54,7 +61,7 @@ def application(environ, start_response):
|
|
|
handler = handlers.get(split[2])
|
|
handler = handlers.get(split[2])
|
|
|
if handler:
|
|
if handler:
|
|
|
body = json.dumps(handler(split, query, environ))
|
|
body = json.dumps(handler(split, query, environ))
|
|
|
- start_response('200 OK', [('Content-type', 'application/json')])
|
|
|
|
|
|
|
+ start_response('200 OK', DEFAULT_HEADERS)
|
|
|
return [body]
|
|
return [body]
|
|
|
else:
|
|
else:
|
|
|
print 'no handler for', split
|
|
print 'no handler for', split
|
|
@@ -63,11 +70,11 @@ def application(environ, start_response):
|
|
|
raise HTTPException(404, 'unhandled path: ' + environ['PATH_INFO'])
|
|
raise HTTPException(404, 'unhandled path: ' + environ['PATH_INFO'])
|
|
|
except HTTPException as e:
|
|
except HTTPException as e:
|
|
|
response = '%d %s' % (e.code, httplib.responses[e.code])
|
|
response = '%d %s' % (e.code, httplib.responses[e.code])
|
|
|
- start_response(response, [('Content-type', 'text/plain')])
|
|
|
|
|
|
|
+ start_response(response, ERROR_HEADERS)
|
|
|
return [e.body]
|
|
return [e.body]
|
|
|
except:
|
|
except:
|
|
|
traceback.print_exc()
|
|
traceback.print_exc()
|
|
|
- start_response('500 Internal Server Error', [('Content-type', 'text/plain')])
|
|
|
|
|
|
|
+ start_response('500 Internal Server Error', ERROR_HEADERS)
|
|
|
return ['ruh roh']
|
|
return ['ruh roh']
|
|
|
|
|
|
|
|
def get_data(split, query, environ):
|
|
def get_data(split, query, environ):
|