Make preview host and port configurable in config.rb and also expose those variables to extensions which are curious. Closes #1477

This commit is contained in:
Thomas Reynolds 2015-03-01 18:17:22 -08:00
parent d60cd3a044
commit a71589becd
4 changed files with 18 additions and 6 deletions

View file

@ -1,3 +1,5 @@
with_layout false do with_layout false do
page "/spaces in file.html" page "/spaces in file.html"
end end
config[:port] = 5555

View file

@ -67,6 +67,14 @@ module Middleman
end end
delegate :root_path, to: :"self.class" delegate :root_path, to: :"self.class"
# Which host preview should start on.
# @return [Fixnum]
config.define_setting :host, '0.0.0.0', 'The preview server host'
# Which port preview should start on.
# @return [Fixnum]
config.define_setting :port, 4567, 'The preview server port'
# Name of the source directory # Name of the source directory
# @return [String] # @return [String]
config.define_setting :source, 'source', 'Name of the source directory' config.define_setting :source, 'source', 'Name of the source directory'

View file

@ -14,11 +14,9 @@ module Middleman::Cli
method_option :host, method_option :host,
type: :string, type: :string,
aliases: '-h', aliases: '-h',
default: '0.0.0.0',
desc: 'Bind to HOST address' desc: 'Bind to HOST address'
method_option :port, method_option :port,
aliases: '-p', aliases: '-p',
default: '4567',
desc: 'The port Middleman will listen on' desc: 'The port Middleman will listen on'
method_option :verbose, method_option :verbose,
type: :boolean, type: :boolean,

View file

@ -5,8 +5,6 @@ require 'middleman-core/logger'
# rubocop:disable GlobalVars # rubocop:disable GlobalVars
module Middleman module Middleman
module PreviewServer module PreviewServer
DEFAULT_PORT = 4567
class << self class << self
attr_reader :app, :host, :port attr_reader :app, :host, :port
delegate :logger, to: :app delegate :logger, to: :app
@ -15,8 +13,6 @@ module Middleman
# @return [void] # @return [void]
def start(opts={}) def start(opts={})
@options = opts @options = opts
@host = @options[:host] || '0.0.0.0'
@port = @options[:port] || DEFAULT_PORT
mount_instance(new_app) mount_instance(new_app)
logger.info "== The Middleman is standing watch at http://#{host}:#{port}" logger.info "== The Middleman is standing watch at http://#{host}:#{port}"
@ -92,6 +88,7 @@ module Middleman
def new_app def new_app
opts = @options.dup opts = @options.dup
server = ::Middleman::Application.server server = ::Middleman::Application.server
# Add in the meta pages application # Add in the meta pages application
@ -107,7 +104,14 @@ module Middleman
) )
config[:environment] = opts[:environment].to_sym if opts[:environment] config[:environment] = opts[:environment].to_sym if opts[:environment]
config[:host] = opts[:host] if opts[:host]
config[:port] = opts[:port] if opts[:port]
end end
@host = @app.config[:host]
@port = @app.config[:port]
@app
end end
def start_file_watcher def start_file_watcher