为了记录自己看Rails源码的过程,全程记录无废话。
我们看看script/server都干了什么
		
				 require File.dirname(__FILE__) 
				+
				 
				'
				/../config/boot
				'
				require File.dirname(__FILE__) 
				+
				 
				'
				/../config/boot
				'
				
						
						 require 
				'
				commands/server
				'
require 
				'
				commands/server
				'
		 
		引用了boot.rb这个文件。看来这个文件是rails启动的入口,来看看怎么回事吧。
 unless defined?(RAILS_ROOT)
unless defined?(RAILS_ROOT)
 root_path = File.join(File.dirname(__FILE__), '..')
  root_path = File.join(File.dirname(__FILE__), '..')

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

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

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

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

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

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

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

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

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

 opts.separator ""
  opts.separator ""

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

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

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

 opts.separator ""
  opts.separator ""

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

 opts.parse!
  opts.parse!
 end
end

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

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

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

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

本来不想把大段的代码贴上来,但是这里面的内容可能大家都比较关心,涉及到server的启动参数。

  :port            
=> 端口,
 :ip              => server ip,
  :ip              => server ip,
 :environment     =>运行环境,
  :environment     =>运行环境,
 :server_root     => web访问的目录,(很多人问这个怎么改)
  :server_root     => web访问的目录,(很多人问这个怎么改)
 :server_type     => WEBrick::SimpleServer,
  :server_type     => WEBrick::SimpleServer,
 :charset         => "UTF-8", 编码
  :charset         => "UTF-8", 编码
 :mime_types      => WEBrick::HTTPUtils::DefaultMimeTypes
  :mime_types      => WEBrick::HTTPUtils::DefaultMimeTypes
后面的require 引入了两个文件,一个是'webrick_server',别看,就是他。另外一个是config/environment,这个文件是系统的全局配置文件,很重要,我们还是下次看看这个文件去吧。里面还真挺有意思。
(不知道blogjava让不让写ROR的文章,看到有人写了所以放在首页了,如果不妥,我会尽快删除)
	posted on 2006-11-20 23:43 
差沙 阅读(4597) 
评论(0)  编辑  收藏  所属分类: 
ROR