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