Play framework 2.0 RC2 を heroku で試してみた



scalaで利用できるWEBフレームワークで、なにかないかと思い探してみた
丁度、Play frameworkの新しいものがリリースされそうだったので試してみた
 

試してみようとおもった動機

  • scalaが利用できる
  • SBT(Simple Build Tools)ベースで開発ができる
  • Railsのようなプロジェクト構成

 

環境

 

Play framework 2.0 RC2のインストール

事前に、Java SDKScala の実行環境を作成しておく
 

play-2.0-RC2.zipをダウンロードする

play-2.0-RC2.zipを取得し、展開する

wget http://download.playframework.org/releases/play-2.0-RC2.zip
unzip play-2.0-RC2.zip

 
.bash_profileにplay-2.0のホームを設定する

vi .bash_profile
export PLAY_HOME=/home/user/play-2.0-RC2
export PATH=$PATH:$PLAY_HOME
source .bash_profile

 
Playの環境ができているかコンソールから確認をしてみる*1

$ play help

       _            _
 _ __ | | __ _ _  _| |
'_ \ / _' _
__/ _ \____ \__ (_)
_ __/
play! 2.0-RC2, http://www.playframework.org Welcome to Play 2.0! These commands are available:
                                                        • -
license Display licensing informations. new Create a new Play application in the current directory. You can also browse the complete documentation at http://www.playframework.org.

 

試しにサンプルプロジェクトを作成してみる

「play new」コマンドでサンプルプロジェクトを作成してみた

$ play new play20_rc2sample
Getting net.java.dev.jna jna 3.2.3 ...
retrieving :: org.scala-tools.sbt#boot-app
confs: [default] 1 artifacts copied, 0 already retrieved (838kB/10ms) Getting Scala 2.9.1 (for console)...
retrieving :: org.scala-tools.sbt#boot-scala
confs: [default] 4 artifacts copied, 0 already retrieved (19939kB/34ms) Getting play console_2.9.1 2.0-RC2 ...
retrieving :: org.scala-tools.sbt#boot-app
confs: [default] 5 artifacts copied, 0 already retrieved (5763kB/13ms) _ _ _ __ | | __ _ _ _| |
'_ \ / _' _
__/ _ \____ \__ (_)
_ __/
play! 2.0-RC2, http://www.playframework.org The new application will be created in /home/user/play20_rc2sample What is the application name? > play20_rc2sample Which template do you want to use for this new application? 1 - Create a simple Scala application 2 - Create a simple Java application 3 - Create an empty project > 1 OK, application play20_rc2sample is created. Have fun!

 
サンプルプロジェクトのアプリケーションを実行してみる
サンプルプロジェクトのディレクトリ直下で、sbtを「play」コマンドで実行する
その後、「run」コマンドでアプリケーションを起動する

$cd play20_rc2sample
$play
Getting org.scala-tools.sbt sbt_2.9.1 0.11.2 ...
retrieving :: org.scala-tools.sbt#boot-app
confs: [default] 37 artifacts copied, 0 already retrieved (7324kB/54ms) [info] Loading project definition from /home/user/play20_rc2sample/project [info] Set current project to play20_rc2sample (in build file:/home/user/play20_rc2sample/) _ _ _ __ | | __ _ _ _| |
'_ \ / _' _
__/ _ \____ \__ (_)
_ __/
play! 2.0-RC2, http://www.playframework.org > Type "help play" or "license" for more information. > Type "exit" or use Ctrl+D to leave this console. $ run [info] Updating {file:/home/user/play20_rc2sample/}play20_rc2sample... [info] Done updating. (Running the application from SBT, auto-reloading is enabled) --- [info] play - Listening for HTTP on port 9000... (Server started, use Ctrl+D to stop and go back to the console...) [info] Compiling 5 Scala sources and 1 Java source to /home/user/play20_rc2sample/target/scala-2.9.1/classes... [info] play - Application started (Dev)

 
http://ip address:9000/ などにアクセスしてみると「Welcome to Play 2.0」というトップページが確認できる
 

herokuで実行してみる

herokuに作成したサンプルアプリケーションをデプロイし、実行してみます
 

環境
  • gemかなにかでherokuコマンドが利用できるようにしておく
$ heroku version
heroku-gem/2.20.1

 

herokuにデプロイする

Procfileファイルを作成する
詳しくは、About the Celadon Cedar Stackをどうぞ

cd /home/user/play20_rc2sample
vi Procfile
web: target/start -Dhttp.port=${PORT} ${JAVA_OPTS} -DapplyEvolutions.default=true

 
project/plugins.sbtに「xsbt-start-script-plugin」というプラグインを追加する 

cd /home/user/play20_rc2sample
vi ./project/plugins.sbt
addSbtPlugin("com.typesafe.startscript" % "xsbt-start-script-plugin" % "0.5.0")

 
herokuに登録するために、ローカルのgitに登録を行う

git init
Initialized empty Git repository in /home/user/play20_rc2sample/.git/
git add .
git commit -m "init"

 
Cedar Stack でアプリケーションを作成する
その後、プロセスを起動する

heroku create --stack cedar
heroku scale web=1

 
gitからherokuへデプロイを行う

git push heroku master

          • > Discovering process types
Procfile declares types -> web
          • > Compiled slug size is 79.4MB
          • > Launching... done, v5
http://xxxx-xxxx-xxx.herokuapp.com deployed to Heroku

 
herokuコマンドで動作確認をする
「heroku ps」で該当プロセスが存在しているか確認する
「heroku logs」で実行アプリケーションにエラーがないか確認する

$ heroku ps
Process  State       Command
------- ---------- ------------------------------------
web.1 up for 18s target/start -Dhttp.port=${PORT} $.. $ heroku logs 2012-02-xxTxx:xx:02+00:00 heroku[router]: GET xxxx-xxxx-xxx.herokuapp.com/ dyno=web.1 queue=0 wait=0ms service=236ms status=200 bytes=498 2012-02-xxTxx:xx:03+00:00 heroku[router]: GET xxxx-xxxx-xxx.herokuapp.com/assets/stylesheets/main.css dyno=web.1 queue=0 wait=0ms service=133ms status=200 bytes=0 2012-02-xxTxx:xx:03+00:00 heroku[router]: GET xxxx-xxxx-xxx.herokuapp.com/assets/javascripts/jquery-1.7.1.min.js dyno=web.1 queue=0 wait=0ms service=34ms status=200 bytes=93868

 
http://xxxx-xxxx-xxx.herokuapp.com/にアクセスし、サンプルページがみえるか確認をする。
 

課題

herokuで実行した際に、「./app/views/main.scala.html」から読み込まれている「./app/views/index.scala.html」が
うまく読み込まれていないようだった。時間がある時に調整してみる
 

*1: playのロゴが崩れていますが気にしないで下さい。