Ruby Sinatra application on Google App Engine Java
Wednesday, June 17th, 2009
This article is all about what I have done to make my first Sinatra application on Google’s App engine/Java aka GAE. As you know, Sinatra is a light web application framework for Ruby. We can run Sinatra on GAE because GAE has started to support Java since April 2009 and there existed a Java implementation of Ruby called JRuby.
Before proceeding, you are sure that you already have a valid account for GAE. Otherwise, hurry up to register your account at here.
OK, now you are ready and let’s get started!
Install JRuby

First, download a JRuby package using git. If you are unfamiliar with Git, tutorials and articles on github, including screencasts and podcasts are your good introduction.
Installation of git on Ubuntu is as easy as
% apt-get install git-core
If you are using other operating systems, check http://github.com/guides/home.
To install JRuby, type
% git clone git://kenai.com/jruby~main
Do not type jruby-main. jruby~main is correct.
Moving to jruby~main directory, start compiling.
% cd jruby~main % ant % ant jar-complete
Check the jruby version with
% bin/jruby –version jruby 1.4.0dev (ruby 1.8.6p287) (2009-06-16 6586) (Java HotSpot(TM) Client VM 1.5.0_16) [i386-java]
Install Sinatra and related gems

Check the gem version with
% bin/jruby –S gem –version 1.3.3
To install rubygems for your new jruby, try
% bin/jruby –S gem install rake sinatra wabler mongrel JRuby limited openssl loaded. gem install jruby-openssl for full support. <a href="http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL">http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL</a> Successfully installed rake-0.8.7 Successfully installed rack-1.0.0 Successfully installed sinatra-0.9.2 Successfully installed warbler-0.9.13 Successfully installed gem_plugin-0.2.3 Successfully installed mongrel-1.1.5-java 6 gems installed
Did you see OpenSSL messages, too? You can leave it for now.
Install JRuby-Rack
Get a copy of jruby-rack from github.
% cd ../ # only if you are still in jruby~main directory % git clone git://github.com/nicksieger/jruby-rack.git % cd jruby-rack % ../jruby\~main/bin/jruby -S rake SKIP_SPECS=true
Be careful not to forget to add “SKIP_SPECS=true” when you perform rake command in jruby-rack. Without it, rake will fail.
Create Sinatra Appcalition
Now, let’s create a new Sinatra application. First make a directory for our first app.
% mkdir myway-app
Make directories
% mkdir views public config lib
Create files
1. config.ru
require 'rubygems' require 'sinatra' root_dir = File.dirname(__FILE__) set :environment, :production set :root, root_dir set :app_file, File.join(root_dir, 'app.rb') disable :run require 'app' run Sinatra::Application
2. app.rb
require 'rubygems' require 'sinatra' get '/' do "Hello from Sinatra running on Java!" end
3. appengine-web.xml
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0"> <application>live-ch</application> <version>1</version> <static-files /> <resource-files /> <sessions-enabled>false</sessions-enabled> <system-properties> <property name="jruby.management.enabled" value="false" /> <property name="os.arch" value="" /> <property name="jruby.compile.mode" value="JIT"/> <!-- JIT|FORCE|OFF --> <property name="jruby.compile.fastest" value="true"/> <property name="jruby.compile.frameless" value="true"/> <property name="jruby.compile.positionless" value="true"/> <property name="jruby.compile.threadless" value="false"/> <property name="jruby.compile.fastops" value="false"/> <property name="jruby.compile.fastcase" value="false"/> <property name="jruby.compile.chainsize" value="500"/> <property name="jruby.compile.lazyHandles" value="false"/> <property name="jruby.compile.peephole" value="true"/> </system-properties> </appengine-web-app>
4. config/warble.rb
Warbler::Config.new do |config| config.dirs = %w(lib views public) config.includes = FileList["appengine-web.xml", "app.rb"] config.staging_dir = 'war' config.java_libs = [] config.gems = ['sinatra'] config.gem_dependencies = true config.webxml.booter = :rack end
For a test run,
% ruby app.rb == Sinatra/0.9.2 has taken the stage on 4567 for development with backup from Mongrel
Go to http://localhost:4567/, and you will see a string message that sinatra is running. OK, press Ctrl+C to stop the server.
Setup for GAE

You need to download GAE SDK for Java from here. The latest version at this moment is version 1.2.1, released on May 13, 2009.
After the download is finished, we need to copy appengine-java-sdk-1.2.1/lib/user/appengine-api-1.0-sdk-1.2.1.jar to Sinatra Application’s lib directory.
% cp ~/Downloads/appengine-java-sdk-1.2.1/lib/user/appengine-api-1.0-sdk-1.2.1.jar lib/
Also, we need to copy jruby-rack jars to lib directory.
% cp ../jruby-rack/target/jruby-rack-0.9.5-SNAPSHOT.jar lib/
Next, we need to copy jruby itself in lib directory, and split it into a two files because the original jruby-complete.jar is too big to use in GAE.
% cp ../juby~main/lib/jruby-complete.jar lib/
Then, create a shellscript called split-jar.sh
#!/bin/sh rm -rf jruby-core.jar rm -rf ruby-stdlib.jar rm -rf tmp_unpack mkdir tmp_unpack cd tmp_unpack jar xf ../jruby-complete.jar cd .. mkdir jruby-core mv tmp_unpack/org jruby-core/ mv tmp_unpack/com jruby-core/ mv tmp_unpack/jline jruby-core/ mv tmp_unpack/jay jruby-core/ mv tmp_unpack/jruby jruby-core/ cd jruby-core jar cf ../jruby-core.jar . cd ../tmp_unpack jar cf ../ruby-stdlib.jar . cd .. rm -rf jruby-core rm -rf tmp_unpack rm -rf jruby-complete.jar
And run
% sh split-jar.sh
Try “ls” to see that you have “jruby-core.jar” and “jruby-stdlib.jar” instead of “jruby-complete.jar”.
Now it is time to create “war” files with warbler.
% ../jruby~main/bin/jrruby –S warble
Run at localhost.
% ../appgengine-java-sdk-1.2.1/bin/dev_appserver.sh --port=18080 war
If you have no error, open http://localhost:18080/ with your own browser. you will see the messages.
Deployment
You need to overwrite APPLICATION-ID with your own in appengine-web.xml. When finished, create war files again as
% ../jruby~main/bin/jrruby –S warble
Then running deployment script
% ../appgengine-java-sdk-1.2.1/bin/appcfg.sh update war
If you run this script first time, you will be prompted to input your email address and password. Input your email of your GAE account correctly. After a while, the script will be finished, and you can see your Sinatra/GAE apps at http://{YOUR-APPLICATION-ID}.appspot.com.
This is all. Enjoy making Ruby web application on GAE!