Merge pull request #1516 from dg-ratiodata/feature/set_hostname
Set host name and iterate over some ports...
This commit is contained in:
commit
6afda2a34c
|
@ -71,6 +71,10 @@ module Middleman
|
||||||
# @return [Fixnum]
|
# @return [Fixnum]
|
||||||
config.define_setting :port, 4567, 'The preview server port'
|
config.define_setting :port, 4567, 'The preview server port'
|
||||||
|
|
||||||
|
# Which server name should be used
|
||||||
|
# @return [NilClass, String]
|
||||||
|
config.define_setting :host, nil , 'The preview host name'
|
||||||
|
|
||||||
# Whether to serve the preview server over HTTPS.
|
# Whether to serve the preview server over HTTPS.
|
||||||
# @return [Boolean]
|
# @return [Boolean]
|
||||||
config.define_setting :https, false, 'Serve the preview server over SSL/TLS'
|
config.define_setting :https, false, 'Serve the preview server over SSL/TLS'
|
||||||
|
|
|
@ -14,6 +14,9 @@ module Middleman::Cli
|
||||||
method_option :port,
|
method_option :port,
|
||||||
aliases: '-p',
|
aliases: '-p',
|
||||||
desc: 'The port Middleman will listen on'
|
desc: 'The port Middleman will listen on'
|
||||||
|
method_option :host,
|
||||||
|
aliases: '-h',
|
||||||
|
desc: 'The host name Middleman will use'
|
||||||
method_option :https,
|
method_option :https,
|
||||||
type: :boolean,
|
type: :boolean,
|
||||||
desc: 'Serve the preview server over SSL/TLS'
|
desc: 'Serve the preview server over SSL/TLS'
|
||||||
|
@ -66,6 +69,7 @@ module Middleman::Cli
|
||||||
params = {
|
params = {
|
||||||
port: options['port'],
|
port: options['port'],
|
||||||
https: options['https'],
|
https: options['https'],
|
||||||
|
host: options['host'],
|
||||||
ssl_certificate: options['ssl_certificate'],
|
ssl_certificate: options['ssl_certificate'],
|
||||||
ssl_private_key: options['ssl_private_key'],
|
ssl_private_key: options['ssl_private_key'],
|
||||||
environment: options['environment'],
|
environment: options['environment'],
|
||||||
|
|
|
@ -9,7 +9,7 @@ require 'middleman-core/logger'
|
||||||
module Middleman
|
module Middleman
|
||||||
module PreviewServer
|
module PreviewServer
|
||||||
class << self
|
class << self
|
||||||
attr_reader :app, :host, :port, :ssl_certificate, :ssl_private_key
|
attr_reader :app, :host, :port, :ssl_certificate, :ssl_private_key, :environment
|
||||||
delegate :logger, to: :app
|
delegate :logger, to: :app
|
||||||
|
|
||||||
def https?
|
def https?
|
||||||
|
@ -22,6 +22,8 @@ module Middleman
|
||||||
@options = opts
|
@options = opts
|
||||||
|
|
||||||
mount_instance(new_app)
|
mount_instance(new_app)
|
||||||
|
|
||||||
|
logger.debug %(== The Middleman is running in "#{environment}" environment)
|
||||||
logger.info "== The Middleman is standing watch at #{uri} (#{uri(public_ip)})"
|
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'}"
|
||||||
|
|
||||||
|
@ -112,14 +114,16 @@ module Middleman
|
||||||
|
|
||||||
config[:environment] = opts[:environment].to_sym if opts[:environment]
|
config[:environment] = opts[:environment].to_sym if opts[:environment]
|
||||||
config[:port] = opts[:port] if opts[:port]
|
config[:port] = opts[:port] if opts[:port]
|
||||||
|
config[:host] = opts[:host].presence || Socket.gethostname.tr(' ', '+')
|
||||||
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 = Socket.gethostname.tr(' ', '+')
|
@host = @app.config[:host]
|
||||||
@port = @app.config[:port]
|
@port = @app.config[:port]
|
||||||
@https = @app.config[:https]
|
@https = @app.config[:https]
|
||||||
|
@environment = @app.config[:environment]
|
||||||
|
|
||||||
@ssl_certificate = @app.config[:ssl_certificate]
|
@ssl_certificate = @app.config[:ssl_certificate]
|
||||||
@ssl_private_key = @app.config[:ssl_private_key]
|
@ssl_private_key = @app.config[:ssl_private_key]
|
||||||
|
@ -182,6 +186,7 @@ module Middleman
|
||||||
http_opts = {
|
http_opts = {
|
||||||
Port: port,
|
Port: port,
|
||||||
AccessLog: [],
|
AccessLog: [],
|
||||||
|
ServerName: host,
|
||||||
DoNotReverseLookup: true
|
DoNotReverseLookup: true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -207,9 +212,26 @@ module Middleman
|
||||||
end
|
end
|
||||||
|
|
||||||
begin
|
begin
|
||||||
|
tries ||= 4
|
||||||
|
tried_ports ||= []
|
||||||
|
|
||||||
::WEBrick::HTTPServer.new(http_opts)
|
::WEBrick::HTTPServer.new(http_opts)
|
||||||
rescue Errno::EADDRINUSE
|
rescue Errno::EADDRINUSE
|
||||||
logger.error "== Port #{port} is unavailable. Either close the instance of Middleman already running on #{port} or start this Middleman on a new port with: --port=#{port.to_i + 1}"
|
tries -= 1
|
||||||
|
|
||||||
|
tried_ports << port
|
||||||
|
|
||||||
|
if tries > 0
|
||||||
|
logger.error %(== Port #{port} is unavailable. Trying port #{port + 1} next.)
|
||||||
|
|
||||||
|
@port += 1
|
||||||
|
http_opts[:Port] = @port
|
||||||
|
|
||||||
|
retry
|
||||||
|
end
|
||||||
|
|
||||||
|
logger.error %(== Ports #{tried_ports.to_sentence} are unavailable. Either close the instances of "Middleman" already running on Ports #{tried_ports.to_sentence} or start this "Middleman"-instance on a another port with: "middleman server --port=#{port + 1}".)
|
||||||
|
|
||||||
exit(1)
|
exit(1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue