aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib
diff options
context:
space:
mode:
Diffstat (limited to 'railties/lib')
-rw-r--r--railties/lib/rails.rb2
-rw-r--r--railties/lib/rails/application.rb58
-rw-r--r--railties/lib/rails/commands/server.rb40
3 files changed, 62 insertions, 38 deletions
diff --git a/railties/lib/rails.rb b/railties/lib/rails.rb
new file mode 100644
index 0000000000..329b60fb87
--- /dev/null
+++ b/railties/lib/rails.rb
@@ -0,0 +1,2 @@
+require "rails/application"
+require "rails/initializer" \ No newline at end of file
diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb
new file mode 100644
index 0000000000..e379504f54
--- /dev/null
+++ b/railties/lib/rails/application.rb
@@ -0,0 +1,58 @@
+require 'action_controller'
+
+module Rails
+ class Application
+ # Loads a Rails application from a directory and returns a Rails
+ # Application object that responds to #call(env)
+ def self.load(path, options = {})
+ require "#{path}/config/environment"
+ new(path, options)
+ end
+
+ def initialize(path, options)
+ @path = path
+
+ ensure_tmp_dirs
+
+ if options[:config]
+ config = File.join(path, options[:config])
+ config = nil unless File.exist?(config)
+ end
+
+ @app = ::Rack::Builder.new {
+ use Rails::Rack::LogTailer unless options[:detach]
+ use Rails::Rack::Debugger if options[:debugger]
+ if options[:path]
+ base = options[:path]
+ ActionController::Base.relative_url_root = base
+ end
+
+ map base || "/" do
+ use Rails::Rack::Static
+
+ if config && config =~ /\.ru$/
+ instance_eval(File.read(config), config)
+ elsif config
+ require config
+ run Object.const_get(File.basename(config, '.rb').capitalize)
+ else
+ run ActionController::Dispatcher.new
+ end
+ end
+ }.to_app
+ end
+
+ def call(env)
+ @app.call(env)
+ end
+
+ private
+
+ def ensure_tmp_dirs
+ %w(cache pids sessions sockets).each do |dir_to_make|
+ FileUtils.mkdir_p(File.join(@path, 'tmp', dir_to_make))
+ end
+ end
+
+ end
+end \ No newline at end of file
diff --git a/railties/lib/rails/commands/server.rb b/railties/lib/rails/commands/server.rb
index 823916b1dc..b8ba4a9f91 100644
--- a/railties/lib/rails/commands/server.rb
+++ b/railties/lib/rails/commands/server.rb
@@ -1,7 +1,6 @@
-require 'action_controller'
-
require 'fileutils'
require 'optparse'
+require 'rails'
options = {
:Port => 3000,
@@ -46,10 +45,6 @@ end
puts "=> Booting #{ActiveSupport::Inflector.demodulize(server)}"
puts "=> Rails #{Rails.version} application starting on http://#{options[:Host]}:#{options[:Port]}#{options[:path]}"
-%w(cache pids sessions sockets).each do |dir_to_make|
- FileUtils.mkdir_p(File.join(RAILS_ROOT, 'tmp', dir_to_make))
-end
-
if options[:detach]
Process.daemon
pid = "#{RAILS_ROOT}/tmp/pids/server.pid"
@@ -60,38 +55,7 @@ end
ENV["RAILS_ENV"] = options[:environment]
RAILS_ENV.replace(options[:environment]) if defined?(RAILS_ENV)
-if File.exist?(options[:config])
- config = options[:config]
- if config =~ /\.ru$/
- cfgfile = File.read(config)
- if cfgfile[/^#\\(.*)/]
- opts.parse!($1.split(/\s+/))
- end
- inner_app = eval("Rack::Builder.new {( " + cfgfile + "\n )}.to_app", nil, config)
- else
- require config
- inner_app = Object.const_get(File.basename(config, '.rb').capitalize)
- end
-else
- require RAILS_ROOT + "/config/environment"
- inner_app = ActionController::Dispatcher.new
-end
-
-if options[:path].nil?
- map_path = "/"
-else
- ActionController::Base.relative_url_root = options[:path]
- map_path = options[:path]
-end
-
-app = Rack::Builder.new {
- use Rails::Rack::LogTailer unless options[:detach]
- use Rails::Rack::Debugger if options[:debugger]
- map map_path do
- use Rails::Rack::Static
- run inner_app
- end
-}.to_app
+app = Rails::Application.load(RAILS_ROOT, options)
puts "=> Call with -d to detach"