http: Remove static file serving from the Router
This commit is contained in:
parent
eea6138b3d
commit
d13194dfa4
@ -62,58 +62,28 @@ class Router(object):
|
|||||||
|
|
||||||
Must be overridden by all subclasses.
|
Must be overridden by all subclasses.
|
||||||
|
|
||||||
This should be the same as the ``ext_name`` of the Mopidy extension
|
This should be the same as the :attr:`~mopidy.ext.Extension.ext_name` of
|
||||||
implementing an HTTP router. The :attr:`~Router.name` will be used to
|
the Mopidy extension implementing an HTTP router. The :attr:`~Router.name`
|
||||||
namespace all URLs handled by this router.
|
will be used to namespace all URLs handled by this router.
|
||||||
|
|
||||||
For example, if :attr:`~Router.name` is ``soundspot``, then the router will
|
For example, if :attr:`~Router.name` is ``soundspot``, then the router will
|
||||||
manage all requests starting with ``http://localhost:6680/soundspot``.
|
manage all requests starting with ``http://localhost:6680/soundspot``.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
static_file_path = None
|
|
||||||
"""Path to location of static files to be served.
|
|
||||||
|
|
||||||
If you only need to serve static files, set this attribute and use the
|
|
||||||
default implementation of :meth:`~Router.get_request_handlers`.
|
|
||||||
|
|
||||||
If you override :meth:`~Router.get_request_handlers` this attribute is not
|
|
||||||
used.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, config, core):
|
def __init__(self, config, core):
|
||||||
self.config = config
|
self.config = config
|
||||||
self.core = core
|
self.core = core
|
||||||
self.hostname = config['http']['hostname']
|
|
||||||
self.port = config['http']['port']
|
|
||||||
if not self.name:
|
if not self.name:
|
||||||
raise ValueError('Undefined router name in %s' % self)
|
raise ValueError('Router name must be set')
|
||||||
|
|
||||||
def get_root_url(self):
|
|
||||||
"""Get the absolute URL to the root of this router."""
|
|
||||||
return 'http://%s:%s/%s/' % (self.hostname, self.port, self.name)
|
|
||||||
|
|
||||||
def get_request_handlers(self):
|
def get_request_handlers(self):
|
||||||
"""
|
"""
|
||||||
Get request handlers for the URL namespace owned by this router.
|
Get request handlers for the URL namespace owned by this router.
|
||||||
|
|
||||||
The default implementation of this method serves static files from
|
Must be overridden by all subclasses.
|
||||||
:attr:`static_file_path`. To extend the HTTP server with more
|
|
||||||
server side functionality, this method must be overridden.
|
|
||||||
|
|
||||||
Must return a list of request handlers compatible with
|
Returns a list of request handlers compatible with
|
||||||
:class:`tornado.web.Application`. The URL patterns should not include
|
:class:`tornado.web.Application`. The URL patterns should not include
|
||||||
the :attr:`name` prefix, as that will be prepended by the web server.
|
the :attr:`name` prefix, as that will be prepended by the web server.
|
||||||
"""
|
"""
|
||||||
if self.static_file_path is None:
|
raise NotImplementedError
|
||||||
raise ValueError('Undefined static file path in %s' % self)
|
|
||||||
|
|
||||||
from mopidy.http.handlers import StaticFileHandler
|
|
||||||
|
|
||||||
logger.info(
|
|
||||||
'Serving HTTP extension %s at %s', type(self), self.get_root_url())
|
|
||||||
return [
|
|
||||||
(r'/(.*)', StaticFileHandler, {
|
|
||||||
'path': self.static_file_path,
|
|
||||||
'default_filename': 'index.html'
|
|
||||||
}),
|
|
||||||
]
|
|
||||||
|
|||||||
@ -6,7 +6,6 @@ import unittest
|
|||||||
import mock
|
import mock
|
||||||
|
|
||||||
from mopidy import http
|
from mopidy import http
|
||||||
from mopidy.http import handlers
|
|
||||||
|
|
||||||
|
|
||||||
class TestRouter(http.Router):
|
class TestRouter(http.Router):
|
||||||
@ -14,12 +13,8 @@ class TestRouter(http.Router):
|
|||||||
static_file_path = os.path.join(os.path.dirname(__file__), 'static')
|
static_file_path = os.path.join(os.path.dirname(__file__), 'static')
|
||||||
|
|
||||||
|
|
||||||
class TestRouterMissingPath(http.Router):
|
|
||||||
name = 'test'
|
|
||||||
|
|
||||||
|
|
||||||
class TestRouterMissingName(http.Router):
|
class TestRouterMissingName(http.Router):
|
||||||
static_file_path = os.path.join(os.path.dirname(__file__), 'static')
|
pass
|
||||||
|
|
||||||
|
|
||||||
class HttpRouterTest(unittest.TestCase):
|
class HttpRouterTest(unittest.TestCase):
|
||||||
@ -40,27 +35,12 @@ class HttpRouterTest(unittest.TestCase):
|
|||||||
self.assertIs(router.config, self.config)
|
self.assertIs(router.config, self.config)
|
||||||
self.assertIs(router.core, self.core)
|
self.assertIs(router.core, self.core)
|
||||||
|
|
||||||
def test_default_request_handlers(self):
|
def test_undefined_name_raises_error(self):
|
||||||
router = TestRouter(self.config, self.core)
|
|
||||||
|
|
||||||
(pattern, handler_class, kwargs) = router.get_request_handlers()[0]
|
|
||||||
|
|
||||||
self.assertEqual(pattern, r'/(.*)')
|
|
||||||
self.assertIs(handler_class, handlers.StaticFileHandler)
|
|
||||||
self.assertEqual(
|
|
||||||
kwargs['path'], os.path.join(os.path.dirname(__file__), 'static'))
|
|
||||||
|
|
||||||
def test_default_router_missing_name(self):
|
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
TestRouterMissingName(self.config, self.core)
|
TestRouterMissingName(self.config, self.core)
|
||||||
|
|
||||||
def test_default_router_missing_path(self):
|
def test_undefined_request_handlers_raises_error(self):
|
||||||
router = TestRouterMissingPath(self.config, self.core)
|
|
||||||
|
|
||||||
with self.assertRaises(ValueError):
|
|
||||||
router.get_request_handlers()
|
|
||||||
|
|
||||||
def test_get_root_url(self):
|
|
||||||
router = TestRouter(self.config, self.core)
|
router = TestRouter(self.config, self.core)
|
||||||
|
|
||||||
self.assertEqual('http://127.0.0.1:6680/test/', router.get_root_url())
|
with self.assertRaises(NotImplementedError):
|
||||||
|
router.get_request_handlers()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user