http: Redirect from root to /mopidy/ if no static_dir app

This commit is contained in:
Stein Magnus Jodal 2014-06-22 01:24:57 +02:00
parent 6596871918
commit 9f925570d2
3 changed files with 31 additions and 48 deletions

View File

@ -68,13 +68,16 @@ class HttpFrontend(pykka.ThreadingActor, CoreListener):
request_handlers.extend(self._get_static_request_handlers())
# Either default Mopidy or user defined path to files
static_dir = self.config['http']['static_dir']
data_dir = os.path.join(os.path.dirname(__file__), 'data')
root_handler = (r'/(.*)', handlers.StaticFileHandler, {
'path': static_dir if static_dir else data_dir,
'default_filename': 'index.html'
})
request_handlers.append(root_handler)
if self.config['http']['static_dir']:
request_handlers.append((r'/(.*)', handlers.StaticFileHandler, {
'path': self.config['http']['static_dir'],
'default_filename': 'index.html',
}))
else:
request_handlers.append((r'/', tornado.web.RedirectHandler, {
'url': '/mopidy/',
'permanent': False,
}))
logger.debug(
'HTTP routes from extensions: %s',

View File

@ -1,30 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Mopidy</title>
<link rel="stylesheet" type="text/css" href="mopidy.css">
</head>
<body>
<div class="box focus">
<h1>Mopidy</h1>
<p>This web server is a part of the Mopidy music server. To learn more
about Mopidy, please visit
<a href="http://www.mopidy.com/">www.mopidy.com</a>.</p>
</div>
<div class="box">
<h2>Static content serving</h2>
<p>To see your own content instead of this placeholder page, change the
config value <tt>http/static_dir</tt> to point to the directory
containing your static files. This can be used to host e.g. a pure
HTML/CSS/JavaScript Mopidy client.</p>
<p>If you replace this page with your own content, the Mopidy resources
and APIs at <tt>/mopidy/</tt> will still be available.</p>
</div>
</body>
</html>

View File

@ -39,21 +39,31 @@ class HttpServerTest(tornado.testing.AsyncHTTPTestCase):
return tornado.web.Application(http_frontend._get_request_handlers())
class RootAppTest(HttpServerTest):
def test_should_return_index(self):
response = self.fetch('/', method='GET')
class RootRedirectTest(HttpServerTest):
def test_should_redirect_to_mopidy_app(self):
response = self.fetch('/', method='GET', follow_redirects=False)
self.assertIn(
'This web server is a part of the Mopidy music server.',
tornado.escape.to_unicode(response.body))
self.assertEqual(
response.headers['X-Mopidy-Version'], mopidy.__version__)
self.assertEqual(response.headers['Cache-Control'], 'no-cache')
self.assertEqual(response.code, 302)
self.assertEqual(response.headers['Location'], '/mopidy/')
class LegacyStaticDirAppTest(HttpServerTest):
def get_config(self):
config = super(LegacyStaticDirAppTest, self).get_config()
config['http']['static_dir'] = os.path.dirname(__file__)
return config
def test_should_return_index(self):
response = self.fetch('/', method='GET', follow_redirects=False)
self.assertEqual(response.code, 404, 'No index.html in this dir')
def test_should_return_static_files(self):
response = self.fetch('/mopidy.css', method='GET')
response = self.fetch('/test_server.py', method='GET')
self.assertIn('html {', tornado.escape.to_unicode(response.body))
self.assertIn(
'test_should_return_static_files',
tornado.escape.to_unicode(response.body))
self.assertEqual(
response.headers['X-Mopidy-Version'], mopidy.__version__)
self.assertEqual(response.headers['Cache-Control'], 'no-cache')