动手写rails(一)_Rack标准和HttpServer之WEBrick

 

无论如何,最终的结果是要启动一个server来接受请求,并返回结果。

ruby世界里,rack和java的servlet一样,是个标准。

 

RACK

Rack描述:

    provides a minimal interface between webservers supporting Ruby and Ruby frameworks.

优点:

    Rack provides a minimal, modular and adaptable interface for developing web applications in Ruby. By wrapping HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers, web frameworks, and software in between (the so-called middleware) into a single method call.

接口规范:

http://rack.rubyforge.org/doc/SPEC.html

使用的例子:

http://rack.rubyforge.org/doc/README.html 注意看 “Quick start”

 

代码例子:

#1 直接使用

 

require 'rubygems'
gem 'rack', '1.0.1'
require 'rack'
require 'rack/showexceptions'

class HelloWorld
  def call(env)
    [200, {"Content-Type" => "text/html"}, "Hello Rack!"]
  end
end

#RUN_1
server = Rack::Handler::WEBrick
server.run HelloWorld.new, :Port => 9292

 

#2 使用Rack::Builder,可以往过滤栈中加入过滤器

#RUN_2
app = Rack::Builder.new {
   use Rack::CommonLogger
   use Rack::ShowExceptions
   map "/" do
     #use Rack::Lint
     run HelloWorld.new
   end
}
server = Rack::Handler::WEBrick
server.run app, :Port => 9292

#3 Rack::Builder返回值还可以被嵌套(或者可以用to_app函数)

 

#RUN_3
inner_app = Rack::Builder.new {
   use Rack::CommonLogger
   use Rack::ShowExceptions
   map "/" do
     run HelloWorld.new
   end
}
app = Rack::Builder.new {
   use Rack::CommonLogger
   use Rack::ShowExceptions
   map "/" do
     run inner_app
   end
}
server = Rack::Handler::WEBrick
server.run app, :Port => 9292

 

Rack的优点利用:

map路径到不同的rack_app

rack stack的过滤

 

rack的相关资料和介绍:

rack官方:http://rack.rubyforge.org/

rack规范:http://rack.rubyforge.org/doc/SPEC.html

rack相关情况:http://rack.rubyforge.org/doc/README.html

Rack::Builder的api:http://rack.rubyforge.org/doc/Rack/Builder.html

rails官方对rack的使用:http://guides.rubyonrails.org/rails_on_rack.html

37signals的核心人员也就是rails的core-team-member对rack的introduction,可能直接对应rails的源码:

http://m.onkey.org/ruby-on-rack-1-hello-rack

http://m.onkey.org/ruby-on-rack-2-the-builder

这是一个实际的应用,rails和sinatra共存。原理:不同的map路径,map到不同的rack-server

http://m.onkey.org/rails-meets-sinatra

还有一篇中文的,总结的很不错:

http://hi.baidu.com/hlxwell/blog/item/614710a87dcaaeb9cb130c20.html

 

 

WEBrick

servlet有了,总要有服务器环境。

ruby默认的lib里自带了WEBrick,一个web-server,实现了http协议,CGI等

WEBrick遵循rack的标准,所以接收到请求后,可以走rack的stack。

WEBrick可以单独作为web-server。

例子:

 

require 'webrick'

root = File.expand_path 'index.html'
server = WEBrick::HTTPServer.new :Port => 8000, :DocumentRoot => root

trap 'INT' do server.shutdown end
server.start

index.html

<html>
<head>
    <title>fantaxy025025</title>
</head>
<doby>hello world!</doby>
</html>

 

关于WEBrick,rails里基本不会单独使用,但可以作为单独的web-server使用。

 

相关文档:

wikipedia.org:http://en.wikipedia.org/wiki/WEBrick

自带的文档:http://www.ruby-doc.org/stdlib-1.9.3/libdoc/webrick/rdoc/WEBrick.html

详细文档:居然很多页面也被枪了!

http://microjet.ath.cx/webrickguide/html/html_webrick.html

官方文档:http://segment7.net/projects/ruby/WEBrick/ 

一个中文的介绍,不错:

http://hi.baidu.com/gsxu/blog/item/fcf9172abdf3a8385243c13d.html/cmtid/0c719e3ef4550ff5838b137c

 

==>>总结<<==

有了上面这些,基本上rails启动了什么,大概底下会做些什么大动作,基本就搞定了。

启动的代码,基本也能看懂了。

别去看启动rails的细节,只要知道:

ActionController::Dispatcher.new is the primary Rack application object of a Rails application. Any Rack compliant web server should be usingActionController::Dispatcher.new object to serve a Rails application.

 

 

            ||

           |  |

          |    |

====结束====

===           ===

==                ==

=                     =

|                       |

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值