Search code examples
rubyherokusinatraruby-datamapper

Getting a 404 on Ruby Sinatra App - Heroku


I have a simple Sinatra App. Works fine on local machine.

Deployed it to Heroku and I keep getting a 404 on the logs.

I promise I searched the web for around 5h straight and can't figure this out.

The logs I get are:

2014-11-08T00:17:12.537150+00:00 heroku[web.1]: Starting process with command `bundle exec rackup config.ru -p 51609`
2014-11-08T00:17:12.840926+00:00 heroku[web.1]: Process exited with status 143
2014-11-08T00:17:16.370395+00:00 app[web.1]: [2014-11-08 00:17:16] INFO  WEBrick 1.3.1
2014-11-08T00:17:16.370515+00:00 app[web.1]: [2014-11-08 00:17:16] INFO  ruby 2.0.0 (2014-10-27) [x86_64-linux]
2014-11-08T00:17:16.371280+00:00 app[web.1]: [2014-11-08 00:17:16] INFO  WEBrick::HTTPServer#start: pid=2 port=51609
2014-11-08T00:17:16.745163+00:00 heroku[web.1]: State changed from starting to up
2014-11-08T00:17:35.351527+00:00 heroku[router]: at=info method=GET path="/" host=pacific-refuge-6392.herokuapp.com request_id=406b2ea5-5898-4a86-8a26-6bf71f70d3f6 fwd="200.170.116.105" dyno=web.1 connect=3ms service=22ms status=404 bytes=319
2014-11-08T00:17:35.352064+00:00 app[web.1]: 200.170.116.105 - - [08/Nov/2014 00:17:35] "GET / HTTP/1.1" 404 18 0.0030

My Gemfile is like this:

ruby '2.0.0'
source "https://rubygems.org"

gem 'sinatra-base'
gem 'sinatra-assetpack'
gem 'sinatra-asset-pipeline'
gem 'sass'
gem 'datamapper'
gem 'pony'

group :production do
    gem "pg"
    gem "dm-postgres-adapter"
end

group :development, :test do
    gem "sqlite3"
    gem "dm-sqlite-adapter"
end

Proclife:

web: bundle exec rackup config.ru -p $PORT

I tried it with ruby instead of rackup and a few other variations I found.

Config.ru

require './my_launch'
run Sinatra::Application

And finally, my app: my_launch.rb:

require 'sinatra/base'
require 'sinatra/assetpack'
require 'sass'
require 'data_mapper'
require 'dm-migrations'
require 'pony'

DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/mylaunch.db")

class Users
  include DataMapper::Resource
  property :id, Serial
  property :email, String, :required => true, :format => :email_address
  property :created_at, DateTime
end

DataMapper.finalize.auto_upgrade!


class MyLaunch < Sinatra::Base
  set :sessions, true
  register Sinatra::AssetPack

  assets do
     css :main, [
      '/css/*.css'
     ]
     css_compression :sass
  end

  get '/' do
    erb :index
  end

  post '/' do
    n = Users.new
    n.email = params[:email]
    n.created_at = Time.now
    n.save
    redirect '/obrigado'
  end

  get '/obrigado' do
    erb :obrigado
  end

  get '/admin' do
    @users = Users.all :order => :id.desc
    erb :admin
  end

  run! if app_file == $0
end

My database seems OK. To test that I fired up heroku console and created an entry on my database. Via requireing my_launch.rb and making a Users.new and later, Users.save

https://github.com/abarro/mylaunch


Solution

  • In your config file your calling run Sinatra::Application which differs from the your Sinatra inherited class where all your routes are defined. Change it to

    require './my_launch'
    run MyLaunch