2012年12月7日 星期五

Rack middleware

將下列存成: shrimp.rb
class Shrimp SHRIMP_STRING = 'Hello World' def initialize(app) @app = app end def call(env) status, headers, response = @app.call(env) response_body = "" response.each { |part| response_body += part } response_body += "<pre>#{SHRIMP_STRING}</pre>" headers["Content-Length"] = response_body.length.to_s [status, headers, response_body] end end

將下列存成: config.ru
# config.ru require 'rack' require 'rack/lobster' require 'shrimp' use Shrimp run Rack::Lobster.new

執行 rackup config.ru

將會看到 一支蝦子, 與後面被塞入 Hellow World .

使用 middleware 在你的 project

Place your middleware module to lib/shrimp.rb
and add "config.middleware.use 'Shrimp'" to your environment.rb.

參考網站 :

http://www.amberbit.com/blog/introduction-to-rack-middleware
Rails on Rack guide

【下列文章您可能也有興趣】

沒有留言: