Address shortcomings in #1508 by removing the "host" parameter, always binding on all interfaces, and printing the preview URL with both the local hostname and the local public IP address.

This commit is contained in:
Ben Hollis 2015-05-03 17:53:54 -07:00
parent bd67f8ad0e
commit 08dee580aa
4 changed files with 13 additions and 21 deletions

View file

@ -2,8 +2,8 @@ master
=== ===
* The preview server can now serve over HTTPS using the `--https` flag. It will use an automatic self-signed cert which can be overridden using `--ssl_certificate` and `--ssl_private_key`. These settings can also be set in `config.rb` * The preview server can now serve over HTTPS using the `--https` flag. It will use an automatic self-signed cert which can be overridden using `--ssl_certificate` and `--ssl_private_key`. These settings can also be set in `config.rb`
* The preview server URL will use 'localhost' rather than '0.0.0.0'. * The preview server URL will use the local hostname rather than '0.0.0.0'. It will also print out a URL based on the host's public IP in case that's useful.
* The preview server URL will once again use the machine's hostname if available. * The `--host` flag and `config.rb` setting have been removed - the preview server will always bind to all interfaces.
3.3.11 3.3.11
=== ===

View file

@ -1,5 +1,3 @@
require 'socket'
# Using Tilt for templating # Using Tilt for templating
require 'tilt' require 'tilt'
@ -69,10 +67,6 @@ 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, Socket.gethostname, 'The preview server host'
# Which port preview should start on. # Which port preview should start on.
# @return [Fixnum] # @return [Fixnum]
config.define_setting :port, 4567, 'The preview server port' config.define_setting :port, 4567, 'The preview server port'

View file

@ -11,10 +11,6 @@ module Middleman::Cli
aliases: '-e', aliases: '-e',
default: ENV['MM_ENV'] || ENV['RACK_ENV'] || 'development', default: ENV['MM_ENV'] || ENV['RACK_ENV'] || 'development',
desc: 'The environment Middleman will run under' desc: 'The environment Middleman will run under'
method_option :host,
type: :string,
aliases: '-h',
desc: 'Bind to HOST address'
method_option :port, method_option :port,
aliases: '-p', aliases: '-p',
desc: 'The port Middleman will listen on' desc: 'The port Middleman will listen on'
@ -69,7 +65,6 @@ module Middleman::Cli
params = { params = {
port: options['port'], port: options['port'],
host: options['host'],
https: options['https'], https: options['https'],
ssl_certificate: options['ssl_certificate'], ssl_certificate: options['ssl_certificate'],
ssl_private_key: options['ssl_private_key'], ssl_private_key: options['ssl_private_key'],

View file

@ -1,6 +1,7 @@
require 'webrick' require 'webrick'
require 'webrick/https' require 'webrick/https'
require 'openssl' require 'openssl'
require 'socket'
require 'middleman-core/meta_pages' require 'middleman-core/meta_pages'
require 'middleman-core/logger' require 'middleman-core/logger'
@ -21,7 +22,7 @@ module Middleman
@options = opts @options = opts
mount_instance(new_app) mount_instance(new_app)
logger.info "== The Middleman is standing watch at #{uri}" logger.info "== The Middleman is standing watch at #{uri} (#{uri(public_ip)})"
logger.info "== Inspect your site configuration at #{uri + '__middleman'}" logger.info "== Inspect your site configuration at #{uri + '__middleman'}"
@initialized ||= false @initialized ||= false
@ -110,14 +111,12 @@ 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] config[:port] = opts[:port] if opts[:port]
config[:https] = opts[:https] unless opts[:https].nil? config[:https] = opts[:https] unless opts[:https].nil?
config[:ssl_certificate] = opts[:ssl_certificate] if opts[:ssl_certificate] config[:ssl_certificate] = opts[:ssl_certificate] if opts[:ssl_certificate]
config[:ssl_private_key] = opts[:ssl_private_key] if opts[:ssl_private_key] config[:ssl_private_key] = opts[:ssl_private_key] if opts[:ssl_private_key]
end end
@host = @app.config[:host]
@port = @app.config[:port] @port = @app.config[:port]
@https = @app.config[:https] @https = @app.config[:https]
@ -180,7 +179,6 @@ module Middleman
# @return [void] # @return [void]
def setup_webrick(is_logging) def setup_webrick(is_logging)
http_opts = { http_opts = {
BindAddress: host,
Port: port, Port: port,
AccessLog: [], AccessLog: [],
DoNotReverseLookup: true DoNotReverseLookup: true
@ -197,7 +195,7 @@ module Middleman
# use a generated self-signed cert # use a generated self-signed cert
http_opts[:SSLCertName] = [ http_opts[:SSLCertName] = [
%w(CN localhost), %w(CN localhost),
%w(CN #{host}) %w(CN #{Socket.gethostname})
].uniq ].uniq
end end
end end
@ -266,10 +264,15 @@ module Middleman
# Returns the URI the preview server will run on # Returns the URI the preview server will run on
# @return [URI] # @return [URI]
def uri def uri(host = Socket.gethostname)
host = @host == '0.0.0.0' ? 'localhost' : @host
scheme = https? ? 'https' : 'http' scheme = https? ? 'https' : 'http'
URI("#{scheme}://#{host}:#{@port}") URI("#{scheme}://#{host}:#{@port}/")
end
# An IPv4 address on this machine which should be externally addressable.
# @return [String]
def public_ip
Socket.ip_address_list.find { |ai| ai.ipv4? && !ai.ipv4_loopback? }.ip_address
end end
end end