Skip to content

Commit

Permalink
Be able to have a single application listen on multiple IPs / ports
Browse files Browse the repository at this point in the history
  • Loading branch information
carllerche committed Mar 2, 2011
1 parent 2fbe298 commit cebc21f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
31 changes: 18 additions & 13 deletions lib/kirk/server/builder.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -65,12 +65,15 @@ def hosts(*hosts)
@current.hosts.concat hosts @current.hosts.concat hosts
end end


def listen(listen) def listen(*listeners)
listen = listen.to_s listeners = listeners.map do |listener|
listen = ":#{listen}" unless listen.index(':') listener = listener.to_s
listen = "0.0.0.0#{listen}" if listen.index(':') == 0 listener = ":#{listener}" unless listener.index(':')
listener = "0.0.0.0#{listener}" if listener.index(':') == 0
listener
end


@current.listen = listen @current.listen = listeners
end end


def watch(*watch) def watch(*watch)
Expand All @@ -88,7 +91,7 @@ def to_handler
application = HotDeployable.new(c) application = HotDeployable.new(c)
application.add_watcher(watcher) application.add_watcher(watcher)


ctx.set_connector_names [c.listen] ctx.set_connector_names c.listen
ctx.set_handler application ctx.set_handler application
end end
end end
Expand All @@ -102,15 +105,17 @@ def to_connectors
@connectors = {} @connectors = {}


@configs.each do |config| @configs.each do |config|
next if @connectors.key?(config.listen) config.listen.each do |listener|
next if @connectors.key?(listener)


host, port = config.listen.split(':') host, port = listener.split(':')


connector = Jetty::SelectChannelConnector.new connector = Jetty::SelectChannelConnector.new
connector.set_host(host) connector.set_host(host)
connector.set_port(port.to_i) connector.set_port(port.to_i)


@connectors[config.listen] = connector @connectors[listener] = connector
end
end end


@connectors.values @connectors.values
Expand All @@ -135,7 +140,7 @@ def expand_path(path)


def new_config def new_config
ApplicationConfig.new.tap do |config| ApplicationConfig.new.tap do |config|
config.listen = '0.0.0.0:9090' config.listen = ['0.0.0.0:9090']
config.watch = [ ] config.watch = [ ]
config.bootstrap_path = File.expand_path('../bootstrap.rb', __FILE__) config.bootstrap_path = File.expand_path('../bootstrap.rb', __FILE__)
end end
Expand Down
32 changes: 32 additions & 0 deletions spec/server/server_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -82,6 +82,38 @@
last_response.should have_body('Hello World') last_response.should have_body('Hello World')
end end


it "can start the same application on multiple ports" do
path = hello_world_path('config.ru')

start do
log :level => :warning

rack "#{path}" do
listen 9091, '127.0.0.1:9092'
end
end

lambda {
get '/'
}.should raise_error(Errno::ECONNREFUSED)

host! IP_ADDRESS, 9091

get '/'
last_response.should have_body('Hello World')

host! IP_ADDRESS, 9092

lambda {
get '/'
}.should raise_error(Errno::ECONNREFUSED)

host! '127.0.0.1', 9092

get '/'
last_response.should have_body('Hello World')
end

it "can partition applications by the host name" do it "can partition applications by the host name" do
path1 = hello_world_path('config.ru') path1 = hello_world_path('config.ru')
path2 = goodbye_world_path('config.ru') path2 = goodbye_world_path('config.ru')
Expand Down

0 comments on commit cebc21f

Please sign in to comment.