Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issues with Sinatra and Ruby 1.9.2 on Shotgun

I have a simple sinatra app.

require 'rubygems'
require 'sinatra'

get '/' do
  "Hello"
end

When I run it on Shotgun I get the following error:

Boot Error

Something went wrong while loading simple.rb

LoadError: no such file to load -- simple.rb

:29:in require' <internal:lib/rubygems/custom_require>:29:in require' /home/thedinga/.rvm/gems/ruby-1.9.2-p0@global/gems/shotgun-0.8/lib/shotgun/loader.rb:114:in inner_app' /home/thedinga/.rvm/gems/ruby-1.9.2-p0@global/gems/shotgun-0.8/lib/shotgun/loader.rb:102:in assemble_app' /home/thedinga/.rvm/gems/ruby-1.9.2-p0@global/gems/shotgun-0.8/lib/shotgun/loader.rb:86:in proceed_as_child' /home/thedinga/.rvm/gems/ruby-1.9.2-p0@global/gems/shotgun-0.8/lib/shotgun/loader.rb:31:in call!' /home/thedinga/.rvm/gems/ruby-1.9.2-p0@global/gems/shotgun-0.8/lib/shotgun/loader.rb:18:in call' /home/thedinga/.rvm/gems/ruby-1.9.2-p0@global/gems/shotgun-0.8/lib/shotgun/favicon.rb:12:in call' /home/thedinga/.rvm/gems/ruby-1.9.2-p0@global/gems/rack-1.2.1/lib/rack/builder.rb:77:in call' /home/thedinga/.rvm/gems/ruby-1.9.2-p0@global/gems/rack-1.2.1/lib/rack/content_length.rb:13:incall' /home/thedinga/.rvm/gems/ruby-1.9.2-p0@global/gems/rack-1.2.1/lib/rack/handler/webrick.rb:52:in service' /home/thedinga/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/httpserver.rb:111:in service' /home/thedinga/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/httpserver.rb:70:in run' /home/thedinga/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/server.rb:183:in block in start_thread'

If i were to use ruby simple.rb instead of shotgun, I get the output you'd expect in a browser. As a sidenote, if I push it to Heroku (which I would really like to run a sinatra app on), Heroku will fail to run the app as well. Is this a version issue with 1.9.2 ? or am I missing something else?

like image 423
Justin Soliz Avatar asked Dec 03 '10 15:12

Justin Soliz


4 Answers

the code above works, just correct require '#{path}/myapp' at config.ru file to require "#{path}/myapp" .

In ruby just strings with " caracter can use inner #{}. In string with ' caracter the string will be continue being '#{path}/myapp' and not 'value/of/path/variabel/myapp'.

So it can be done like the following

# FILE config.ru

path = File.expand_path "../", __FILE__

require 'rubygems'
require 'sinatra'
require "#{path}/myapp"

run Sinatra::Application


# FILE myapp.rb

get '/' do
  'hello'
end

star the application just running the command shotgun at app root directory

like image 65
Diego LUcena Avatar answered Nov 13 '22 03:11

Diego LUcena


Did you try decoupling the config from the app itself?

My template is something like

# FILE config.ru

path = File.expand_path "../", __FILE__

require 'rubygems'
require 'sinatra'
require '#{path}/myapp'

run Sinatra::Application


# FILE myapp.rb

get '/' do
  'hello'
end

# FILE start.sh

shotgun -o 0.0.0.0 -p 8888 &

This with ruby1.9.2-p0..

like image 21
Jack Avatar answered Nov 13 '22 03:11

Jack


A quick fix for this is discussed on the shotgun issues page on github.

$ shotgun -I. simple.rb
like image 27
jpalardy Avatar answered Nov 13 '22 03:11

jpalardy


My config.ru is typically much simpler:

root = ::File.dirname(__FILE__)
require ::File.join( root, 'app' )

# Assuming your application is subclassed from Sinatra::Application
run MyApp.new 

The app.rb file can (and should) require Sinatra (and rubygems if you're not on 1.9).

like image 1
Phrogz Avatar answered Nov 13 '22 05:11

Phrogz