Merge pull request #1509 from bhollis/localhost

Address shortcomings in #1508
v3-stable
Thomas Reynolds 2015-05-03 21:39:34 -07:00
commit 08c3969eb3
4 changed files with 14 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 URL will use 'localhost' rather than '0.0.0.0'.
* The preview server URL will once again use the machine's hostname if available.
* 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 `--host` flag and `config.rb` setting have been removed - the preview server will always bind to all interfaces.
3.3.11
===

View File

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

View File

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

View File

@ -1,6 +1,7 @@
require 'webrick'
require 'webrick/https'
require 'openssl'
require 'socket'
require 'middleman-core/meta_pages'
require 'middleman-core/logger'
@ -21,7 +22,7 @@ module Middleman
@options = opts
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'}"
@initialized ||= false
@ -110,14 +111,12 @@ module Middleman
)
config[:environment] = opts[:environment].to_sym if opts[:environment]
config[:host] = opts[:host] if opts[:host]
config[:port] = opts[:port] if opts[:port]
config[:https] = opts[:https] unless opts[:https].nil?
config[:ssl_certificate] = opts[:ssl_certificate] if opts[:ssl_certificate]
config[:ssl_private_key] = opts[:ssl_private_key] if opts[:ssl_private_key]
end
@host = @app.config[:host]
@port = @app.config[:port]
@https = @app.config[:https]
@ -180,7 +179,6 @@ module Middleman
# @return [void]
def setup_webrick(is_logging)
http_opts = {
BindAddress: host,
Port: port,
AccessLog: [],
DoNotReverseLookup: true
@ -197,7 +195,7 @@ module Middleman
# use a generated self-signed cert
http_opts[:SSLCertName] = [
%w(CN localhost),
%w(CN #{host})
%w(CN #{Socket.gethostname})
].uniq
end
end
@ -266,10 +264,16 @@ module Middleman
# Returns the URI the preview server will run on
# @return [URI]
def uri
host = @host == '0.0.0.0' ? 'localhost' : @host
def uri(host = Socket.gethostname)
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
ip = Socket.ip_address_list.find { |ai| ai.ipv4? && !ai.ipv4_loopback? }
ip ? ip.ip_address : '127.0.0.1'
end
end