无论如何,最终的结果是要启动一个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.
||
| |
| |
====结束====
=== ===
== ==
= =
| |
分享到:
相关推荐
rails_apps_composer, 一个 gem,为 Rails 启动应用程序创建 Rails 应用程序模板 Rails 应用编辑器 Rails 应用程序编辑器 gem 安装一个 命令行 工具来从"食谱"的Collection 组装 Rails 应用程序。"你可以使用 rails_...
InspiniaAdmin 2.6.1 Rails_Full_Version
rails_semantic_logger, Rails 语义记录器用语义记录器替换 Rails 缺省记录器 Rails 语义记录器 语义记录器用语义记录器替代 Rails 缺省记录器。http://github.com/rocketjob/rails_semantic_logger文档有关完整文档...
agile_web_development_with_rails_3rd_edition.9994652073.pdf
inspinia admin 2.5 中的 Rails_Full_Version
复杂Rails系统架构优化
介绍插件,用于对记录进行排序(使用 gem)安装要启用rails_admin_acts_as_list,请将以下内容添加到您的Gemfile : gem 'rails_admin_acts_as_list'gem 'rails_admin' 重要提示: rails_admin_acts_as_list之前必须...
中文世界唯一一本Rails 4.0.0 + Ruby 2.0.0 的自學書籍
RestFul_Rails_Dev_v_0.1 RestFul_Rails_Dev_v_0.1
Ruby on Rails Guides_ A Guide to Testing Rails Applications.pdf
我们将从 Ruby、Rack 和 ActiveSupport 构建一个应用程序。 本课的目的是从内部展示 Rails 是如何工作的。 这将逐步构建一些 Rails 组件的精简版。 它会: 创建一个类似Rails 的初始化过程。 使用 Rails 目录...
Api-rails5_api_tutorial.zip,了解如何在michael hartl的rails 5教程上构建一个现代api立即在rails应用程序中构建一个api!(Rails 5版本),一个api可以被认为是多个软件设备之间通信的指导手册。例如,api可用于web...
ruby_on_rails_3_入门教程(中文)
Webrick rails 外部电脑 访问慢
rails_best_practices ...默认情况下,rails_best_practices将解析vendor , spec , test和features目录中的代码。 排除目录 要排除目录,只需使用-e或--exclude调用: rails_best_practices -e "db/migrate
weixin_rails_middleware, 微信集成 ruby weixin_rails_middleware for integration weixin.
Rails_Recipes_with_Source_Code
Ruby on Rails Guides_ A Guide to Active Record Associations.pdf
分2卷压缩,另一卷在我的资源列表 本书从Ruby和Rails的历史讲起,以在Rails框架上克隆故事共享网站digg.com为例,详细讲述了Ruby和Rails的安装过程、框架组成部分、与AJAX和Web 2.0技术结合、安全措施、高级话题和...
Rails_3_Cheat_Sheets.pdf