From 8a8a78e025cf30bba4460d714668a3148d6e9940 Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Fri, 5 Apr 2013 15:52:38 +0200 Subject: [PATCH] http: Use new config system --- mopidy/frontends/http/__init__.py | 28 ++++++++++++++-------------- mopidy/frontends/http/actor.py | 11 ++++++----- tests/frontends/http/events_test.py | 11 ++++++++++- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/mopidy/frontends/http/__init__.py b/mopidy/frontends/http/__init__.py index d588a376..1b93fb8a 100644 --- a/mopidy/frontends/http/__init__.py +++ b/mopidy/frontends/http/__init__.py @@ -61,19 +61,19 @@ Setup The frontend is enabled by default if all dependencies are available. -When it is enabled it starts a web server at the port specified by -:attr:`mopidy.settings.HTTP_SERVER_PORT`. +When it is enabled it starts a web server at the port specified by the +``http/port`` config value. .. warning:: Security As a simple security measure, the web server is by default only available - from localhost. To make it available from other computers, change - :attr:`mopidy.settings.HTTP_SERVER_HOSTNAME`. Before you do so, note that - the HTTP frontend does not feature any form of user authentication or - authorization. Anyone able to access the web server can use the full core - API of Mopidy. Thus, you probably only want to make the web server - available from your local network or place it behind a web proxy which - takes care or user authentication. You have been warned. + from localhost. To make it available from other computers, change the + ``http/hostname`` config value. Before you do so, note that the HTTP + frontend does not feature any form of user authentication or authorization. + Anyone able to access the web server can use the full core API of Mopidy. + Thus, you probably only want to make the web server available from your + local network or place it behind a web proxy which takes care or user + authentication. You have been warned. Using a web based Mopidy client @@ -81,10 +81,11 @@ Using a web based Mopidy client The web server can also host any static files, for example the HTML, CSS, JavaScript, and images needed for a web based Mopidy client. To host static -files, change :attr:`mopidy.settings.HTTP_SERVER_STATIC_DIR` to point to the -root directory of your web client, e.g.:: +files, change the ``http/static_dir`` to point to the root directory of your +web client, e.g.:: - HTTP_SERVER_STATIC_DIR = u'/home/alice/dev/the-client' + [http] + static_dir = /home/alice/dev/the-client If the directory includes a file named ``index.html``, it will be served on the root of Mopidy's web server. @@ -405,8 +406,7 @@ Example to get started with 2. Create an empty directory for your web client. -3. Change the setting :attr:`mopidy.settings.HTTP_SERVER_STATIC_DIR` to point - to your new directory. +3. Change the ``http/static_dir`` config value to point to your new directory. 4. Start/restart Mopidy. diff --git a/mopidy/frontends/http/actor.py b/mopidy/frontends/http/actor.py index 54085471..149cbc7f 100644 --- a/mopidy/frontends/http/actor.py +++ b/mopidy/frontends/http/actor.py @@ -6,7 +6,7 @@ import os import pykka -from mopidy import exceptions, models, settings +from mopidy import exceptions, models from mopidy.core import CoreListener try: @@ -25,6 +25,7 @@ logger = logging.getLogger('mopidy.frontends.http') class HttpFrontend(pykka.ThreadingActor, CoreListener): def __init__(self, config, core): super(HttpFrontend, self).__init__() + self.config = config self.core = core self._setup_server() self._setup_websocket_plugin() @@ -35,8 +36,8 @@ class HttpFrontend(pykka.ThreadingActor, CoreListener): cherrypy.config.update({ 'engine.autoreload_on': False, 'server.socket_host': ( - settings.HTTP_SERVER_HOSTNAME.encode('utf-8')), - 'server.socket_port': settings.HTTP_SERVER_PORT, + self.config['http']['hostname'].encode('utf-8')), + 'server.socket_port': self.config['http']['port'], }) def _setup_websocket_plugin(self): @@ -48,8 +49,8 @@ class HttpFrontend(pykka.ThreadingActor, CoreListener): root.mopidy = MopidyResource() root.mopidy.ws = ws.WebSocketResource(self.core) - if settings.HTTP_SERVER_STATIC_DIR: - static_dir = settings.HTTP_SERVER_STATIC_DIR + if self.config['http']['static_dir']: + static_dir = self.config['http']['static_dir'] else: static_dir = os.path.join(os.path.dirname(__file__), 'data') logger.debug('HTTP server will serve "%s" at /', static_dir) diff --git a/tests/frontends/http/events_test.py b/tests/frontends/http/events_test.py index 7661ac6e..c334eefa 100644 --- a/tests/frontends/http/events_test.py +++ b/tests/frontends/http/events_test.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import json try: @@ -24,7 +26,14 @@ from tests import unittest @mock.patch('cherrypy.engine.publish') class HttpEventsTest(unittest.TestCase): def setUp(self): - self.http = actor.HttpFrontend(config=None, core=mock.Mock()) + config = { + 'http': { + 'hostname': '127.0.0.1', + 'port': 6680, + 'static_dir': None, + } + } + self.http = actor.HttpFrontend(config=config, core=mock.Mock()) def test_track_playback_paused_is_broadcasted(self, publish): publish.reset_mock()