I’m using JRuby more and more these days to deploy Rails apps. Typically that means creating a war file and deploying it on Glassfish. Yesterday I had request that seemed would be best fulfilled with a Sinatra app rather than Rails. It was my first experience getting Sinatra running on Glassfish and I thought I would share it.
The goal state here is a war file in the Glassfish autodeploy directory and the tool of choice is Warbler.
gem install warbler
With that installed we need to get ourselves set up to warble something. Warbler expects a specific directory structure it turns out, which is pretty typical for those familiar with the Ruby/Rails world:
mike@sleepycat:~/projects/myapp☺ find .
Of course you can add a views directory and other stuff in there as well. If you start doing things that Warbler does not expect you might have to add a warble.rb file but since I am using bundler and doing things that fall within warblers expectations I didn’t need one. I had a small number of very simple views, so I chose not to make a views directory and opted for named templates in the main app. To make this a little easier to digest, I’ve stripped out my code and made a basic hello world style app below thats going to be my starting point for next time I do this sort of thing. The three pieces you need are the Gemfile, the rackup file and the app itself.
group :development do
set :run, false #disable the built-in web server
set :environment, :production
And, sitting inside the lib directory, the application itself with a named template:
get '/' do
@greeting = “Bonjour”
template :index do
“<%= @greeting %> from Sinatra”
If you want to try it out at this point you can launch the app using rackup:
With those files in place you are ready to create a war file by running the “warble” command in the project directory. What that does is create a war file with the following structure:
...all the gemspecs from the gem dependencies end up here...
... All the gems that your project depends on are copied in to this folder ...
And now you can drop that war in the Glassfish autodeploy directory and off it goes. My app was simple enough that I didn’t even need to use a database so you might need to add to this a little but it looks pretty simple. In fact every time I end up using Sinatra I come away impressed by how clean and simple it is. Now that I know how to run this on Glassfish, the next time may come sooner than later.