Herokuに速攻デプロイするSinatraアプリテンプレートをつくる #1

以下の記事に従って,Herokuにデプロイするための最低限のテンプレートをもったSinatraアプリケーションを作成してみようと思います.

http://qiita.com/gogotanaka/items/760d4e6ad2b19ff78ff9

以下のリポジトリにおいてます.

totzYuta/sinatra-app-template-for-heroku · GitHub

Heroku toolbeltをインストール

以下からheroku toolbeltをインストールします.

https://toolbelt.heroku.com/

これでherokuコマンドを叩けるようになります.

Gemfileの作成

まずはbundlerで必要なgemをインストールできるように

$ bundle init

Gemfileを以下のように編集

# Gemfile
source 'https://rubygems.org'

gem 'sinatra'

# Assets
gem 'haml'
gem 'sass'
gem 'coffee-script'

group :development do
  gem 'foreman'
end 

その他ファイル作成

その他のファイルも作成していきます.

$ mkdir views
$ touch app.rb Procfile views/index.haml

app.rb, Procfile, views/index.hamlを以下のように編集

# app.rb
require 'bundler/setup'
require 'sinatra'

require 'haml'
require 'sass'
require 'coffee-script'

get '/' do
  @mes = 'Hello!'
  haml :index
end
# Procfile
web: bundle exec ruby app.rb -p $PORT
# views/index.haml
:sass
  h1
    color: red

:coffee
  alert 'Hello!'

%h1= @mes

ローカルで走らせる

そしたら

$ bundle exec foreman start

herokuにpush

$ heroku create your-project
$ git add -A
$ git commit -m 'Add sample app for heroku deploying'
$ git push heroku master
$ bundle exec heroku open

そしたらデプロイできてるか確認します.

$ heroku open

これでデプロイできました〜〜〜すごい!!

ということで次回はdatabaseを使えるようにしたものも作っていこうと思いますーーー