make context optional for oxtornado
This commit is contained in:
parent
c236bef25a
commit
d2465539c8
2 changed files with 16 additions and 9 deletions
|
@ -2,6 +2,7 @@
|
||||||
# vi:si:et:sw=4:sts=4:ts=4
|
# vi:si:et:sw=4:sts=4:ts=4
|
||||||
from __future__ import division, with_statement
|
from __future__ import division, with_statement
|
||||||
|
|
||||||
|
from contextlib import contextmanager
|
||||||
import inspect
|
import inspect
|
||||||
import sys
|
import sys
|
||||||
import json
|
import json
|
||||||
|
@ -17,7 +18,6 @@ from functools import wraps
|
||||||
import logging
|
import logging
|
||||||
logger = logging.getLogger('oxtornado')
|
logger = logging.getLogger('oxtornado')
|
||||||
|
|
||||||
import db
|
|
||||||
|
|
||||||
def json_response(data=None, status=200, text='ok'):
|
def json_response(data=None, status=200, text='ok'):
|
||||||
if not data:
|
if not data:
|
||||||
|
@ -70,9 +70,14 @@ def trim(docstring):
|
||||||
# Return a single string:
|
# Return a single string:
|
||||||
return '\n'.join(trimmed)
|
return '\n'.join(trimmed)
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def defaultcontext():
|
||||||
|
yield
|
||||||
|
|
||||||
@run_async
|
@run_async
|
||||||
def api_task(request, callback):
|
def api_task(context, request, callback):
|
||||||
|
if context == None:
|
||||||
|
context = defaultcontext
|
||||||
action = request.arguments.get('action', [None])[0]
|
action = request.arguments.get('action', [None])[0]
|
||||||
data = request.arguments.get('data', ['{}'])[0]
|
data = request.arguments.get('data', ['{}'])[0]
|
||||||
data = json.loads(data) if data else {}
|
data = json.loads(data) if data else {}
|
||||||
|
@ -87,7 +92,7 @@ def api_task(request, callback):
|
||||||
logger.debug('API %s %s', action, data)
|
logger.debug('API %s %s', action, data)
|
||||||
f = actions.get(action)
|
f = actions.get(action)
|
||||||
if f:
|
if f:
|
||||||
with db.session():
|
with context():
|
||||||
try:
|
try:
|
||||||
response = f(data)
|
response = f(data)
|
||||||
except:
|
except:
|
||||||
|
@ -98,8 +103,8 @@ def api_task(request, callback):
|
||||||
callback(response)
|
callback(response)
|
||||||
|
|
||||||
class ApiHandler(tornado.web.RequestHandler):
|
class ApiHandler(tornado.web.RequestHandler):
|
||||||
def initialize(self):
|
def initialize(self, context=None):
|
||||||
pass
|
self._context = context
|
||||||
|
|
||||||
def get(self):
|
def get(self):
|
||||||
self.write('use POST')
|
self.write('use POST')
|
||||||
|
@ -113,7 +118,7 @@ class ApiHandler(tornado.web.RequestHandler):
|
||||||
self.write('')
|
self.write('')
|
||||||
return
|
return
|
||||||
|
|
||||||
response = yield tornado.gen.Task(api_task, self.request)
|
response = yield tornado.gen.Task(api_task, self._context, self.request)
|
||||||
if not 'status' in response:
|
if not 'status' in response:
|
||||||
response = json_response(response)
|
response = json_response(response)
|
||||||
response = json_dumps(response)
|
response = json_dumps(response)
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# vi:si:et:sw=4:sts=4:ts=4
|
# vi:si:et:sw=4:sts=4:ts=4
|
||||||
|
from __future__ import division, print_function
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
@ -12,6 +13,7 @@ import settings
|
||||||
import websocket
|
import websocket
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
import db
|
||||||
import state
|
import state
|
||||||
import node.server
|
import node.server
|
||||||
import oxtornado
|
import oxtornado
|
||||||
|
@ -53,7 +55,7 @@ def run():
|
||||||
(r'/(.*?)/pdf/', FileHandler),
|
(r'/(.*?)/pdf/', FileHandler),
|
||||||
(r'/(.*?)/txt/', FileHandler),
|
(r'/(.*?)/txt/', FileHandler),
|
||||||
(r'/(.*)/(cover|preview)(\d*).jpg', IconHandler),
|
(r'/(.*)/(cover|preview)(\d*).jpg', IconHandler),
|
||||||
(r'/api/', oxtornado.ApiHandler),
|
(r'/api/', oxtornado.ApiHandler, dict(context=db.session)),
|
||||||
(r'/ws', websocket.Handler),
|
(r'/ws', websocket.Handler),
|
||||||
(r"(.*)", MainHandler),
|
(r"(.*)", MainHandler),
|
||||||
]
|
]
|
||||||
|
@ -91,11 +93,11 @@ def run():
|
||||||
else:
|
else:
|
||||||
host = settings.server['address']
|
host = settings.server['address']
|
||||||
url = 'http://%s:%s/' % (host, settings.server['port'])
|
url = 'http://%s:%s/' % (host, settings.server['port'])
|
||||||
print 'open browser at %s' % url
|
print('open browser at %s', url)
|
||||||
try:
|
try:
|
||||||
state.main.start()
|
state.main.start()
|
||||||
except:
|
except:
|
||||||
print 'shutting down...'
|
print('shutting down...')
|
||||||
|
|
||||||
if state.downloads:
|
if state.downloads:
|
||||||
state.downloads.join()
|
state.downloads.join()
|
||||||
|
|
Loading…
Reference in a new issue