Skip to content
Browse files

Be able to have a single application listen on multiple IPs / ports

  • Loading branch information...
1 parent 2fbe298 commit cebc21f9f2fcd543f4a1445638b20b4f06222389 @carllerche carllerche committed Mar 1, 2011
Showing with 50 additions and 13 deletions.
  1. +18 −13 lib/kirk/server/builder.rb
  2. +32 −0 spec/server/server_spec.rb
View
31 lib/kirk/server/builder.rb
@@ -65,12 +65,15 @@ def hosts(*hosts)
@current.hosts.concat hosts
end
- def listen(listen)
- listen = listen.to_s
- listen = ":#{listen}" unless listen.index(':')
- listen = "0.0.0.0#{listen}" if listen.index(':') == 0
+ def listen(*listeners)
+ listeners = listeners.map do |listener|
+ listener = listener.to_s
+ listener = ":#{listener}" unless listener.index(':')
+ listener = "0.0.0.0#{listener}" if listener.index(':') == 0
+ listener
+ end
- @current.listen = listen
+ @current.listen = listeners
end
def watch(*watch)
@@ -88,7 +91,7 @@ def to_handler
application = HotDeployable.new(c)
application.add_watcher(watcher)
- ctx.set_connector_names [c.listen]
+ ctx.set_connector_names c.listen
ctx.set_handler application
end
end
@@ -102,15 +105,17 @@ def to_connectors
@connectors = {}
@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.set_host(host)
- connector.set_port(port.to_i)
+ connector = Jetty::SelectChannelConnector.new
+ connector.set_host(host)
+ connector.set_port(port.to_i)
- @connectors[config.listen] = connector
+ @connectors[listener] = connector
+ end
end
@connectors.values
@@ -135,7 +140,7 @@ def expand_path(path)
def new_config
ApplicationConfig.new.tap do |config|
- config.listen = '0.0.0.0:9090'
+ config.listen = ['0.0.0.0:9090']
config.watch = [ ]
config.bootstrap_path = File.expand_path('../bootstrap.rb', __FILE__)
end
View
32 spec/server/server_spec.rb
@@ -82,6 +82,38 @@
last_response.should have_body('Hello World')
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
path1 = hello_world_path('config.ru')
path2 = goodbye_world_path('config.ru')

0 comments on commit cebc21f

Please sign in to comment.
Something went wrong with that request. Please try again.