What is the preferred way to run Sinatra application?

1.1k views Asked by At

EDIT: I got the application working using TorqueBox, so I guess the problem is related to Trinidad. However, I think my guestion about the preferred way to run Sinatra applications is still valid.

I am using JRuby, Bundler, and modular style. Here's my application code:

# myapp.rb
class MyApp < Sinatra::Base
  configure :development do
    register Sinatra::Reloader
  end

  set :server, 'trinidad'
  set :environment, :development
  set :logging, true
  #...
end

# config.ru
require 'bundler/setup'

Bundler.require(:default)

$: << File.dirname(__FILE__)

require 'myapp'

run MyApp

# Gemfile
source 'http://rubygems.org'

gem 'rack'
gem 'sinatra', :require => 'sinatra/base'
gem 'sinatra-contrib', :require => 'sinatra/reloader'
gem 'coffee-script'
gem 'neography'
gem 'gon-sinatra'
gem 'sass'
gem 'trinidad'

What is the proper way (i.e. what kind of command) to launch my application? I'm using Trinidad, because I read that it's good solution for JRuby applications (I couldn't get TorqueBox to work). Currently I'm running my app by 'trinidad config.ru', but it throws following kind error messages:

application error
org.jruby.rack.RackInitializationException: no such file to load -- bundler/setup
    from org/jruby/RubyKernel.java:991:in `require'
    from /home/myhome/.rvm/rubies/jruby-1.7.0.preview1/lib/ruby/shared/rubygems/custom_require.rb:36:in `require'
    from /home/myhome/RubymineProjects/myapp/config.ru:1:in `(root)'
    from org/jruby/RubyBasicObject.java:1818:in `instance_eval'
    from jar:file:/home/myhome/.rvm/gems/jruby-1.7.0.preview1/gems/jruby-rack-1.1.9/lib/jruby-rack-1.1.9.jar!/vendor/rack-1.4.1/rack/builder.rb:51:in `initialize'
    from /home/myhome/RubymineProjects/myapp/config.ru:1:in `(root)'

    at org.jruby.rack.DefaultRackApplicationFactory$4.init(DefaultRackApplicationFactory.java:224)
    at org.jruby.rack.DefaultRackApplicationFactory.getApplication(DefaultRackApplicationFactory.java:58)
    at org.jruby.rack.PoolingRackApplicationFactory.createApplication(PoolingRackApplicationFactory.java:323)
    at org.jruby.rack.PoolingRackApplicationFactory.getApplication(PoolingRackApplicationFactory.java:171)
    at org.jruby.rack.DefaultRackDispatcher.getApplication(DefaultRackDispatcher.java:27)
    at org.jruby.rack.AbstractRackDispatcher.process(AbstractRackDispatcher.java:32)
    at org.jruby.rack.AbstractServlet.service(AbstractServlet.java:37)
    at org.jruby.rack.AbstractServlet.service(AbstractServlet.java:43)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
Caused by: org.jruby.exceptions.RaiseException: (LoadError) no such file to load -- bundler/setup
    at org.jruby.RubyKernel.require(org/jruby/RubyKernel.java:991)
    at Kernel.require(/home/myhome/.rvm/rubies/jruby-1.7.0.preview1/lib/ruby/shared/rubygems/custom_require.rb:36)
    at RUBY.(root)(/home/myhome/RubymineProjects/myapp/config.ru:1)
    at org.jruby.RubyBasicObject.instance_eval(org/jruby/RubyBasicObject.java:1818)
    at RUBY.initialize(jar:file:/home/myhome/.rvm/gems/jruby-1.7.0.preview1/gems/jruby-rack-1.1.9/lib/jruby-rack-1.1.9.jar!/vendor/rack-1.4.1/rack/builder.rb:51)
    at RUBY.(root)(/home/myhome/RubymineProjects/myapp/config.ru:1)
ignoring null application

I'm using versions 1.4.1 of Trinidad and Rack, 1.7.0.preview1 of JRuby, and 1.3.3 of Sinatra.

If I run 'bundle exec trinidad config.ru', I get following errors:

application error
org.jruby.rack.RackInitializationException: Valid types are [:development, :runtime], 
    from /home/myhome/.rvm/rubies/jruby-1.7.0.preview1/lib/ruby/shared/rubygems/dependency.rb:45:in `initialize'
    from /home/myhome/.rvm/rubies/jruby-1.7.0.preview1/lib/ruby/shared/rubygems/specification.rb:821:in `add_dependency_with_type'
    from :1:in `'
    from :1:in `'
    from org/jruby/RubyKernel.java:1037:in `eval'
    from org/jruby/RubyArray.java:1611:in `each'
    from org/jruby/RubyArray.java:1611:in `each'
    from org/jruby/RubyEnumerable.java:649:in `find_all'
    from org/jruby/RubyKernel.java:991:in `require'
    from org/jruby/RubyKernel.java:991:in `require'
    from org/jruby/RubyKernel.java:991:in `require'

    at org.jruby.rack.DefaultRackApplicationFactory.createApplication(DefaultRackApplicationFactory.java:235)
    at org.jruby.rack.DefaultRackApplicationFactory.newApplication(DefaultRackApplicationFactory.java:49)
    at org.jruby.rack.DefaultRackApplicationFactory.getApplication(DefaultRackApplicationFactory.java:57)
    at org.jruby.rack.PoolingRackApplicationFactory.createApplication(PoolingRackApplicationFactory.java:323)
    at org.jruby.rack.PoolingRackApplicationFactory.getApplication(PoolingRackApplicationFactory.java:171)
    at org.jruby.rack.DefaultRackDispatcher.getApplication(DefaultRackDispatcher.java:27)
    at org.jruby.rack.AbstractRackDispatcher.process(AbstractRackDispatcher.java:32)
    at org.jruby.rack.AbstractServlet.service(AbstractServlet.java:37)
    at org.jruby.rack.AbstractServlet.service(AbstractServlet.java:43)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
Caused by: org.jruby.exceptions.RaiseException: (ArgumentError) Valid types are [:development, :runtime], 
    at Gem::Dependency.initialize(/home/myhome/.rvm/rubies/jruby-1.7.0.preview1/lib/ruby/shared/rubygems/dependency.rb:45)
    at Gem::Specification.add_dependency_with_type(/home/myhome/.rvm/rubies/jruby-1.7.0.preview1/lib/ruby/shared/rubygems/specification.rb:821)
    at RUBY.(:1)
    at RUBY.(:1)
    at org.jruby.RubyKernel.eval(org/jruby/RubyKernel.java:1037)
    at org.jruby.RubyArray.each(org/jruby/RubyArray.java:1611)
    at org.jruby.RubyArray.each(org/jruby/RubyArray.java:1611)
    at org.jruby.RubyEnumerable.find_all(org/jruby/RubyEnumerable.java:649)
    at org.jruby.RubyKernel.require(org/jruby/RubyKernel.java:991)
    at org.jruby.RubyKernel.require(org/jruby/RubyKernel.java:991)
    at org.jruby.RubyKernel.require(org/jruby/RubyKernel.java:991)
ignoring null application
2

There are 2 answers

0
kares On

rubygems is not loaded by default (at least with jruby --1.8) thus you'll either add require 'rubygems' on top of the config.ru as JRuby (actually JRuby-Rack) loads your app in a separate "runtime" (kind of a new ruby process but inside the same JVM process) by default. alternatively, you might as well run using rackup -s trinidad

0
digitalextremist On

As far as the CLI command to invoke your rack configuration and application, kares is right. Using "rackup" with "-s trinidad" is the way to go if you are not using Torquebox.

As far as the Ruby command to call your application from within your rack config, here is what I do.. Not sure if this applies in your situation, if this is what you're looking for, but it will help regardless of HTTP handler:

You have MyApp, which mixes in Sinatra::Base. If you trim that class down to only configure your environment, and run "before do" and "after do" for every pageload... but not actually handle and get/post calls; then you can create specialized classes which handle certain paths in your overall application. This uses Rack::Builder's map() method...

require 'myapp'
require 'mylanding'
require 'myauthentication'
require 'myautocomplete'
require 'mysearch'

#de run MyApp

map "/" do
    run MyLanding    #de Catch-all, which would have "get '/*' do" in it too.
end

map "/authentication/" do
    run MyAuthentication
end

map "/autocomplete/" do
    run MyAutocomplete
end

map "/search/" do
    run MySearch
end

Then inside each My* class, do this:

class MyLanding < MyApp
  get "/*" do
    ..handle landing.. or see what's in the splat.
  end
end

class MyAuthentication < MyApp
  get "/sign/in" do
    ..present sign-in..
  end
  post "/sign/in" do
    ..handle sign-in..
  end
end

etc...

Your sub-classes have the overall MyApp treatment of every pageload, but add routes, such that /authentication/sign/in is handled by MyAuthentication's /sign/in, for example. It's a cool way of having somewhat focused sub-applications all using the same foundation: MyApp.