Rack ã使ã£ã¦ Web ãµã¼ãã§çµ±ä¸ãããã¤ã³ã¿ã¼ãã§ã¤ã¹ã®å©ç¨ãã
http://rack.rubyforge.org/
å
æ¥ç»å ´ãã Rack ã¨ããã©ã¤ãã©ãªã使ãã¨ãWeb ãµã¼ããã¨ã«åä¸ã®ã¤ã³ã¿ã¼ãã§ã¤ã¹ãå©ç¨ã§ããããã«ãªãã¾ããé常ãRuby 㧠Web ã¢ããªã±ã¼ã·ã§ã³ ãåä½ãããã¨ããcgi ãªã®ããfcgi ãªã®ããããã㯠mongrel ã webrick ã使ãã®ããã¨ãããã¨ãèããªããã°ãªãã¾ãããcgi ã®ã¿ã§åä½æ±ºããã¡ããªã©è¯ãã®ã§ãããã¬ã¯ã§ãã Web ã¢ããªã±ã¼ã·ã§ã³ãã¬ã¼ã ã¯ã¼ã¯ãä¸è¬ã«é
å¸ããã¢ããªã±ã¼ã·ã§ã³ãªã©ãæ¢åã®ãã¬ã¼ã ã¯ã¼ã¯ãç¨ããã«ä½ãã¨ãã¯ããã¾ãã¾ãªåä½ç°å¢ã§ãåãããã«èæ
®ããªãã¦ã¯ãªãã¾ããã
ãã¨ãã°æ¢åã®ãã¬ã¼ã ã¯ã¼ã¯ã§ãã Rails ã Camping ã¯ããããåå¥ã« cgi/fcgi/mongrel/webrick ãªã©ã®å¯¾å¿ãè¡ã£ã¦ãã¾ããããã Rack ã§å®ç¾©ããã¦ããã¤ã³ã¿ã¼ãã§ã¤ã¹ã使ããã¨ã«ãã£ã¦ãã©ããªç°å¢ã§åããããæèããã«éçºããããã¨ãå¯è½ã«ãªãã¾ãã
ããã§ã¯åç´ãª HelloWorld ã¢ããªã±ã¼ã·ã§ã³ã®ä¾ã§ãã
require 'rubygems' require 'rack' include Rack hello_app = Proc.new do |env| Response.new.finish do |res| res.write 'hello world!' end end Handler::Mongrel.run hello_app, :Port => 9202
ãã®ã³ã¼ããåããã¨ãmongrel ã port 9202 ã§ç«ã¡ä¸ãããhttp://localhost:9202/ ã«ã¢ã¯ã»ã¹ãã㨠hello world! ã¨è¡¨ç¤ºããã¦ããã¯ãã§ããã§ãçç®ãã¹ãã¯æå¾ã®
Handler::Mongrel.run hello_app, :Port => 9202
ã®è¡ã§ããã³ãã©ã WEBrick ã«å¤æ´ã
Handler::WEBrick.run hello_app, :Port => 9202
èµ·åãããã¨ã§ãWEBrick ãç«ã¡ä¸ãã hello world! ã表示ãããã¯ãã§ããã¾ã Handler ã¯ç¾æç¹ã§ CGI/FastCGI/Mongrel/WEBrick ãç¨æããã¦ããããããããåãæ¿ããã ãã§å¯¾å¿ããããã¨ãã§ãã¾ããç°¡åã§ããã
ä¸æ©é²ãã å©ç¨æ¹æ³
ãã³ãã©èµ·åæã«æ¸¡ãã¦ãã hello_app ã¯å¥ã« Proc ãªãã¸ã§ã¯ããããªãã¨ããcall ã¡ã½ãããå®ç¾©ããã¦ããã°ä½ã§ã OK ã§ããããã¦ãã©ã¦ã¶ããã®ãªã¯ã¨ã¹ãçºçæã« call ã¡ã½ããã®å¼æ°ã«ç°å¢å¤æ° env ã渡ããå¼ã³åºããã¾ãããã㧠Response ã®ã¤ã³ã¹ã¿ã³ã¹ãçµæã¨ãã¦è¿ãã°å¦ç㯠OK ã§ãã
ãªãã©ã¤ãã©ãªã«æ¨æºã¨ãã¦
- File
- Lint
- CommonLogger
- Cascade
- URLMap
- ShowExceptions
- Builder
ãªã©ã® call ã¡ã½ãããåããã¯ã©ã¹ãããããã®ãã¡ã®å¤§åã Proxy ã¨ãã¦ãã³ã³ã¹ãã©ã¯ã¿ã®å¼æ°ã®ã¤ã³ã¹ã¿ã³ã¹ã® call ã¡ã½ãããå¼ãã§ããã¾ãã
ãã¨ãã°ãç¾å¨ã¯ã¨ã©ã¼ãèµ·ããã¨ãã¯ä½ã«ãå¦çãè¡ã£ã¦ã¾ããããã¨ã©ã¼ãèµ·ããã¨ãã« html 㧠backtrace ã表示ãã ShowExceptions ã使ãã¨ããã¨
hello_app = Proc.new do |env| foo # error Response.new.finish do |res| res.write 'hello world!' end end Handler::Mongrel.run ShowExceptions.new(hello_app), :Port => 9202
ã®ãããªã³ã¼ãã«ãªãã¾ãã
ç»å
ãªç»é¢ã表示ããã¾ãããã¾ããã¢ã¯ã»ã¹æ¯ã«ãã°ã表示ããããªããã®ä¸ã« CommonLogger ããã¶ãã¾ãã
Handler::Mongrel.run CommonLogger.new(ShowExceptions.new(hello_app)), :Port => 9202
ã§ãã¢ã¯ã»ã¹ããã¨
192.168.155.200 - - [07/Mar/2007 16:30:52] "GET / HTTP/1.1" 500 55069 0.3931
ã®ãããªãã°ã表示ããã¾ãã
ã¾ã URL ã®ãããã³ã°ã«ã¯ URLMap ã¯ã©ã¹ã使ãã¾ããæ¬å½ã¯ Builder ã¯ã©ã¹ã§ã§ããã¯ããªã®ã§ãããªããæåãå¤ãªã®ã§ URLMap ã¯ã©ã¹ã§èª¬æãã¾ãã
hello_app = Proc.new do |env| Response.new.finish do |res| res.write 'hello world!' end end reverse_app = Proc.new do |env| req = Request.new(env) if req.GET['word'] body = CGI::escapeHTML(req.GET['word'].reverse) else body = 'set query param word' end Response.new.finish do |res| res.write body end end app = URLMap.new([ ['/reverse', reverse_app], ['/', hello_app] ]) Handler::Mongrel.run app, :Port => 9202
ãããªããã«ãURLMap ã®ã³ã³ã¹ãã©ã¯ã¿ã§ path, app ã®é åã渡ãã¦ããã¨ããããã«ãããã³ã°ããã/reverse?word=takeyabuyaketa ã«ã¢ã¯ã»ã¹ããã¨ãatekayubayekatããã/ ã«ã¢ã¯ã»ã¹ããã¨ãhello world!ãã表示ãããã¨æãã¾ãã
ã¨ããããã§ããªãç°¡åã«æ±ãã¦ããã¤æå°éã®æ©è½ã®ã¿ãæ¼ããã¦ããã®ã§ããã¡ãã¡ Rails 使ãã»ã©ãããªããã© cgi/fcgi/mongrel ãªã©ãªã©ã«ã¯ã¨ãããã対å¿ãã¦ããããããªãã¦ã²ã¨ã¯ä½¿ã£ã¦ã¿ã¦ã¯ã©ãã§ããããã