views:

202

answers:

1

So I would like my Rails app instances to register themselves on a "I'm up" kind of thing I'm playing with, and I'd like it to be able to mention what local port it's running on. I can't seem to find how to do it - in fact just finding out its IP is tricky and needs a bit of a hack.

But no problem, I have the IP - but how can I find what port my mongrel/thin/webrick server is running on?

To be super explicit, if I start a rails app using script/server -p 3001, what can I do to pull that 3001 inside the app.

Thanks.

+4  A: 

From inside any controller action, check the content of request.port, thus:

class SomeController < ApplicationController
  def some_action
    raise "I'm running on port #{request.port}."
  end
end
Lars Haugseth
So simple and so easily overlooked. I thought about this for a few minutes and was thinking out round about ways of getting it. This question is a good reminder for me to review request again.http://api.rubyonrails.org/classes/ActionController/AbstractRequest.html
Jim
Precise for localhost, but does not work when behind an Apache Server or in a cluster setup.
Swanand
Thanks for the answer. Do you have any idea how to access that programmatically, ie something I can do inside script/console? After all, if I need to access it via HTTP, that means I already know the port!
RailFan