route giving 404 in sinatra - ruby

I have just started working on sinatra, now my application is not able to navigate upto route and get data from db, I am always getting 404. This is what I am doing.
File Structure
app
|---->helpers
|----->models
|----->public
|----->routes
| |------->candidate.rb
| |------->init.rb
|----->app.rb
|----->config.ru
config.ru
require './app'
run MyApp
app.rb
require 'json'
require 'sinatra'
require 'data_mapper'
require 'dm-migrations'
require 'sinatra/cross_origin'
require 'logger'
require './models/init'
require './helpers/init'
require './routes/init'
class MyApp < Sinatra::Base
configure :development do
enable :cross_origin
DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(
:default,
'mysql://root:#localhost/hackerrank'
)
end
get '/' do
File.read(File.join('public', 'index.html'))
end
DataMapper.finalize
end
Command
bundle exec rackup -p 4567 config.ru
api in candidate.rb are correct but if i try to access the resource as http://localhost:4567/#/recruiter I am getting 404 .
I am not able to figure out what is wrong I am doing here.

You're getting a 404, since GET '/' only works for the root page and you're trying to load /recruiter. You can change it to GET '/:pagename' and that would fix the 404 problem instantly.

Your codebase looks fine. When you're running your Sinatra app from the terminal, each request shows a log line such as
127.0.0.1 - - [02/Jan/2016 00:43:53] "GET / HTTP/1.1" 200 - 0.0033
Ensure that the HTTP verb and or endpoint/route you see is the one you are expecting (perhaps you are making a POST request when accessing the resource?)

Related

Define sinatra request routes in included files

I am using Sinatra and I would like to have my project structed in a way that keeps all requests for a specific action in separated files.
The problem I'm running into is that the routes aren't registered with sinatra, and it always 404s and runs my not_found handler, even though I've included a file with the route.
Here's an example of what I'm trying to achieve; Rackup would start the Info app which requires user and post. Info only contains a error and not found handler, and the related routes go in the corresponding required file.
config.ru:
require 'rubygems'
require 'bundler'
Bundler.require
require 'rack'
require './info.rb'
run Info
info.rb:
require 'rubygems'
require 'bundler'
require 'sinatra'
class Info < Sinatra::Base
require './user.rb'
require './post.rb'
# 500 handler
error StandardError do
status 500
content_type :json
return '{"error": "Internal server error", "code": 500}'
end
not_found do
status 404
content_type :json
return '{"error": "Page not found", "code": 404}'
end
end
And user.rb (post.rb would look the same):
require 'rubygems'
require 'bundler'
require 'sinatra'
get '/1/user/:userid' do
# request stuff
end
require doesn’t work the way you seem to think it does. When you call require './user.rb', even though you do it inside the body of class Info < Sinatra::Base, its contents are not loaded as if they were inside that class. Instead they are parsed at the top level, and the routes are added to the default Sinatra::Application and not your application class.
You will have to have your user and post routes defined inside the same class body:
#info.rb
require 'sinatra/base' # Require 'sinatra/base' if you are using modular style.
class Info < Sinatra::Base
# It's a bit unusual to have require inside a class, but not
# wrong as such, and you might want to have the class defined
# before loading the other files.
require_relative 'user.rb' # require_relative is probably safer here.
require_relative 'post.rb'
# ... error handlers etc.
end
#user.rb
require 'sinatra/base'
# The routes need to be in the same class.
class Info < Sinatra::Base
get '/1/user/:userid' do
# request stuff
end
end

Sinatra Routing - Separate Files

I'm going through a recently released book on Sinatra that demonstrates this way of setting up routes in different files:
# app.rb
require "sinatra"
require "slim"
class Todo < Sinatra::Base
# ...
Dir[File.join(File.dirname(__FILE__), "lib", "*.rb")].each { |lib| require lib }
end
# lib/routes.rb
get "/test" do
"The application is running"
end
# config.ru
require "sinatra"
require "bundler/setup"
Bundler.require
ENV["RACK_ENV"] = "development"
require File.join(File.dirname(__FILE__), "app.rb")
Todo.start!
However, it fails to find the route at http://localhost:4567/test. It would make sense to me that this should work when I run ruby config.ru or bundle exec rackup -p 4567. But coming from Rails development where all this configuration is built-in, I don't have a complete understanding of how everything gets wired together. The server is running on that port and I get the Sinatra doesn't know this ditty 404 page. If I reopen the class as suggested by this SO answer, the /test route is found.
# lib/routes.rb
class Todo < Sinatra::Base
get "/test" do
"The application is running"
end
end
Is there something I'm missing about this suggested way to include routes without reopening the class?
Try ruby app.rb, it should work.
You'll need to restart the webserver to load routes that were added while it was running. Routes are loaded into memory when app.rb is invoked and Sinatra is launched. The route itself looks fine and it appears routes.rb is being imported successfully via Dir[File.join(File.dirname(__FILE__), "lib", "*.rb")].each { |lib| require lib }.
If you're running the server directly through terminal Ctrl+X, Ctrl+C should shut it down, then restart it via rackup config.ru* or ruby app.rb. You may confirm the route is recognized by making a get request through your browser to: http://127.0.0.1:4567/test.
For the rackup config.ru command to work, you can change config.ru to something like:
# config.ru
require './app'
run Sinatra::Application
This is just a deployment convenience.
Edit: #shaun, because Todo extends Sinatra::Base it's fine to use run Todo in your case.
The book suggested Todo.start! to run the application from the config.ru file, but the Sinatra documentation example uses run Sinatra::Application. So I just changed the line from Todo.start! to
run Todo
That seems to work, but I'll have to look into the consequences.

not_found is not working in Sinatra Ruby

i have two urls i want to redirect users to one of those urls. i want users to get redirected if the first url will not work. Following is my code. Thank you for your support. The following code is not redirecting to the 2nd url when my first url is not working. Can you please tell what am i missing here.
require 'rubygems'
require 'sinatra'
require 'json'
require 'open-uri'
set :environment, :production
get '/jde' do
redirect "url1"
end
not_found do
redirect "url2"
end

Ruby, Sinatra , omniauth-github Authentication Failure Callback Error

ok it has been over 5 hours and I am still getting no where. What I am trying to do is setup omniauth-gihub gem in one of my Ruby-Sinatra based applications. Following is What I have done yet.
Added the Gems to the Gemfile (& Ran bundler update command ofcourse):
source 'https://rubygems.org'
gem 'sinatra'
gem 'haml'
gem 'shotgun'
gem 'omniauth', :git => 'git://github.com/intridea/omniauth.git'
gem 'omniauth-github', :git => 'git://github.com/intridea/omniauth-github.git'
Under my app.rb file I have the following code:
#imports
require 'rubygems'
require 'bundler'
require 'sinatra'
require 'omniauth'
require 'omniauth-github'
require 'haml'
require './helpers.rb'
#Configure OmniAuth
use OmniAuth::Builder do
provider :github, ENV['api_key'], ENV['secret'], # Removing the key and secret for security reasons
scope: "user,repo,gist"
end
#Application Settings
set :sessions, true
set :views, 'templates'
#Get Method for Application Root
get '/' do
haml :index
end
#Get/Post Methods For Authentication
%w(get post).each do |method|
send(method, "/auth/:provider/callback") do
env['omniauth.auth']
end
end
The Github application's settings are as follows:
URL = http://127.0.0.1:4567
Callback URL = http://127.0.0.1:4567/auth/github/callback
Now whenever I visit 127.0.0.1:4567/auth/github/callback I get the following error:
I, [2012-07-26T07:05:23.540462 #30458] INFO -- omniauth: (github) Callback phase initiated.
E, [2012-07-26T07:05:23.540700 #30458] ERROR -- omniauth: (github) Authentication failure! invalid_credentials: OmniAuth::Strategies::OAuth2::CallbackError, OmniAuth::Strategies::OAuth2::CallbackError
localhost - - [26/Jul/2012:07:05:23 IST] "GET /auth/github/callback HTTP/1.1" 302 9
- -> /auth/github/callback
localhost - - [26/Jul/2012:07:05:23 IST] "GET /auth/failure?message=invalid_credentials&strategy=github HTTP/1.1" 404 448
- -> /auth/failure?message=invalid_credentials&strategy=github
localhost - - [26/Jul/2012:07:05:23 IST] "GET /favicon.ico HTTP/1.1" 404 447
- -> /favicon.ico
it seems it's not even trying to connect to github, I thought I was already logged in so I logged out of github and try visiting 127.0.0.4567/auth/github/callback again and yes it is not even connecting or sending any information to github.
I have check my api key and the secret and they are correct. I can't really figure out what am I missing and am really tired. Any help or suggestion will be much appreciated.
EDIT::
Okay I found that the code raising the error is as follows in oauth2.rb
def callback_phase
if request.params['error'] || request.params['error_reason']
raise CallbackError.new(request.params['error'], request.params['error_description'] || request.params['error_reason'], request.params['error_uri'])
end
if request.params['state'].to_s.empty? || request.params['state'] != session.delete('omniauth.state')
raise CallbackError.new(nil, :csrf_detected)
end
I feel it is something to do with CSRF.
This might be of interest:
https://github.com/intridea/omniauth-github/issues/12
I was getting the same error as you, and adding scope: 'user' fixed it for me.
I see you're already using scope, but the link might put you on the right track.
Had the same issue - downgrading omniauth-facebook to 1.4.0 fixed it for me. https://github.com/mkdynamic/omniauth-facebook/issues/73

deploy a sinatra app with passenger gives only 404, page not founds. Yet a simple rack app works

I have correctly (or prbably not) installed passenger on apache 2. Rack works, but sinatra keeps giving 404's.
Here is what works:
config.ru:
#app = proc do |env|
return [200, { "Content-Type" => "text/html" }, "hello <b>world</b>"]
end
run app
Here is what works too:
Running the app.rb (see below) with ruby app.rb and then looking at localhost:4567/about and /
restarting the app, gives me a correct hello world. w00t.
But then there is the sinatra entering the building:
config.ru
require 'rubygems'
require 'sinatra'
root_dir = File.dirname(__FILE__)
set :environment, ENV['RACK_ENV'].to_sym
set :root, root_dir
set :app_file, File.join(root_dir, 'app.rb')
disable :run
run Sinatra::Application
and an app.rb
require 'rubygems'
require 'sinatra'
get '/' do
"Hallo wereld!"
end
get '/about' do
"Hello world, it's #{Time.now} at the server!"
end
This keeps giving 404s.
/var/logs/apache2/error.log lists these correctly as "404" with something that worries me:
83.XXXXXXXXX - - [30/May/2010 16:06:52] "GET /about " 404 18 0.0007
83.XXXXXXXXX - - [30/May/2010 16:06:56] "GET / " 404 18 0.0007
The thing that worried me, is the space after the / and the /about. Would apache or sinatra go looking for /[space], like /%20?
If anyone knows what this problem relates to, maybe a known bug (that I could not find) or a known gotcha?
Maybe I am just being stupid and getting "it all wrong?"
Otherwise any hints on where to get, read or log more developers data on a running rack, sinatra or passenger app would be helpfull too: to see what sinatra is looking for, for example.
Some other information:
Running ubuntu 9.04, apache2-mm-prefork (deb), mod_php5, ruby 1.8.7, passenger 2.2.11, sinatra 1.0
You are not loading the routes in app.rb. To do this, replace require 'sinatra' with require File.join(File.dirname(__FILE__), 'app.rb') in config.ru.
root_dir = File.dirname(__FILE__)
app_file = File.join(root_dir, 'app.rb')
require app_file
set :environment, ENV['RACK_ENV'].to_sym
set :root, root_dir
set :app_file, app_file
disable :run
run Sinatra::Application
set :app_file won't load them for you.
Just substitute the require sinatra with a require 'app' and you're set to go.

Resources