How to deploy rack apps with ruby1.9? - ruby

I've been trying to get a simple hello world web app running on Sinatra.
I have the following setup:
config.ru
require 'hello.rb'
run Sinatra::Application
hello.rb
require "sinatra"
get "/" do
"Hello World!\n"
end
Gemfile
source :rubygems
gem 'sinatra'
This works on my local machine if I run ruby hello.rb, ruby1.9.1 hello.rb or rackup and go to the correct address in my browser. However, this doesn't work when deployed to heroku or if I run rackup1.9.1. I get the following error:
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- hello.rb (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from config.ru:1:in `block in <main>'
from /usr/lib/ruby/1.9.1/rack/builder.rb:46:in `instance_eval'
from /usr/lib/ruby/1.9.1/rack/builder.rb:46:in `initialize'
from config.ru:1:in `new'
from config.ru:1:in `<main>'
from /usr/lib/ruby/1.9.1/rack/builder.rb:35:in `eval'
from /usr/lib/ruby/1.9.1/rack/builder.rb:35:in `parse_file'
from /usr/lib/ruby/1.9.1/rack/server.rb:113:in `app'
from /usr/lib/ruby/1.9.1/rack/server.rb:189:in `wrapped_app'
from /usr/lib/ruby/1.9.1/rack/server.rb:155:in `start'
from /usr/lib/ruby/1.9.1/rack/server.rb:83:in `start'
from /usr/bin/rackup1.9.1:4:in `<main>'
How can I get this working? I'm guessing config.ru should be different but I don't know what to change.

Try doing
require './hello'
or
require_relative 'hello'
The current directory is not present in the LOAD_PATH by default in Ruby 1.9.2.

Related

Passenger throwing error for Sinatra app on Amazon EC2

I am trying to start Passenger with a Sinatra app on an Amazon EC2 instance and I get the following error:
$ passenger start -e production
=============== Phusion Passenger Standalone web server started ===============
PID file: /home/ec2-user/github/mainpage/tmp/pids/passenger.3000.pid
Log file: /home/ec2-user/github/mainpage/log/passenger.3000.log
Environment: production
Accessible via: http://0.0.0.0:3000/
You can stop Phusion Passenger Standalone by pressing Ctrl-C.
Problems? Check https://www.phusionpassenger.com/documentation/Users%20guide%20Standalone.html#troubleshooting
===============================================================================
App 29028 stdout:
App 29028 stdout:
[ 2015-01-20 05:16:52.8259 28997/7fb5c6463700 Pool2/Implementation.cpp:287 ]: Could not spawn process for application /home/ec2-user/github/mainpage: An error occured while starting up the preloader.
Error ID: 0ad6d346
Error details saved to: /tmp/passenger-error-911MCI.html
Message from application: cannot load such file -- json (LoadError)
/home/ec2-user/.gem/ruby/2.0/gems/mime-types-2.4.3/lib/mime/type.rb:4:in `require'
/home/ec2-user/.gem/ruby/2.0/gems/mime-types-2.4.3/lib/mime/type.rb:4:in `<top (required)>'
/home/ec2-user/.gem/ruby/2.0/gems/mime-types-2.4.3/lib/mime/types.rb:3:in `require'
/home/ec2-user/.gem/ruby/2.0/gems/mime-types-2.4.3/lib/mime/types.rb:3:in `<top (required)>'
/home/ec2-user/.gem/ruby/2.0/gems/mail-2.6.3/lib/mail.rb:9:in `require'
/home/ec2-user/.gem/ruby/2.0/gems/mail-2.6.3/lib/mail.rb:9:in `<module:Mail>'
/home/ec2-user/.gem/ruby/2.0/gems/mail-2.6.3/lib/mail.rb:2:in `<top (required)>'
/home/ec2-user/.gem/ruby/2.0/gems/pony-1.11/lib/pony.rb:1:in `require'
/home/ec2-user/.gem/ruby/2.0/gems/pony-1.11/lib/pony.rb:1:in `<top (required)>'
/home/ec2-user/github/mainpage/app.rb:9:in `require'
/home/ec2-user/github/mainpage/app.rb:9:in `<top (required)>'
config.ru:1:in `require'
config.ru:1:in `block in <main>'
/home/ec2-user/.gem/ruby/2.0/gems/rack-1.6.0/lib/rack/builder.rb:55:in `instance_eval'
/home/ec2-user/.gem/ruby/2.0/gems/rack-1.6.0/lib/rack/builder.rb:55:in `initialize'
config.ru:1:in `new'
config.ru:1:in `<main>'
/home/ec2-user/.gem/ruby/2.0/gems/passenger-4.0.57/helper-scripts/rack-preloader.rb:112:in `eval'
/home/ec2-user/.gem/ruby/2.0/gems/passenger-4.0.57/helper-scripts/rack-preloader.rb:112:in `preload_app'
/home/ec2-user/.gem/ruby/2.0/gems/passenger-4.0.57/helper-scripts/rack-preloader.rb:158:in `<module:App>'
/home/ec2-user/.gem/ruby/2.0/gems/passenger-4.0.57/helper-scripts/rack-preloader.rb:29:in `<module:PhusionPassenger>'
/home/ec2-user/.gem/ruby/2.0/gems/passenger-4.0.57/helper-scripts/rack-preloader.rb:28:in `<main>'
[ 2015-01-20 05:16:52.8332 28997/7fb5bffff700 agents/HelperAgent/RequestHandler.h:2306 ]: [Client 20] Cannot checkout session because a spawning error occurred. The identifier of the error is 0ad6d346. Please see earlier logs for details about the error.
Here is my config.ru file:
require './app'
run Sinatra::Application
and my gem file:
source 'https://rubygems.org'
source 'https://rubygems.org'
gem 'sinatra'
gem 'nokogiri'
gem 'pony'
gem 'passenger'
Is the error related to json? I tried installing the json gem but I still get this error. Any help would be appreciated.
Thank you.
I was able to finally get it working....
I had to manually add gem 'json' into my Gemfile and then did bundle install. After reading the errors, I figured that it had something to do with the pony gem. I am guessing that there was a dependency for pony (json gem) that was not being picked up by the bundler.

LoadError: cannot load such file -- omniauth-twitter

I'm trying to get OAuth2 working for my Sinatra app via OmniAuth but receive:
LoadError: cannot load such file -- omniauth-twitter
I'm following these examples, though they're a bit inconsistent
- http://recipes.sinatrarb.com/p/middleware/twitter_authentication_with_omniauth
app.rb
- https://github.com/intridea/omniauth/wiki/Sinatra-Example
The Sinatra Recipe tells me to put the builder within 'configure do' while the official example doesn't, but has 'use Rack::Session::Cookie'
I've scoured the net looking for good Sinatra OmniAuth examples, but it seems it favors Rails.
require "omniauth"
require "omniauth-twitter"
# OAuth2 configuration
use Rack::Session::Cookie
use OmniAuth::Builder do
provider :twitter, 'CONSUMER_KEY', 'CONSUMER_SECRET'
end
before do
# we do not want to redirect to twitter when the path info starts
# with /auth/
pass if request.path_info =~ /^\/auth\//
# /auth/twitter is captured by omniauth:
# when the path info matches /auth/twitter, omniauth will redirect to twitter
redirect to('/auth/twitter') unless current_user
end
get '/auth/twitter/callback' do
# probably you will need to create a user in the database too...
session[:uid] = env['omniauth.auth']['uid']
# this is the main endpoint to your application
redirect to('/')
end
get '/twitter' do
erb "<a href='/auth/twitter'>Sign in with Twitter</a>"
end
# Support for OAuth failure
get '/auth/failure' do
flash[:notice] = params[:message] # if using sinatra-flash or rack-flash
redirect '/'
end
The full error I get
Boot Error
Something went wrong while loading config.ru
LoadError: cannot load such file -- omniauth-twitter /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in `require'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in `block in require'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:236:in `load_dependency'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in `require'
/Users/chadsakonchick/Projects/restlessnapkin/app.rb:11:in `<top (required)>'
config.ru:1:in `require'
config.ru:1:in `block in inner_app'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
config.ru:1:in `new'
config.ru:1:in `inner_app'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/shotgun-0.9/lib/shotgun/loader.rb:112:in `eval'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/shotgun-0.9/lib/shotgun/loader.rb:112:in `inner_app'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/shotgun-0.9/lib/shotgun/loader.rb:102:in `assemble_app'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/shotgun-0.9/lib/shotgun/loader.rb:86:in `proceed_as_child'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/shotgun-0.9/lib/shotgun/loader.rb:31:in `call!'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/shotgun-0.9/lib/shotgun/loader.rb:18:in `call'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/shotgun-0.9/lib/shotgun/favicon.rb:12:in `call'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rack-1.5.2/lib/rack/builder.rb:138:in `call'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rack-1.5.2/lib/rack/handler/webrick.rb:60:in `service'
/usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
Shotgun was the culprit. Works fine when I run 'ruby app.rb' from the terminal, but with shotgun I receive the error.
It looks like the omniauth-twitter gem isn't installed. For example:
> require "blahblahblah"
LoadError: cannot load such file -- blahblahblah
You need to gem install omniauth-twitter, include it in your gemspec if you're using bundler, or whatever makes sense in you project.

Rake task not working with postgresql and sinatra-activerecord

I was trying to setup facebook app locally, using ruby and sinatra, in Kubuntu 12.04.
I have included following gems in my gemfile. I have postgresql installed in my system and I am able to run the postgresql command line and run basic commands as createdb, create table etc.
gem "sinatra"
gem "koala"
gem "json", "1.5.5"
gem "httparty"
gem "thin"
gem "rack", "1.3.10"
gem "pg"
gem "activerecord"
gem "sinatra-activerecord"
And in my app.rb file added these files,
require "rubygems"
require "sinatra"
require "sinatra/activerecord"
require "koala"
require "./config/environment" #database configuration
Running
rake -T
in my terminal gives me the following error.
rake aborted!
(<unknown>): found character that cannot start any token while scanning for the next token at line 2 column 1
/var/lib/gems/1.9.1/gems/sinatra-activerecord-1.2.2/lib/sinatra/activerecord.rb:39:in `database_file='
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:1033:in `set'
/var/lib/gems/1.9.1/gems/sinatra-activerecord-1.2.2/lib/sinatra/activerecord.rb:50:in `registered'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:1317:in `block in register'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:1315:in `each'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:1315:in `register'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:1648:in `register'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:1688:in `register'
/var/lib/gems/1.9.1/gems/sinatra-activerecord-1.2.2/lib/sinatra/activerecord.rb:76:in `<module:Sinatra>'
/var/lib/gems/1.9.1/gems/sinatra-activerecord-1.2.2/lib/sinatra/activerecord.rb:6:in `<top (required)>'
/home/nitin/facebook_app/guarded-gorge-3234/app.rb:3:in `<top (required)>'
/home/nitin/facebook_app/guarded-gorge-3234/Rakefile:1:in `<top (required)>'
/var/lib/gems/1.9.1/gems/rake-10.0.4/lib/rake/rake_module.rb:25:in `load'
/var/lib/gems/1.9.1/gems/rake-10.0.4/lib/rake/rake_module.rb:25:in `load_rakefile'
/var/lib/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:589:in `raw_load_rakefile'
/var/lib/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:89:in `block in load_rakefile'
/var/lib/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:160:in `standard_exception_handling'
/var/lib/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:88:in `load_rakefile'
/var/lib/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:72:in `block in run'
/var/lib/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:160:in `standard_exception_handling'
/var/lib/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:70:in `run'
Could any one please help. Thank you.
As #iain mentioned, this is due to a parse error in your config/database.yml file.
Check that it is valid syntax, and replace any tabs with spaces.

How to rackup a config file from outside the root directory?

When I run rackup from within my app directory, it works fine:
walkraft#li234-166:~/discourse$ rackup config.ru
Flushing redis (development mode)
/home/walkraft/.rvm/gems/ruby-1.9.3-p374/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `block in require': iconv will be deprecated in the future, use String#encode instead.
/home/walkraft/discourse/vendor/gems/message_bus/lib/message_bus.rb:130: warning: already initialized constant ENCODE_SITE_TOKEN
>> Thin web server (v1.5.0 codename Knife)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:9292, CTRL+C to stop
However, if I try running rackup from outside this directory:
walkraft#li234-166:~$ rackup discourse/config.ru
/home/walkraft/discourse/config/application.rb:7:in `require': cannot load such file -- ./lib/discourse_plugin_registry (LoadError)
from /home/walkraft/discourse/config/application.rb:7:in `<top (required)>'
from /home/walkraft/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /home/walkraft/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /home/walkraft/discourse/config/environment.rb:2:in `<top (required)>'
from /home/walkraft/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /home/walkraft/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /home/walkraft/discourse/config.ru:2:in `block in <main>'
from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.4.4/lib/rack/builder.rb:51:in `instance_eval'
from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.4.4/lib/rack/builder.rb:51:in `initialize'
from /home/walkraft/discourse/config.ru:in `new'
from /home/walkraft/discourse/config.ru:in `<main>'
from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.4.4/lib/rack/builder.rb:40:in `eval'
from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.4.4/lib/rack/builder.rb:40:in `parse_file'
from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.4.4/lib/rack/server.rb:200:in `app'
from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.4.4/lib/rack/server.rb:304:in `wrapped_app'
from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.4.4/lib/rack/server.rb:254:in `start'
from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.4.4/lib/rack/server.rb:137:in `start'
from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.4.4/bin/rackup:4:in `<top (required)>'
from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/bin/rackup:19:in `load'
from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/bin/rackup:19:in `<main>'
from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/bin/ruby_noexec_wrapper:14:in `eval'
from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/bin/ruby_noexec_wrapper:14:in `<main>'
How can I run rackup when I'm not inside of the root directory?
If you don't want to manually cd to the Discourse root folder, then why not just add a fix to the rackup config.ru file:
# Insert as first line in config.ru
Dir.chdir(File.dirname(File.expand_path(__FILE__)))
Actually, it's not a problem with rackup; it's a problem with your code.
You have
require './lib/discourse_plugin_registry'
somewhere. This is not ideal. It should rather be something like:
require File.expand_path('../../lib/discourse_plugin_registry', __FILE__)
The way you have it, it uses the current directory explicitly, and no matter what you do to rackup, until you change the current directory - it won't work.

Problem using require command

I am simply requiring a file present in the same folder as the testfile however I am getting this wierd message again and again...
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- a.rb (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from C:/dummyFirefox/test_a.rb:1:in `<top (required)>'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `load'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `block in load_spec_files'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `map'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `load_spec_files'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/command_line.rb:18:in `run'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:55:in `run_in_process'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:46:in `run'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:10:in `block in autorun'
the code that I write in my testfile test_a.rb is simply:
require 'a.rb'
and when I issue the command : rspec test_a.rb the message is the above mentioned error.
I am using ruby-1.9.2 for development.
Hope I don't miss any details.
Thanks in advance.
In Ruby 1.9.2 the current working directory is no longer part of the load path ($LOAD_PATH or $:). You have to add it manually using:
$LOAD_PATH.unshift '.'
or
$:.unshift '.'
Or you can require the file explicitly:
require './test_a.rb'

Resources