TOTD #79: Getting Started with Sinatra applications on JRuby and GlassFish Gem


Sinatra is a DSL for quickly creating web-applications in Ruby with minimal effort. Like Rails and Merb, Sinatra is not an MVC framework and basically follows a flat-file structure instead. The framework define conventions such as location of static files and views, bootstrap, dev/production/test environment variables, filters, helpers, TDD, and much more.  Read Getting Started for complete details. Even though Sinatra is not a MVC framework but sinatra-gen may be used to generate new Sinatra projects.

GlassFish Gem can easily run Rails, Merb, Sinatra, and any other Ruby framework applications based upon Rack. TOTD #70 shows how to run Rails applications and TOTD #53 shows to run Merb applications. This TOTD will explain how to run a trivial Sinatra application. A later blog will describe how to plug a generic Rack-based framework.

Lets see how to get started with Sinatra using JRuby and GlassFish gem.
  1. Install Sinatra gem as:

    ~/tools/jruby >./bin/jruby -S gem install sinatra
    JRuby limited openssl loaded. gem install jruby-openssl for full support.
    http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
    Successfully installed sinatra-0.9.1.1
    1 gem installed
    Installing ri documentation for sinatra-0.9.1.1...
    Installing RDoc documentation for sinatra-0.9.1.1...

  2. Create a directory "sinatra-sample", create a file "hello.rb" in that directory with the contents shown below:

    require 'rubygems'
    require 'sinatra'
    get '/hi' do
      "Hello World!"
    end

  3. Run your sample using GlassFish gem as:

    ~/tools/jruby/samples/sinatra-sample >../../bin/jruby -S glassfish
    Log file /Users/arungupta/tools/jruby-1.2.0/samples/sinatra-sample/log/development.log does not exist. Creating a new one...
    Starting GlassFish server at: 192.168.1.145:3000 in development environment...
    Writing log messages to: /Users/arungupta/tools/jruby-1.2.0/samples/sinatra-sample/log/development.log.
    Press Ctrl+C to stop.

    And then the output is available at "http://localhost:3000/hi" and looks like:


Neat and simple!

You'll hear all about it at Develop with Pleasure, Deploy with Fun: GlassFish and NetBeans for a Better Rails Experience at Rails Conf next week.

Here is the order in which I'll seek any help:
The next blog will show how to create a Sinatra CRUD application and run it using GlassFish.

Please leave suggestions on other TOTD (Tip Of The Day) that you'd like to see. A complete archive of all the tips is available here.
Technorati: totd glassfish jruby sinatra
Comments:

Thanks for the info, it was very easy for me to get Sinatra running in Glassfish.

However, I need to restart Glassfish to see any changes to my Sinatra file. Is this normal? Is there a better way to develop?

Thanks!

Posted by Seth Ladd on March 30, 2010 at 11:04 AM PDT #

Seth Ladd,

I remember facing this issue earlier and described at:

http://blogs.sun.com/arungupta/entry/rails_conf_2009_day_1

It says:

-- cut here --
Sinatra allows reloading of application but that "feature" will be removed soon. Instead install shotgun (which does not work with JRuby yet!).
-- cut here --

JRuby do support shotgun now though. So try it and let me know.

Posted by Arun Gupta on March 31, 2010 at 03:24 AM PDT #

Post a Comment:
Comments are closed for this entry.
About

profile image
Arun Gupta is a technology enthusiast, a passionate runner, author, and a community guy who works for Oracle Corp.


Java EE 7 Samples

Stay Connected

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today