Play! frameworkで作ったアプリをHerokuで動かしてみた

HerokuJavaに対応*1しました。そこで、Play! frameworkTutorialで作るブログエンジン、「Yabe」をHerokuで動かしてみました。

Herokuにアプリを作成*2

Herokuにアプリを作成しますが、この時に「--stack」オプションを追加して「cedar」スタックを指定してください。アプリの名前を「yabe-nkmrshn」に変更していますが、これは必須ではありません。

$ heroku create --stack cedar
$ heroku apps:rename yabe-nkmrshn

次にデータベースを追加。とりあえず無料の共有データベースを指定しました。

$ heroku heroku addons:add shared-database

そして、conf/application.confで設定するPostgreSQLのURLを取得します。

$ heroku config
SHARED_DATABASE_URL => postgres://<URL>

Yet Another Blog Engine(Yabe)の準備

HerokuのデータベースはPostgreSQLなので「User」というテーブルが作れません。そこで、Userをすべて「Account」に書き換えました。また、Tutorialの第10章「Completing the application tests」を終えた後、私はフレームワークIDを「prod」に設定してあります。

conf/application.confのデータベースの設定にPostgreSQLを追加します。

%prod.db=postgres://<URL>

また、DDLはあえて「update」を指定しました。これは、app/Bootstrap.javaでテーブルの作成と初期値*3の挿入をおこなわせる為です。本当はnoneにして、migrationモジュールを使って管理するのがいいんだと思いますが、Herokuのコマンドで「heroku run play db:migrate」などと実行できるのかなぁ...

%prod.jpa.ddl=update

最後に「Procfile」という名前のテキストファイルをアプリケーションルートに作成します。最後の「--http.port=$PORT」は必須です。

web: play run --%prod --http.port=$PORT

Herokuにデプロイ

gitコマンドでHerokuにpushしてデプロイします。

$ git push heroku master
Counting objects: 7, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 337 bytes, done.
Total 4 (delta 3), reused 0 (delta 0)

-----> Heroku receiving push
-----> Play! app detected
-----> Building Play! application...
       ~        _            _ 
       ~  _ __ | | __ _ _  _| |
       ~ | '_ \| |/ _' | || |_|
       ~ |  __/|_|\____|\__ (_)
       ~ |_|            |__/   
       ~
       ~ play! 1.2.3, http://www.playframework.org
       ~
       1.2.3
       Play! application root found at ./
       Resolving dependencies: .play/play dependencies ./ --forceCopy --sync --silent -Duser.home=/tmp/build_35cm0iosbsdu2 2>&1
       ~ Resolving dependencies using /tmp/build_35cm0iosbsdu2/conf/dependencies.yml,
       ~
       ~
       ~ Installing resolved dependencies,
       ~
       ~        modules/crud -> /tmp/build_35cm0iosbsdu2/.play/modules/crud
       ~        modules/secure -> /tmp/build_35cm0iosbsdu2/.play/modules/secure
       ~
       ~ Done!
       ~
       Precompiling: .play/play precompile ./ --silent 2>&1
       Listening for transport dt_socket at address: 8000
       12:27:59,957 INFO  ~ Starting /tmp/build_35cm0iosbsdu2
       12:27:59,962 INFO  ~ Module secure is available (/tmp/build_35cm0iosbsdu2/modules/secure)
       12:27:59,962 INFO  ~ Module crud is available (/tmp/build_35cm0iosbsdu2/modules/crud)
       12:28:00,725 INFO  ~ Precompiling ...
       12:28:08,140 INFO  ~ Done.
       
-----> Discovering process types
       Procfile declares types -> web
-----> Compiled slug size is 26.6MB
-----> Launching... done, v17
       http://yabe-nkmrshn.herokuapp.com deployed to Heroku

To git@heroku.com:yabe-nkmrshn.git
   22989f1..6e542c5  master -> master

余談

Herokuすげー。

*1:Heroku for Java

*2:既にHerokuのアカウントやHerokuのGem、そしてgitコマンドなどが使える状態にあると想定。

*3:Tutorialのconf/initial-data.yml