aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/application.rb
blob: e379504f5412863a44ea1b28fc6011b571c0ff4a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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