Fix port suggestion from unused ports
This commit is contained in:
parent
211f1b5a85
commit
407a17bc77
|
@ -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'
|
||||||
require 'middleman-core/rack'
|
require 'middleman-core/rack'
|
||||||
|
@ -200,7 +201,7 @@ module Middleman
|
||||||
begin
|
begin
|
||||||
::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}"
|
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=#{unused_tcp_port}"
|
||||||
exit(1)
|
exit(1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -234,6 +235,15 @@ module Middleman
|
||||||
scheme = https? ? 'https' : 'http'
|
scheme = https? ? 'https' : 'http'
|
||||||
URI("#{scheme}://#{host}:#{@port}")
|
URI("#{scheme}://#{host}:#{@port}")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Returns unused TCP port
|
||||||
|
# @return [Fixnum]
|
||||||
|
def unused_tcp_port
|
||||||
|
server = TCPServer.open(0)
|
||||||
|
port = server.addr[1]
|
||||||
|
server.close
|
||||||
|
port
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class FilteredWebrickLog < ::WEBrick::Log
|
class FilteredWebrickLog < ::WEBrick::Log
|
||||||
|
|
Loading…
Reference in a new issue