Allow setting host in Invoke serve & livereload tasks

Instead of serving on localhost by default with no way to override, the
host can now be configured, allowing both `serve` and `livereload` tasks
to serve output on non-localhost addresses such as `0.0.0.0`.
This commit is contained in:
Justin Mayer 2020-04-30 15:21:04 +02:00
commit d9809c34fc

View file

@ -39,7 +39,8 @@ CONFIG = {
'github_pages_branch': '{{github_pages_branch}}', 'github_pages_branch': '{{github_pages_branch}}',
'commit_message': "'Publish site on {}'".format(datetime.date.today().isoformat()), 'commit_message': "'Publish site on {}'".format(datetime.date.today().isoformat()),
{% endif %} {% endif %}
# Port for `serve` # Host and port for `serve`
'host': 'localhost',
'port': 8000, 'port': 8000,
} }
@ -67,17 +68,17 @@ def regenerate(c):
@task @task
def serve(c): def serve(c):
"""Serve site at http://localhost:$PORT/ (default port is 8000)""" """Serve site at http://$HOST:$PORT/ (default is localhost:8000)"""
class AddressReuseTCPServer(RootedHTTPServer): class AddressReuseTCPServer(RootedHTTPServer):
allow_reuse_address = True allow_reuse_address = True
server = AddressReuseTCPServer( server = AddressReuseTCPServer(
CONFIG['deploy_path'], CONFIG['deploy_path'],
('', CONFIG['port']), (CONFIG['host'], CONFIG['port']),
ComplexHTTPRequestHandler) ComplexHTTPRequestHandler)
sys.stderr.write('Serving on port {port} ...\n'.format(**CONFIG)) sys.stderr.write('Serving at {host}:{port} ...\n'.format(**CONFIG))
server.serve_forever() server.serve_forever()
@task @task
@ -111,8 +112,8 @@ def livereload(c):
for extension in static_file_extensions: for extension in static_file_extensions:
static_file = '{0}/static/**/*{1}'.format(theme_path, extension) static_file = '{0}/static/**/*{1}'.format(theme_path, extension)
server.watch(static_file, lambda: build(c)) server.watch(static_file, lambda: build(c))
# Serve output path on configured port # Serve output path on configured host and port
server.serve(port=CONFIG['port'], root=CONFIG['deploy_path']) server.serve(host=CONFIG['host'], port=CONFIG['port'], root=CONFIG['deploy_path'])
{% if cloudfiles %} {% if cloudfiles %}
@task @task