require 'rack'
app = lambda{|env|[200,{},["hello from rack"]]}
Rack::Handler::WEBrick.run app
class Decorator
def initialize(app)
@app = app
end
def call(env)
status, headers, body = @app.call(env)
new_body = "from middleware input <br/>"
body.each{|str| new_body << str}
headers['Content-Length'] = new_body.bytesize.to_s
[status, headers, [new_body]]
end
end
app = lambda{|env|[200,{},["hello from rack"]]}
Rack::Handler::WEBrick.run Decorator.new(app)
require 'decorator'
app = Rack::Builder.new{
use Rack::ContentLength
use Decorator
run lambda {|env| [200, {"Content-Type"=>"text/html"}, ["hello world"]]}
}.to_app
Rack::Handler::WEBrick.run app
app = Rack::Builder.new {
map '/hello' do
run lambda {|env| [200, {"Content-Type" => "text/html"}, ["hello"]] } end
map '/world' do
run lambda {|env| [200, {"Content-Type" => "text/html"}, ["world"]] }
end
map '/' do
run lambda {|env| [200, {"Content-Type" => "text/html"}, ["all"]] } end
}.to_app
Rack::Handler::WEBrick.run app, :Port => 3000
require 'rack'
app = lambda{|env| [200,{},["hello world"]] }
Rack::Server.new.server.run app
ruby xxx.rb
#test.ru
run lambda{|env| [ 200,{},["rackup start"] ]}
rackup test.ru
def logging_middleware
lambda { |server|
server.server.name =~ /CGI/ || server.options[:quiet] ? nil : [Rack::CommonLogger, $stderr]
}
end
m = Hash.new {|h,k| h[k] = []}
m["deployment"] = [
[Rack::ContentLength],
[Rack::Chunked],
logging_middleware,
[Rack::TempfileReaper]
]
m["development"] = [
[Rack::ContentLength],
[Rack::Chunked],
logging_middleware,
[Rack::ShowExceptions],
[Rack::Lint],
[Rack::TempfileReaper]
]
m
end
#test.ru run Rack::Directory.new "~/" rackup test.ru
** **rake middleware use Rack::Sendfile use ActionDispatch::Static use Rack::Lock use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x007feef1563b90> use Rack::Runtime use Rack::MethodOverride use ActionDispatch::RequestId use Rails::Rack::Logger use ActionDispatch::ShowExceptions use WebConsole::Middleware use ActionDispatch::DebugExceptions use ActionDispatch::RemoteIp use ActionDispatch::Reloader use ActionDispatch::Callbacks use ActiveRecord::Migration::CheckPending use ActiveRecord::ConnectionAdapters::ConnectionManagement use ActiveRecord::QueryCache use ActionDispatch::Cookies use ActionDispatch::Session::CookieStore use ActionDispatch::Flash use ActionDispatch::ParamsParser use Rack::Head use Rack::ConditionalGet use Rack::ETag run Fool::Application.routes
2.2.1 :001 > QuestionsController.action(:new) => #<Proc:0x007fbe482ee0b0@/Users/killman/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_controller/metal.rb:237 (lambda)>
config.middleware.use(new_middleware, args)#:把新中间件添加到列表末尾; config.middleware.insert_before(existing_middleware, new_middleware, args)#:在 existing_middleware
config.middleware.insert_after(existing_middleware, new_middleware, args)#:在 existing_middleware
config.middleware.swap ActionDispatch::ShowExceptions, Lifo::ShowExceptions #替换中间件 config.middleware.delete "Rack::Lock"#删除中间件
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有