LoginSignup
84
86

More than 5 years have passed since last update.

Heroku×bottleを使って4分33秒でWebアプリを公開する

Last updated at Posted at 2015-04-29

この記事について

下記2つを使って簡単かつ高速にWebアプリ開発をスタートできることを紹介します。
開発スタートから公開まで目標は4分33秒とします。

  • bottle: Pythonの軽量Webアプリケーションフレームワーク
  • Heroku: アプリケーション実行プラットフォーム

実際には出会って4分33秒で即公開という要求も必要も無いだろうとは思います。
ただ、4分33秒でデプロイできるというのはWeb開発のハードルがどんどん下がっていることの証左ですし、その感覚をコードとともに持っておくことは大事だと思う次第です。

この記事を読んでわかること

  • 4分33秒でHello Worldアプリを全世界に公開する方法
  • 4分33秒で公開したアプリを1分で片付ける方法

環境構築〜公開まで

0.事前準備

下記がインストールされていること。

  • 最低限

    • git
    • Python 2.7.9
    • heroku toolbelt
  • Pythonグローバル環境を汚したくない方は

    • pyenv
    • virtualenv

1.環境構築 (Git repository初期化 / Python仮想環境作成)

$ mkdir agile-dev
$ cd agile-dev
$ git init
$ pyenv virtualenv 2.7.9 agile-dev  # 任意
$ pyenv local agile-dev             # 任意

2.Hello worldアプリ開発

開発に必要なライブラリのインストールとアプリケーションの実装です。
ちなみにapp.pyの中身はRecipes — Bottle 0.13-dev documentation -のままですのでココだけコピペしています。

$ pip install bottle
app.py
import os
from bottle import route, run

@route("/")
def hello_world():
        return "Hello World!"

run(host="0.0.0.0", port=int(os.environ.get("PORT", 5000)))

3.Herokuデプロイに必要なファイルを作成

$ pip freeze > requirements.txt         # 依存ライブラリ一覧
$ echo web: python app.py > Procfile    # アプリケーションタイプとエントリポイント指定

ここまでで開発は終わりですのでコミットしておきます。

$ git add .
$ git commit -m "initial commit"

4.Herokuアプリ作成、公開

即公開します。

$ heroku apps:create agile-dev
$ git push heroku master

コマンド実行結果としてremote: Verifying deploy... done.が返って来たらデプロイ完了ですのでブラウザで確認してみましょう。公開先URLはターミナルに表示されているはずです。
remote: https://agile-dev.herokuapp.com/ deployed to Heroku

$ open https://agile-dev.herokuapp.com/

スクリーンショット 2015-04-29 16.14.22.png

無事公開されました!

リードタイム

作業開始前後にdateコマンド実行結果を比較すると… 3分54秒という結果に。
4分33秒より短く終わってしまいました。

macbook-pro:~ ohbarye$ date
2015年 4月29日 水曜日 15時40分49秒 JST
〜〜(省略)〜〜
macbook-pro:agile-dev ohbarye$ date
2015年 4月29日 水曜日 15時44分43秒 JST

後片付け

片付けて元通りにしておきます。

$ heroku apps:destroy --app agile-dev --confirm agile-dev
$ pyenv uninstall agile-dev
$ cd ../
$ rm -rf agile-dev

ちなみに片付けは1分8秒でしたのでトータルで5分強、ということになりました。

実行時結果ログ

参考までに貼り付けておきます。

開発〜デプロイ
macbook-pro:~ ohbarye$ date
2015年 4月29日 水曜日 15時40分49秒 JST
macbook-pro:~ ohbarye$ mkdir agile-dev
macbook-pro:~ ohbarye$ cd agile-dev
macbook-pro:agile-dev ohbarye$ git init
Initialized empty Git repository in /Users/ohbarye/dev/~/agile-dev/.git/
macbook-pro:agile-dev ohbarye$ pyenv virtualenv 2.7.9 agile-dev
New python executable in /Users/ohbarye/.pyenv/versions/agile-dev/bin/python2.7
Also creating executable in /Users/ohbarye/.pyenv/versions/agile-dev/bin/python
Installing setuptools, pip...done.
Ignoring indexes: https://pypi.python.org/simple/
Requirement already satisfied (use --upgrade to upgrade): setuptools in /Users/ohbarye/.pyenv/versions/agile-dev/lib/python2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): pip in /Users/ohbarye/.pyenv/versions/agile-dev/lib/python2.7/site-packages
Cleaning up...
macbook-pro:agile-dev ohbarye$ pyenv local agile-dev
macbook-pro:agile-dev ohbarye$ pip install bottle
Collecting bottle
  Using cached bottle-0.12.8.tar.gz
Installing collected packages: bottle
  Running setup.py install for bottle
Successfully installed bottle-0.12.8
macbook-pro:agile-dev ohbarye$ vim app.py
macbook-pro:agile-dev ohbarye$ pip freeze > requirements.txt
macbook-pro:agile-dev ohbarye$ echo web: python app.py > Procfile
macbook-pro:agile-dev ohbarye$ git add .
macbook-pro:agile-dev ohbarye$ git commit -m "initial commit"
[master (root-commit) f213a06] initial commit
 4 files changed, 11 insertions(+)
 create mode 100644 .python-version
 create mode 100644 Procfile
 create mode 100644 app.py
 create mode 100644 requirements.txt
macbook-pro:agile-dev ohbarye$ heroku apps:create agile-dev
Creating agile-dev... done, stack is cedar-14
https://agile-dev.herokuapp.com/ | https://git.heroku.com/agile-dev.git
Git remote heroku added
macbook-pro:agile-dev ohbarye$ git push heroku master
Counting objects: 6, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (6/6), 518 bytes | 0 bytes/s, done.
Total 6 (delta 0), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Python app detected
remote: -----> Installing runtime (python-2.7.9)
remote: -----> Installing dependencies with pip
remote:        Collecting bottle==0.12.8 (from -r requirements.txt (line 1))
remote:          Downloading bottle-0.12.8.tar.gz (69kB)
remote:        Installing collected packages: bottle
remote:          Running setup.py install for bottle
remote:            changing mode of build/scripts-2.7/bottle.py from 600 to 755
remote:            changing mode of /app/.heroku/python/bin/bottle.py to 755
remote:        Successfully installed bottle-0.12.8
remote: 
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote: 
remote: -----> Compressing... done, 35.1MB
remote: -----> Launching... done, v3
remote:        https://agile-dev.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy... done.
To https://git.heroku.com/agile-dev.git
 * [new branch]      master -> master
macbook-pro:agile-dev ohbarye$ open https://agile-dev.herokuapp.com/
macbook-pro:agile-dev ohbarye$ date
2015年 4月29日 水曜日 15時44分43秒 JST
macbook-pro:agile-dev ohbarye$ 
後片付け
macbook-pro:agile-dev ohbarye$ date
2015年 4月29日 水曜日 15時49分11秒 JST
macbook-pro:agile-dev ohbarye$ heroku apps:destroy --app agile-dev --confirm agile-dev
Destroying agile-dev (including all add-ons)... done
macbook-pro:agile-dev ohbarye$ pyenv uninstall agile-dev
pyenv: remove /Users/ohbarye/.pyenv/versions/agile-dev? y
macbook-pro:agile-dev ohbarye$ cd ../
macbook-pro:~ ohbarye$ rm -rf agile-dev/
macbook-pro:~ ohbarye$ date
2015年 4月29日 水曜日 15時50分19秒 JST
macbook-pro:~ ohbarye$ 
84
86
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
84
86