zoli
zoli

Reputation: 468

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 configuration 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.

Upvotes: 2

Views: 3256

Answers (3)

ndbroadbent
ndbroadbent

Reputation: 13793

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.

Upvotes: 2

jules testard
jules testard

Reputation: 313

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'].

Upvotes: 2

zoli
zoli

Reputation: 468

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.

Upvotes: 1

Related Questions