当前位置: 首页 > 工具软件 > script-server > 使用案例 >

Rails源码阅读(二)_script/server

钦德佑
2023-12-01

Rails源码阅读(二)_script/server

 

如果明白了script/console,那么理解script/server会轻松些;

区别在于,启动console其实是需要加载irb,而启动server则要处理http请求;

应该先看看本系列的如下文章:

boot:Rails源码阅读(零)_config/boot

http-server:动手写rails(一)_Rack标准和HttpServer之WEBrick

 

script/server的代码:

require File.expand_path('../../config/boot',  __FILE__)
require 'commands/server'

第一行是准备环境, 第二行才是启动server

commands/server.rb的主要代码:

server = Rack::Handler.get(ARGV.first) rescue nil
unless server
  begin
    server = Rack::Handler::Mongrel
  rescue LoadError => e
    server = Rack::Handler::WEBrick
  end
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"
  File.open(pid, 'w'){ |f| f.write(Process.pid) }
  at_exit { File.delete(pid) if File.exist?(pid) }
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

puts "=> Call with -d to detach"

trap(:INT) { exit }

puts "=> Ctrl-C to shutdown server"

begin
  server.run(app, options.merge(:AccessLog => []))
ensure
  puts 'Exiting'
end

#(1) 可以指定maping的path,这样在rack中就map到这个路径:map map_path

if options[:path].nil?
  map_path = "/"
else
  ActionController::Base.relative_url_root = options[:path]
  map_path = options[:path]
end

#(2) 在启动rack之前,先要加载rails环境和组建

require RAILS_ROOT + "/config/environment"

#(3) 使用rack启动,用WEBrick做web服务,见下面分析。

 

commands/server.rb的启动,除了一些配置如端口等外,更重要的是用到了两个东西:

#1 server:WEBrick|Mongrel,用来处理http等

server = Rack::Handler.get(ARGV.first) rescue nil
unless server
  begin
    server = Rack::Handler::Mongrel
  rescue LoadError => e
    server = Rack::Handler::WEBrick
  end
end
 

看看WEBrick的源码:继承于::WEBrick::HTTPServlet::AbstractServlet

module Rack
  module Handler
    class WEBrick < ::WEBrick::HTTPServlet::AbstractServlet
      def self.run(app, options={})
        
      end

      def self.shutdown
       
      end

      def initialize(server, app)
        super server
        @app = Rack::ContentLength.new(app)
      end

      def service(req, res)

 

#2 rack 和 middleware

rack的应用上来说,比较简单直接:

#1 
rackXXX = generate_a_rack_app
#2 
builder = Rack::Builder.new{
  user middlewareXXX
  ... ...
  run rackXXX
}
#3 
Rack::Handler::HandlerXXX.run builder

 

    对应的server启动代码为:

#1 generate a racke proc
#   这里只要实现call方法就行
inner_app = ActionController::Dispatcher.new #Dispatcher实现了call方法

#2 RackBuilder
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

#3 RackHandler
server = Rack::Handler.get(ARGV.first) rescue nil
unless server
  begin
    server = Rack::Handler::Mongrel
  rescue LoadError => e
    server = Rack::Handler::WEBrick
  end
end
server.run(app, options.merge(:AccessLog => []))
 

这里要明白流程:

启动server是要启动什么?

HttpServer(CGIserver),这里就是WEBrick或者Mongrel

server怎么和ruby联系上了?

WEBrick等遵循rack标准,server.run(rack_app, options)

ruby写一个rack的app即可

rails里的rack是什么?

是:ActionController::Dispatcher

出处代码:inner_app = ActionController::Dispatcher.new

还需要哪些知识?

rack的介绍,原理,好处,标准,使用等

WEBrick活Mongrel的介绍和使用

 

====结束====

===           ===

==                ==

=                     =

|                       |

 

 

 

 

 

 

 

 

 

 

 

 

 类似资料: