Reputation: 15009
I want to register a callback URL to my Rails server from a third-party application, and to perform this registering on startup so I can start catching notifications.
Looking at this question and the answers I see advice like:
class SomeController < ApplicationController
def some_action
raise "I'm running on port #{request.port}."
end
end
This is no good as I am not in a Controller. Next there is:
Rails::Server.new.options[:Port]
However, I get this error from my code and rails console
:
NoMethodError: undefined method 'Server' for Rails::Module
I've got the host name successfully using Socket.gethostname
, but how do I get the port number?
Upvotes: 1
Views: 310
Reputation: 8954
The reason you cant access request.port
is that the request object is tied to a clients request which does only exist when you look at a specific request. What you want is accessing the webservers configuration. Thats a completely other thing.
You might solve this looking up the pid of the webserver which is possible by accessing the applicationwide $$
variable which contains the pid of the process executing the ruby code. Then you can lookup the port used by this process. You can du this using the system
command which executes external applications. Here is a post that explains how ou can identify a port that belongs to a process:
http://www.cyberciti.biz/faq/what-process-has-open-linux-port/
This will work when there is only one process that is running the whole application. When using a combination of thin and nginx this wont work because the process running the code isn't the process thats running the webserver.
Upvotes: 1