How to tell which port Rails is running on in an initializer
Is there a way to tell which port a Rails application (or a generic Rack app) is running on in an initializer ?
I would like to be able to load a different configura开发者_JS百科tion based on the port or the host name, in order to connect to a host-specific FaceBook application.
I'm using Rails 2.3.5.
This is not very clean but this is a way you can get the port you declared when calling :
rails server -p portnumber
wherever you want in your application (for rails 3).
Here is my scripts/rails.rb file :
#!/usr/bin/env ruby
# This command will automatically be run when you run "rails"
# with Rails 3 gems installed from the root of your application.
ENV['PORT']=ARGV[3]
APP_PATH = File.expand_path('../../config/application', __FILE__)
require File.expand_path('../../config/boot', __FILE__)
require 'rails/commands'
Then whenever you want to get the port number of your server, all you need to do is call ENV['PORT']
.
You can call Rails::Server.new.options[:Port]
to get the port that your Rails server is running on. This will parse the -p
arg from your rails server
command, or default to port 3000
.
Based on the lack of answers here and this thread on rubyforum: http://www.ruby-forum.com/topic/196017#new , I think that there probably isn't a standard way to tell the port.
精彩评论