LoginSignup
7

More than 5 years have passed since last update.

Procfileは.confで短くできるが、ポート指定だけは必要っぽい

Posted at

 タイトルのまんまです。

Procfile長過ぎ問題

 Heroku + play + postgresql するためにはProcfileを使う必要があると言われています (環境変数で頑張れば動くような気もしますが検証はしません)。

Then create a new file in your project’s root directory named Procfile (with a capital “P”) that contains the following (substituting the myapp with your project’s name):

web: target/universal/stage/bin/myapp -Dhttp.port=${PORT} -DapplyEvolutions.default=true -Ddb.default.driver=org.postgresql.Driver -Ddb.default.url=${DATABASE_URL}

 しかし、Procfileを使うにしても、-Dapply... とか -Ddb というような文字列はあまり見た目がよろしくありません。
 ドキュメントにもある方法を使って、別のファイルに書いておくのがよさそうです。

 まずテスト用の設定を書いてから、herokuでのみ適用したい設定を別ファイルに書いてみます。

application.conf
# 用意したpostgresqlの設定を書きまくる
# ただし「設定が面倒くさいからローカルはh2で済ませる」等も可能
db.default.driver = org.postgresql.Driver
db.default.url = "jdbc:postgresql://localhost:5432/hogedb"
db.default.user = ${?DATABASE_USER}
db.default.password = ${?DATABASE_PASSWORD}
slick.default = "models.*"

# ほかにもいろいろ
heroku.conf
# インクルードしないと上書きされず単に無視される?
include "application.conf"

# 以下、Heroku + postgresqlのための設定

# なるべくお金を払いたくないのでpostgresqlにしましょう
db.default.driver = org.postgresql.Driver
# 必要な情報は環境変数なので、毎回この設定でOK。地味に楽
db.default.url = ${DATABASE_URL}
# 「DBエボリューションでオラワクワクしてきたぞボタン」が出ないよう設定
applyEvolutions.default=true

 ここまで用意することでProcfileを短くできます。

web: target/universal/stage/bin/hogesoft -Dhttp.port=$PORT -Dconfig.resource=heroku.conf

 以上です。

なおポート指定を外すと詰む

 一見したところ -Dhttp.port=$PORT もheroku.confに書けそうな気がします。ところがこれをProcfileから外してみると、エラーが出て何も動かなくなります。
 ここがよく分からず、数時間ほど延々とpushして試行錯誤する羽目になりました。

 ポートが分からないと繋がらない(?)と考えれば、至極当然という気もしますが… 詳しいことはよく分かりませんでした。
 Procfile関連の仕様についてはこちら参照。

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
7