appengine-jrubyã§ç°¡åGAE/JRubyéçº
å°ãåã®ã¨ã³ããªã§ãGAE/JRubyä¸ã§Rackã¢ããªãåããæé ãæ¸ãã¾ããã
ãã®ã¨ãã®æé ã¯ãJava SDKãã¤ã³ã¹ãã¼ã«ãã¦ãGAE SDKããã¦ã³ãã¼ããã¦ãjruby-complete.jarãã³ã³ãã¤ã«ãã¦â¦ã¨ãããç ©éãªãã®ã§ããããã®å½æã¯ãããããããä¸è¬çãªææ³ã ã£ãã®ã§ã¯ãªããã¨æãã¾ãã
ããããä»ãæ代ã¯å¤ããã¾ãããä»GAE/JRubyã§Rackã¢ããªãä½ãã®ã«å¿ è¦ãªä½æ¥ã¯ããã£ãã²ã¨ã¤ã®gemã®ã¤ã³ã¹ãã¼ã«ã ãã§ãã
æå¿ã«ãã£ã¦GAE/JRubyä¸ã®éçºãæ¯æ´ãã"appengine-jruby"ã¨ããããã¸ã§ã¯ããé²ãããã¦ããããã®ææã¨ãã¦"google-appengine"ã¨ããgemãéçºããã¦ãã¾ãããããå©ç¨ããã°ãGAE SDKããjruby-complete.jarããJRuby-Rackããä½ä¸ã¤èªåã§ç¨æããå¿ è¦ã¯ããã¾ããã*1æ´ã«ãè¨å®ç¨ã®XMLãã¡ã¤ã«ããæ¸ãå¿ è¦ãããã¾ãããé©ãã»ã©ç°¡åã«GAE/JRubyä¸ã«Rackã¢ããªãæ§ç¯ã§ãã¦ãã¾ãã¾ãã*2
å®éã«ããã·ã³ãã«ãªRackã¢ããªã¨Sinatraã¢ããªãä½ãã¤ã¤ãã©ãã»ã©ãæ軽ã«GAE/JRubyã«ããéçºãã§ãã¦ãã¾ãã®ããå®æãã¦ã¿ããã¨æãã¾ãã
ãªãã以ä¸ã®ä½æ¥ã¯å ¨ã¦CentOS 5.3ä¸ã§Ruby 1.9.1ã使ã£ã¦è¡ã£ã¦ãã¾ããã¾ãJDKã¯yumçµç±ã§java-1.6.0-openjdkããã±ã¼ã¸ã¨java-1.6.0-openjdk-develããã±ã¼ã¸ãã¤ã³ã¹ãã¼ã«æ¸ã¿ã§ãã
ã¾ãä½ã£ã¦ã¿ã
æåã¯ã決ã¾ãã®"Hello world"ã§ãã£ã¦ã¿ã¾ããç´ ã®Rackã¢ããªã§ãã以前のエントリã§ãã¾ããããä½æãã¾ããã®ã§ããã®æã®ä½æ¥éã¨æ¯è¼ããã¨"google-appengine gem"ã®å¨åããããããããããªãã¨æãã¾ãã
gemã®ã¤ã³ã¹ãã¼ã«
以ä¸ã®ã³ãã³ããå®è¡ãã¾ããããã§å®è¡ããgemã¯CRubyã®gemã§ãã®ã§ãæ°ãä»ãã¦ãã ããã
$ gem install google-appengine Successfully installed rack-1.0.0 Successfully installed appengine-rack-0.0.2 Successfully installed appengine-sdk-1.2.2 Successfully installed appengine-jruby-jars-0.0.2 Successfully installed appengine-tools-0.0.2 Successfully installed google-appengine-0.0.2 6 gems installed
ããã¤ãä¾åé¢ä¿ã«ããgemãä¸ç·ã«ã¤ã³ã¹ãã¼ã«ããã¾ããGAE SDKãªã©å¿ è¦ãªãã®ã¯å ¨ã¦ãããã®ä¸ã«å«ã¾ãã¦ãã¾ãã
ã¢ããªã±ã¼ã·ã§ã³ã®ä½æ
ã§ã¯ã¢ããªã±ã¼ã·ã§ã³ã®ä½æã«åãæããã¾ããã¾ãé©å½ã«ãã£ã¬ã¯ããªãæãã¾ãã
$ mkdir hello $ cd hello
次ã«ã"config.ru"ã¨ããåå*3ã§ã¢ããªã±ã¼ã·ã§ã³ã®è¨å®ãã¡ã¤ã«å ¼ã½ã¼ã¹ãã¡ã¤ã«ãä½æãã¾ãã
- config.ru
require 'appengine-rack' AppEngine::Rack.configure_app( :application => 'application-id', # Replace your application id. :version => 1 # Replace your application version. ) run lambda { Rack::Response.new('Hello world!') }
"*.ru"ã¨ãããã¡ã¤ã«ã¯rackupç¨ã®è¨å®ãã¡ã¤ã«ã§ããrackupã¨ããã®ã¯rackã®ããã«ã¦ã§ã¢ãç°¡åã«æ±ãããã®ã¦ã¼ãã£ãªãã£ã§ããããã®è¾ºãã®è©³ãããã¨ã¯ä»¥ä¸ã®ãã¼ã¸çãåç §ãã¦ãã ããã
- ウノウラボ Unoh Labs: RackでWebアプリのWebサーバー依存を無くす
- KoshigoeBLOG: Rack解説を試みて失敗しつつも晒す、の巻
- あーありがち - CGI を rackup してみた
ã³ã¼ãã®ä¸èº«ã¯ãã ãããããã¨è¦ã¦æå³ã¯ããããã¨æãã¾ããè¨å®ã¨ãã¦æä½éå¿ è¦ãªãã¢ããªã±ã¼ã·ã§ã³IDãã¨ããã¼ã¸ã§ã³çªå·ããã»ããããrunã¡ã½ããã«æ¸¡ããããã¯*4å ã§ã¢ããªã±ã¼ã·ã§ã³ã®å¦çæ¬ä½ãè¨è¿°ãã¦ãã¾ããããã§ã¯åã«"Hello world!"ãè¿ãã¦ããã ãã§ãã
éçºãµã¼ãã®èµ·å
ã§ã¯éçºãµã¼ããèµ·åãã¦ãã¢ããªã±ã¼ã·ã§ã³ã®åä½ã確èªãã¦ã¿ã¾ãã
ã¾ã ä»ã®è¨å®ãã¡ã¤ã«ãã©ã¤ãã©ãªãä½ãç¨æãã¦ããªãï¼å¤§ä¸å¤«ã§ãããã®è¾ºã¯å ¨ã¦"google-appengine gem"ã®ã¦ã¼ãã£ãªãã£ç¾¤ããªãã¨ããã¦ããã¾ãã
$ dev_appserver.rb .
å¤ããã¢ã¯ã»ã¹ããå ´åã¯ã次ã®ããã«ã¢ã¯ã»ã¹ãåãä»ããã¢ãã¬ã¹ã®ç¯å²ãæå®ãã¾ãããã®è¾ºãã¯GAE SDKã«å«ã¾ããdev_appserver.shã¨åãã§ãã
$ dev_appserver.rb --address=0.0.0.0 .
ã³ãã³ãå®è¡å¾ãã¤ãã¤ãã¨ãã°ã表示ããã¦ããã¾ããããã®ã¿ã¤ãã³ã°ã§ããã¸ã§ã¯ãã«å¿ è¦ãªã©ã¤ãã©ãªãã³ãã¼ããè¨å®ãã¡ã¤ã«ãèªåçæãã¦ãã¾ãããã®è¾ºãã®åæåå¦çã¯ãé¢ä¿ããé¨åã«å¤æ´ãç¡ããã°æ¬¡å以éã¯ã¹ãããããã¾ãã
ãã°ããå¾ ã¤ã¨ä»¥ä¸ã®ã¡ãã»ã¼ã¸ã表示ãããã¢ã¯ã»ã¹å¯è½ãªç¶æ ã«ãªãã¾ããå®éã«ãã©ã¦ã¶ã§ã¢ã¯ã»ã¹ãã¦åä½ã確èªãã¦ã¿ã¦ãã ããã
The server is running at http://localhost:8080/
GAEã¸ã®ã¢ãããã¼ã
æå¾ã«ãæ¬çªç°å¢ã§åããã¦ã¿ã¾ãã
ã¢ãããã¼ãåã«ãå ç¨ã®"config.ru"å ã®ã¢ããªã±ã¼ã·ã§ã³IDã¨ãã¼ã¸ã§ã³çªå·ãæ£ãããã¨ã確èªãã¦ãã ããã
$ appcfg.rb update .
éä¸ã§ã¡ã¼ã«ã¢ãã¬ã¹ã¨ãã¹ã¯ã¼ããèããã¾ãã®ã§å ¥åãã¾ãã
Update completed successfully. Success. Cleaning up temporary files...
ä¸è¨ã®ã¡ãã»ã¼ã¸ãåºåããããç¡äºæåã§ããGAEä¸ã®ã¢ããªã±ã¼ã·ã§ã³ã«ã¢ã¯ã»ã¹ãã¦åä½ã確èªãã¦ã¿ã¦ãã ããã
ã¡ãªã¿ã«ãéçºãµã¼ããä¸åº¦ãç«ã¡ä¸ããã«GAEã¸ã¢ãããã¼ãããå ´åã§ããã¢ãããã¼ãåã«ã¡ããã¨å¿ è¦ãªã©ã¤ãã©ãªãè¨å®ãã¡ã¤ã«ãçæãããã®ã§åé¡ããã¾ããããã¾ãç¡ãã±ã¼ã¹ã ã¨ã¯æãã¾ããã
æé ã®ã¾ã¨ã
以ä¸ã§"Hello world"ã¢ããªã¯å®æã§ãã
æé ãããããããã¨ã
- gemã®ã¤ã³ã¹ãã¼ã«ï¼2åç®ä»¥éã¯ä¸è¦ï¼
- "config.ru"ã®ä½æ
- éçºãµã¼ãã®èµ·åï¼ã¹ãããå¯ï¼
- ã¢ãããã¼ã
ã ãã§ãã以åã®ãèªåã§GAE/JRubyç°å¢ãç¨æãã¦ããããã¨æ¯ã¹ãã¨ãé©ãã»ã©ç°¡åã«ãªãã¾ããã
Sinatraã¢ããªãä½ã£ã¦ã¿ã
ã¨ã¯è¨ããç´ ã®Rackã¢ããªã§è²ã ããã®ã¯ä½ãã¨ããã¤ã®ã§ãSinatraãå°å ¥ããã¨ãããããã¾ã§ã¯ãã£ã¦ãããã¨æãã¾ãã
Sinatraã¯ã¨ã¦ãã·ã³ãã«ã§æ±ããããWebã¢ããªã±ã¼ã·ã§ã³ãã¬ã¼ã ã¯ã¼ã¯ã§ããå ¬å¼ã«ã¯âæå°å´åã§ææ©ãã¦ã§ãã¢ããªã±ã¼ã·ã§ã³ãä½æããããã®DSLâã ã¨èª¬æããã¦ãã¾ãã
ã·ã³ãã«ã§ãããWebã¢ããªã±ã¼ã·ã§ã³ãæ¸ãã®ã«å¿ è¦ååãªæ©è½ã¯æã£ã¦ãã¾ããGAE/JRubyã§è»½ãã®Webã¢ããªããµã¯ãã¨ä½ãããå ´åã¯Railsããé©ãã¦ããããããã¾ããã
Sinatraã«ã¤ãã¦ã¯ä»¥ä¸ã®ãã¼ã¸çãåèã«ãã¦ãã ããã
- Sinatra: README (Japanese)
- Rubyの軽量Webフレームワーク「Sinatra」がステキ - 医者を志す妻を応援する夫の日記
- ウノウラボ Unoh Labs: Sinatra気に入った
Sinatraã®ã¤ã³ã¹ãã¼ã«
Sinatraã¯ä¸ç·ã«GAEã«ã¢ãããã¼ããããã¨ã«ãªãã¾ãã®ã§ãããã¸ã§ã¯ãã®ãã£ã¬ã¯ããªå ã«ã¤ã³ã¹ãã¼ã«ãã¾ãã
ããã以åã¯gemã³ãã³ãã®-iãªãã·ã§ã³çã使ã£ã¦ãã¾ãããã"google-appengine gem"ã«ä»ãã¦ããappcfg.rbã³ãã³ãçµç±ã§gemã®ç®¡çãã§ããããã«ãªãã¾ããã
以ä¸ã®ããã«é©å½ã«ãã£ã¬ã¯ããªãä½æå¾ããã®ä¸ã§ã¤ã³ã¹ãã¼ã«ãè¡ãã¾ãã
$ mkdir greeting $ cd greeting $ appcfg.rb gem install sinatra Successfully installed rack-1.0.0 Successfully installed sinatra-0.9.4 2 gems installed
ããã¸ã§ã¯ããã£ã¬ã¯ããªå ã«ã¤ã³ã¹ãã¼ã«ãã¦ãã¾ãã®ã§ãå¥ã®ããã¸ã§ã¯ããä½æããéã¯ãã®é½åº¦ã¤ã³ã¹ãã¼ã«ããå¿ è¦ãããã¾ãã
è¨å®ãã¡ã¤ã«ã®ä½æ
"config.ru"ãä½æãã¾ããä»åã¯ã¢ããªã±ã¼ã·ã§ã³æ¬ä½ã¯å¥ãã¡ã¤ã«ã«ãã¾ãã®ã§ã"config.ru"ã¯ç´ç²ã«è¨å®ãã¡ã¤ã«ã®å½¹å²ãæããã¾ãã
- config.ru
require 'appengine-rack' require 'application' AppEngine::Rack.configure_app( :application => 'application-id', # Replace your application id. :version => 1 # Replace your application version. ) run Sinatra::Application
2è¡ç®ã§requireãã¦ããã®ãã¢ããªã±ã¼ã·ã§ã³æ¬ä½ã®ãã¡ã¤ã«ã§ããä»ã¯ç¹ã«èª¬æã¯è¦ããªããã¨æãã¾ãã
ã¢ããªã±ã¼ã·ã§ã³ãã¡ã¤ã«ã®ä½æ
Sinatraãåããã¨ã確èªã§ããã°ããã®ã§ããã¹ã«æå®ããæååã«åããã¦ãããã¤ãå¤ããã ãã®ç°¡åãªã¢ããªã±ã¼ã·ã§ã³ã«ãã¾ããâHello world ã¬ãã«2âã¨ãã£ãæãã§ãã
- application.rb
require 'rubygems' require 'sinatra' get '/' do 'Hello world!' end get '/morning' do 'Good morning.' end get '/evening' do 'Good evening.' end get '/night' do 'Good night.' end get '/bye' do 'Good bye.' end
ãããç¹ã«èª¬æã¯è¦ããªããã¨æãã¾ãã
éçºãµã¼ãã®èµ·åï¼ã¢ãããã¼ã
ãã以éã¯åºæ¬çã«å ç¨ã¨åãä½æ¥ã§ãã
$ dev_appserver.rb --address=0.0.0.0 .
ã§åä½ã確èªãã
$ appcfg.rb update .
ã§GAEã¸ã¢ãããã¼ããã¾ãã
次ã®ã¹ãããã¸
appengine-jrubyã«å«ã¾ããã®ã¯GAE/JRubyã¢ããªã®ä½ææ¯æ´æ©è½ã ãã§ã¯ããã¾ããã
以ä¸ã®ããã¥ã¡ã³ããè¦ã¦ããããéããDatastoreã«URLFetchãªã©ãGAEã«ç¨æããã¦ããä¸éãã®ãµã¼ãã¹ãå©ç¨ããããã®APIãæã£ã¦ãã¾ãã
ã¾ã APIã®è©³ç´°ã¯ç¢ºèªãã¦ãã¾ããããããããããä¸ã¤ã§ãããªãã«ãã¡ãã¨ããWebã¢ããªã±ã¼ã·ã§ã³ã¯ä½ãã¦ãã¾ãã®ã§ã¯ãªãã§ãããããããJavaã®APIãæèããå¿ è¦ãç¡ãããã§ãã
ããã¾ã§Javaã®ä½ã¬ãã«APIãç´æ¥è§¦ã£ã¦è²ã ãã£ã¦ãã¾ãããããã£ãããªã®ã§æ¹éã転æããappengine-jrubyã使ãåãæ¹åã§è²ã å¼ã£ã¦ã¿ããã¨æãã¾ãã
ã¨ããããã¯appengine-jrubyã§Twitter botã§ãä½ã£ã¦ã¿ããããªï¼*5