Code Monkey home page Code Monkey logo

sinatra-demo-heroku's Introduction

How to Host to Heroku

Step 1 - setting up your environment

Step 2 - create a Heroku app

  • Log into your Heroku account
  • Click 'New' --> 'Create new app'
  • Name your application

Step 3 - Choose how you want to deploy your application

  • Choose Heroku git or Github

Step 4 - Change your Sinatra production database to Postgresql

  • Group your Gemfile into development and production
  • Move sqlite3 into the development group (along with other gems that are just used for development)
group :development do
  gem 'sqlite3', '~> 1.3.6'
  gem 'tux'
  gem 'pry'
  gem 'shotgun'
end
  • Add postgresql to the production group
group :production do
  gem 'pg', '~> 0.20'
end
  • delete the gemfile.lock and run bundle install

Configure the Database

Create a config/database.yml file:

# config/database.yml
development:
  adapter: sqlite3
  encoding: unicode
  database: db/development.sqlite3
  pool: 5
production:
  adapter: postgresql
  encoding: unicode
  pool: 5

and we'll need to update our config/environment.rb file to use this file to establish a connection to our DB. To do that, replace these lines

# config/environment.rb
# ...

ActiveRecord::Base.establish_connection(
  :adapter => "sqlite3",
  :database => "db/#{ENV['SINATRA_ENV']}.sqlite"
)

with

set :database_file, "./database.yml"

Create a Procfile

  • Add the gem 'foreman' to your gemfile
  • Create a Procfile with the following code:
web: bundle exec thin start -p $PORT
release: bundle exec rake db:migrate
  • This tells Heroku how to start the server and to run migrations before releasing a build

Add Config Vars

  • Go to your app settings on Heroku and set the following variables:
    • SINATRA_ENV = production
    • SESSION_SECRET = 335673ae2a1c5cccec147456
  • Ensure your local .env variables only load in development so they don't override the Heroku variables:
    • In your config/environment.rb:
    Dotenv.load if ENV['SINATRA_ENV'] == "development"
    

Commit all changes and push code to github

  • git add .
  • git commit -m "configure app for heroku"
  • git push origin master
  • Go to heroku and deploy your master branch

Troubleshooting

When I updated my ruby version I also installed bundler v2. Heroku wants an older version of bundler (1.15.2) when I do the deployment, so I had to install it as well and then figure out how to use it to generate the Gemfile.lock.

gem install bundler -v 1.15.2
bundle _1.15.2_ install

Resources

sinatra-demo-heroku's People

Contributors

hansenjl avatar

Watchers

James Cloos avatar  avatar

Forkers

mrsdo rlj0713

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.