Merge pull request #1530 from iBenza/fix_port_suggestion

Fix port suggestion from unused ports
feature/livereload-locales-data
Thomas Reynolds 2015-05-26 08:05:31 -07:00
commit bca04841fe
1 changed files with 11 additions and 1 deletions

View File

@ -1,6 +1,7 @@
require 'webrick'
require 'webrick/https'
require 'openssl'
require 'socket'
require 'middleman-core/meta_pages'
require 'middleman-core/logger'
require 'middleman-core/rack'
@ -200,7 +201,7 @@ module Middleman
begin
::WEBrick::HTTPServer.new(http_opts)
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)
end
end
@ -234,6 +235,15 @@ module Middleman
scheme = https? ? 'https' : 'http'
URI("#{scheme}://#{host}:#{@port}")
end
# Returns unused TCP port
# @return [Fixnum]
def unused_tcp_port
server = TCPServer.open(0)
port = server.addr[1]
server.close
port
end
end
class FilteredWebrickLog < ::WEBrick::Log