http: Avoid tornado import before extension is loaded, group all handlers
This commit is contained in:
parent
acad8ab7a2
commit
ea5a317b00
@ -3,8 +3,6 @@ from __future__ import unicode_literals
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import tornado.web
|
|
||||||
|
|
||||||
import mopidy
|
import mopidy
|
||||||
from mopidy import config as config_lib, exceptions, ext
|
from mopidy import config as config_lib, exceptions, ext
|
||||||
|
|
||||||
@ -42,13 +40,6 @@ class Extension(ext.Extension):
|
|||||||
registry.add('frontend', HttpFrontend)
|
registry.add('frontend', HttpFrontend)
|
||||||
|
|
||||||
|
|
||||||
class StaticFileHandler(tornado.web.StaticFileHandler):
|
|
||||||
def set_extra_headers(self, path):
|
|
||||||
self.set_header('Cache-Control', 'no-cache')
|
|
||||||
self.set_header(
|
|
||||||
'X-Mopidy-Version', mopidy.__version__.encode('utf-8'))
|
|
||||||
|
|
||||||
|
|
||||||
class Router(object):
|
class Router(object):
|
||||||
"""
|
"""
|
||||||
HTTP router interface.
|
HTTP router interface.
|
||||||
@ -104,9 +95,11 @@ class Router(object):
|
|||||||
Must return a list of request handlers compatible with
|
Must return a list of request handlers compatible with
|
||||||
:class:`tornado.web.Application`.
|
:class:`tornado.web.Application`.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if self.static_file_path is None:
|
if self.static_file_path is None:
|
||||||
raise ValueError('Undefined static file path in %s' % self)
|
raise ValueError('Undefined static file path in %s' % self)
|
||||||
|
|
||||||
|
from mopidy.http.handlers import StaticFileHandler
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
'Serving HTTP extension %s at %s', type(self), self.get_root_url())
|
'Serving HTTP extension %s at %s', type(self), self.get_root_url())
|
||||||
return [
|
return [
|
||||||
|
|||||||
@ -13,7 +13,7 @@ import tornado.websocket
|
|||||||
|
|
||||||
from mopidy import models, zeroconf
|
from mopidy import models, zeroconf
|
||||||
from mopidy.core import CoreListener
|
from mopidy.core import CoreListener
|
||||||
from mopidy.http import StaticFileHandler, handlers
|
from mopidy.http import handlers
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@ -54,7 +54,7 @@ class HttpFrontend(pykka.ThreadingActor, CoreListener):
|
|||||||
static_dir = self.config['http']['static_dir']
|
static_dir = self.config['http']['static_dir']
|
||||||
|
|
||||||
# either default mopidy or user defined path to files
|
# either default mopidy or user defined path to files
|
||||||
primary_dir = (r'/(.*)', StaticFileHandler, {
|
primary_dir = (r'/(.*)', handlers.StaticFileHandler, {
|
||||||
'path': static_dir if static_dir else mopidy_dir,
|
'path': static_dir if static_dir else mopidy_dir,
|
||||||
'default_filename': 'index.html'
|
'default_filename': 'index.html'
|
||||||
})
|
})
|
||||||
@ -69,7 +69,7 @@ class HttpFrontend(pykka.ThreadingActor, CoreListener):
|
|||||||
routes.extend([
|
routes.extend([
|
||||||
(r'/mopidy/ws/?', handlers.WebSocketHandler, {'actor': self}),
|
(r'/mopidy/ws/?', handlers.WebSocketHandler, {'actor': self}),
|
||||||
(r'/mopidy/rpc', handlers.JsonRpcHandler, {'actor': self}),
|
(r'/mopidy/rpc', handlers.JsonRpcHandler, {'actor': self}),
|
||||||
(r'/mopidy/(.*)', StaticFileHandler, {
|
(r'/mopidy/(.*)', handlers.StaticFileHandler, {
|
||||||
'path': mopidy_dir, 'default_filename': 'mopidy.html'
|
'path': mopidy_dir, 'default_filename': 'mopidy.html'
|
||||||
}),
|
}),
|
||||||
primary_dir,
|
primary_dir,
|
||||||
|
|||||||
@ -115,3 +115,10 @@ class JsonRpcHandler(tornado.web.RequestHandler):
|
|||||||
self.set_header(
|
self.set_header(
|
||||||
'X-Mopidy-Version', mopidy.__version__.encode('utf-8'))
|
'X-Mopidy-Version', mopidy.__version__.encode('utf-8'))
|
||||||
self.set_header('Content-Type', 'application/json; utf-8')
|
self.set_header('Content-Type', 'application/json; utf-8')
|
||||||
|
|
||||||
|
|
||||||
|
class StaticFileHandler(tornado.web.StaticFileHandler):
|
||||||
|
def set_extra_headers(self, path):
|
||||||
|
self.set_header('Cache-Control', 'no-cache')
|
||||||
|
self.set_header(
|
||||||
|
'X-Mopidy-Version', mopidy.__version__.encode('utf-8'))
|
||||||
|
|||||||
@ -11,6 +11,7 @@ from tornado.web import Application
|
|||||||
|
|
||||||
import mopidy
|
import mopidy
|
||||||
from mopidy import http
|
from mopidy import http
|
||||||
|
from mopidy.http import handlers
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -51,11 +52,10 @@ class HttpRouterTest(unittest.TestCase):
|
|||||||
def test_default_router(self):
|
def test_default_router(self):
|
||||||
router = TestRouter(self.config)
|
router = TestRouter(self.config)
|
||||||
|
|
||||||
handlers = router.get_request_handlers()
|
(pattern, handler_class, kwargs) = router.get_request_handlers()[0]
|
||||||
(pattern, handler_class, kwargs) = handlers[0]
|
|
||||||
|
|
||||||
self.assertEqual(pattern, r'/test/(.*)')
|
self.assertEqual(pattern, r'/test/(.*)')
|
||||||
self.assertIs(handler_class, http.StaticFileHandler)
|
self.assertIs(handler_class, handlers.StaticFileHandler)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
kwargs['path'], os.path.join(os.path.dirname(__file__), 'static'))
|
kwargs['path'], os.path.join(os.path.dirname(__file__), 'static'))
|
||||||
|
|
||||||
@ -74,7 +74,7 @@ class HttpRouterTest(unittest.TestCase):
|
|||||||
|
|
||||||
class StaticFileHandlerTest(AsyncHTTPTestCase):
|
class StaticFileHandlerTest(AsyncHTTPTestCase):
|
||||||
def get_app(self):
|
def get_app(self):
|
||||||
app = Application([(r'/(.*)', http.StaticFileHandler, {
|
app = Application([(r'/(.*)', handlers.StaticFileHandler, {
|
||||||
'path': os.path.dirname(__file__),
|
'path': os.path.dirname(__file__),
|
||||||
'default_filename': 'test_router.py'
|
'default_filename': 'test_router.py'
|
||||||
})])
|
})])
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user