Skip to content

Commit

Permalink
Option to change the livereload host
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas Walch committed Sep 9, 2016
1 parent 47dd560 commit a99d1e9
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 16 deletions.
12 changes: 8 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,27 @@ Add ``'livereload.middleware.LiveReloadScript'`` to the
'livereload.middleware.LiveReloadScript',
)

This will inject the ``livereload.js`` script into your webpages if ``DEBUG`` setting is on.

Configuration
-------------
If you need the livereload server to use a different port than the default 35729,
specify it by setting ``LIVERELOAD_PORT`` in ``settings.py``.
If you need the livereload server to use a different host and port than the default 127.0.0.1 and 35729,
specify them by setting ``LIVERELOAD_HOST`` and ``LIVERELOAD_PORT`` in ``settings.py``.

Usage
-----
Start the livereload server with: ::

$ ./manage.py livereload

Extra files and/or paths to watch for changes can be added as positional arguments.
Extra files and/or paths to watch for changes can be added as positional arguments. Host and port can be overridden with
``--host`` and ``port`` options.

Start the development server as usual with ``./manage.py runserver``. The command now accepts two additional
Start the development server as usual with ``./manage.py runserver``. The command now accepts three additional
options:

* ``--nolivereload`` to disable livereload functionality
* ``--livereload-host`` to override both default and settings file specified host address
* ``--livereload-port`` to override both default and settings file specified port

Background
Expand Down
6 changes: 5 additions & 1 deletion livereload/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""django-livereload"""
__version__ = '0.2.1'
__version__ = '0.2.2'
__license__ = 'BSD License'

__author__ = 'Tomas Walch'
Expand All @@ -12,3 +12,7 @@ def livereload_port():
from django.conf import settings
return int(getattr(settings, 'LIVERELOAD_PORT', 35729))


def livereload_host():
from django.conf import settings
return getattr(settings, 'LIVERELOAD_HOST', '127.0.0.1')
11 changes: 7 additions & 4 deletions livereload/management/commands/livereload.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.conf import settings
from django.apps import apps
from django.core.management.base import BaseCommand
from ... import livereload_port, server as S
from ... import livereload_port, server as S, livereload_host


class Command(BaseCommand):
Expand All @@ -18,7 +18,10 @@ def add_arguments(self, parser):
help='Extra files or directories to watch',
)
parser.add_argument(
'--host', dest='host', default='127.0.0.1', help='Host address for livereload sever.'
'--host', dest='host', default=livereload_host(), help='Host address for livereload sever.'
)
parser.add_argument(
'--port', dest='port', default=livereload_port(), help='Listening port for livereload sever.'
)

def handle(self, *args, **options):
Expand All @@ -40,6 +43,6 @@ def handle(self, *args, **options):
server.watch(os.path.join(app_config.path, 'templates'))

server.serve(
host=options.get('host', '127.0.0.1'),
liveport=livereload_port(),
host=options['host'],
liveport=options['port'],
)
14 changes: 11 additions & 3 deletions livereload/management/commands/runserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from django.core.management.commands.runserver import \
Command as RunserverCommand

from livereload import livereload_port
from livereload import livereload_port, livereload_host


class Command(RunserverCommand):
Expand All @@ -30,7 +30,12 @@ class Command(RunserverCommand):
('--livereload-port',
{'action': 'store', 'dest': 'livereload_port', 'type': int,
'default': livereload_port(),
'help': 'Port where LiveReload listen.'})]
'help': 'Port where LiveReload listens.'}),
('--livereload-host',
{'action': 'store', 'dest': 'livereload_host', 'type': str,
'default': livereload_host(),
'help': 'Address to LiveReload server.'})
]

def add_arguments(self, parser):
super(Command, self).add_arguments(parser)
Expand All @@ -51,7 +56,10 @@ def livereload_request(self, **options):
"""
style = color_style()
verbosity = int(options['verbosity'])
host = 'localhost:%s' % options['livereload_port']
host = '%s:%d' % (
options['livereload_host'],
options['livereload_port'],
)
try:
urlopen('http://%s/forcereload' % host)
self.message('LiveReload request emitted.\n',
Expand Down
12 changes: 8 additions & 4 deletions livereload/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from django.utils.encoding import smart_str
from django.conf import settings
from livereload import livereload_port
from livereload import livereload_port, livereload_host


class LiveReloadScript(object):
Expand All @@ -25,13 +25,17 @@ def process_response(self, request, response):
'html.parser',
)

if not getattr(soup, 'head', None):
head = getattr(soup, 'head', None)
if not head:
return response

script = soup.new_tag(
'script', src='http://localhost:%d/livereload.js' % livereload_port(),
'script', src='http://%s:%d/livereload.js' % (
livereload_host(),
livereload_port(),
)
)
soup.head.append(script)
head.append(script)

response.content = str(soup)

Expand Down

0 comments on commit a99d1e9

Please sign in to comment.