说说Rails吧,启动开始。 - 差沙的密码 -- SSHWSFC‘s code - Bl...

来源:百度文库 编辑:神马文学网 时间:2024/04/17 07:49:07
说说Rails吧,启动开始。
为了记录自己看Rails源码的过程,全程记录无废话。
我们看看script/server都干了什么
require File.dirname(__FILE__)  +   ‘ /../config/boot ‘
require  ‘ commands/server ‘
引用了boot.rb这个文件。看来这个文件是rails启动的入口,来看看怎么回事吧。
unless defined?(RAILS_ROOT)
  root_path = File.join(File.dirname(__FILE__), ‘..‘)

  unless RUBY_PLATFORM =~ /mswin32/
    require ‘pathname‘
    root_path = Pathname.new(root_path).cleanpath(true).to_s
  end

  RAILS_ROOT = root_path
end
这一部分定义了RAILS_ROOT这个系统的全局变量,指定了项目的根目录,大家可以在以后华丽的使用了。
下一部分是找到rails,粗略看一下。
  if File.directory?("#{RAILS_ROOT}/vendor/rails")
    require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
  else
    require ‘rubygems‘
这里能看到,他先跑到vendor/rails去找rails了,这就是我们为什么能在插件里用rails是原因。如果没有那么gems的干活。
接下来是初始化一下load_path,没什么看的了。boot.rb就这样的吧。
回到script/server的第二行,包含了‘commands/server‘这个文件,这个文件是什么?Rails的源码里面找吧。我们在Rails的源码里面找到这个文件。
require ‘active_support‘
require ‘fileutils‘

begin
  require_library_or_gem ‘fcgi‘
rescue Exception
  # FCGI not available
end

server = case ARGV.first
  when "lighttpd"
    ARGV.shift
  when "webrick"
    ARGV.shift
  else
    if RUBY_PLATFORM !~ /mswin/ && !silence_stderr { `lighttpd -version` }.blank? && defined?(FCGI)
      "lighttpd"
    else
      "webrick"
    end
end

if server == "webrick"
  puts "=> Booting WEBrick"
else
  puts "=> Booting lighttpd (use ‘script/server webrick‘ to force WEBrick)"
end

FileUtils.mkdir_p(%w( tmp/sessions tmp/cache tmp/sockets ))
require "commands/servers/#{server}"

没想到ActiveRecord居然是在这里引用的,这个ActiveRecord里面扩展了很对Ruby的既有类型,所以我们看源码的时候如果发现有不熟悉的方法,就来这里找找,当然,看Rails的API是最好的选择。
从参数一目了然,我们可以传入server的名字,lighttpd和webrick,根据不同的server选择不同的server文件来读取。我们还是看看webrick的吧。
require ‘webrick‘
require ‘optparse‘

OPTIONS = {
  :port            => 3000,
  :ip              => "0.0.0.0",
  :environment     => (ENV[‘RAILS_ENV‘] || "development").dup,
  :server_root     => File.expand_path(RAILS_ROOT + "/public/"),
  :server_type     => WEBrick::SimpleServer,
  :charset         => "UTF-8",
  :mime_types      => WEBrick::HTTPUtils::DefaultMimeTypes
}

ARGV.options do |opts|
  script_name = File.basename($0)
  opts.banner = "Usage: ruby #{script_name} [options]"

  opts.separator ""

  opts.on("-p", "--port=port", Integer,
          "Runs Rails on the specified port.",
          "Default: 3000") { |v| OPTIONS[:port] = v }
  opts.on("-b", "--binding=ip", String,
          "Binds Rails to the specified ip.",
          "Default: 0.0.0.0") { |v| OPTIONS[:ip] = v }
  opts.on("-e", "--environment=name", String,
          "Specifies the environment to run this server under (test/development/production).",
          "Default: development") { |v| OPTIONS[:environment] = v }
  opts.on("-m", "--mime-types=filename", String,
                  "Specifies an Apache style mime.types configuration file to be used for mime types",
                  "Default: none") { |mime_types_file| OPTIONS[:mime_types] = WEBrick::HTTPUtils::load_mime_types(mime_types_file) }

  opts.on("-d", "--daemon",
          "Make Rails run as a Daemon (only works if fork is available -- meaning on *nix)."
          ) { OPTIONS[:server_type] = WEBrick::Daemon }

  opts.on("-c", "--charset=charset", String,
          "Set default charset for output.",
          "Default: UTF-8") { |v| OPTIONS[:charset] = v }

  opts.separator ""

  opts.on("-h", "--help",
          "Show this help message.") { puts opts; exit }

  opts.parse!
end

ENV["RAILS_ENV"] = OPTIONS[:environment]
RAILS_ENV.replace(OPTIONS[:environment]) if defined?(RAILS_ENV)

require RAILS_ROOT + "/config/environment"
require ‘webrick_server‘

OPTIONS[‘working_directory‘] = File.expand_path(RAILS_ROOT)

puts "=> Rails application started on http://#{OPTIONS[:ip]}:#{OPTIONS[:port]}"
puts "=> Ctrl-C to shutdown server; call with --help for options" if OPTIONS[:server_type] == WEBrick::SimpleServer
DispatchServlet.dispatch(OPTIONS)

本来不想把大段的代码贴上来,但是这里面的内容可能大家都比较关心,涉及到server的启动参数。
  :port            => 端口,
  :ip              => server ip,
  :environment     =>运行环境,
  :server_root     => web访问的目录,(很多人问这个怎么改)
  :server_type     => WEBrick::SimpleServer,
  :charset         => "UTF-8", 编码
  :mime_types      => WEBrick::HTTPUtils::DefaultMimeTypes
后面的require 引入了两个文件,一个是‘webrick_server‘,别看,就是他。另外一个是config/environment,这个文件是系统的全局配置文件,很重要,我们还是下次看看这个文件去吧。里面还真挺有意思。