Deploy Sinatra app on Heroku

前端 未结 5 588
后悔当初
后悔当初 2020-12-23 14:58

I have simple Sinatra app.

web.rb:

require \'sinatra\'

get \'/\' do 
    \"Hello\" 
end

Gemfile:*

相关标签:
5条回答
  • 2020-12-23 15:45

    As an update, here is a slightly more minimal app that I created, and confirmed to be working as of today. The thin gem was not needed, and a Procfile was not needed to get an initial working app.

    Gemfile

    source 'https://rubygems.org'
    gem 'sinatra'
    

    config.ru

    require './app'
    
    run Sinatra::Application
    

    Note: The require line uses './app' instead of 'app'.

    app.rb

    require 'sinatra'
    
    get '/' do
      'Hello, World! Find me in app.rb'
    end
    

    If you want to use this template, you can copy it, bundle, and push the Git repo.

    $ git init
    $ git add .
    $ git commit -m "initial sinatra app"
    $ bundle
    $ git add Gemfile.lock
    $ git commit -m "bundle install"
    $ heroku create
    $ git push heroku master
    $ heroku open
    
    0 讨论(0)
  • 2020-12-23 15:51

    Here's how to create a minimal sinatra app that deploys to heroku:

    app.rb:

    require 'sinatra'
    
    get '/' do
      "hello world"
    end
    

    Gemfile:

    source 'https://rubygems.org'
    
    gem 'heroku'
    gem 'sinatra'
    gem 'thin'
    

    config.ru:

    require './app'
    run Sinatra::Application
    

    Type these commands in your command line to deploy (without the $ signs):

    $ bundle install
    $ git init
    $ git add -f app.rb Gemfile Gemfile.lock config.ru
    $ git commit -am "initial commit"
    $ heroku create <my-app-name>
    $ git push heroku master
    

    Then test your app:

    $ curl <my-app-name>.heroku.com
    

    and you should see:

    hello world
    
    0 讨论(0)
  • 2020-12-23 15:53

    I have had this problem a number of times in the past, and it was all because I didn't include my config.ru file with the requiring of [app].rb & then pushing to Heroku. Even if I added it afterwards and restarted, Heroku would never pick it up.

    • Destroy your small app on Heroku's site (http://www.heroku.com)
    • Then remove the remote from your project folder

      $ git remote rm heroku
      
    • Then recreate the app

    0 讨论(0)
  • 2020-12-23 15:54

    You need a Procfile file alongside your config.ru to tell Heroku how to run your app. Here is the content of an example Procfile:

    web: bundle exec ruby web.rb -p $PORT
    

    Heroku Ruby docs on Procfiles

    EDIT: Here's a sample config.ru from one of my sinatra/Heroku apps:

    $:.unshift File.expand_path("../", __FILE__)
    require 'rubygems'
    require 'sinatra'
    require './web'
    run Sinatra::Application
    

    You may need to require sinatra and rubygems for it to work.

    0 讨论(0)
  • 2020-12-23 16:00

    By adding the gem 'heroku' to the Gemfile, I got it working. No Procfile needed.

    0 讨论(0)
提交回复
热议问题