DRYな備忘録

Don't Repeat Yourself.

HerokuでGo言語のRevelを動かしたい【heroku】【revel】【golang】

ゴール

記録

とりあえず新しいRevelプロジェクトつくる

% revel new myapp
% cd $GOPATH/src/myapp

gitリポジトリにする

% git status
fatal: Not a git repository (or any of the parent directories): .git
% git init
# なにかmodifyしたかったらここで何かして...
% git add .
% git commit -m "Initial commit だよーん"

RevelをHerokuで動かす用のBuildPackを指定しつつ、herokuインスタンスを確保する(ちなみにこれrobfig*1謹製)

% heroku create -b https://github.com/robfig/heroku-buildpack-go-revel.git
Creating limitless-depths-8649... done, stack is cedar
BUILDPACK_URL=https://github.com/robfig/heroku-buildpack-go-revel.git
http://limitless-depths-8649.herokuapp.com/ | git@heroku.com:limitless-depths-8649.git
Git remote heroku added

git remoteに確保したインスタンスherokuの名前でaddされていることを確認

% git remote -v
heroku  git@heroku.com:limitless-depths-8649.git (fetch)
heroku  git@heroku.com:limitless-depths-8649.git (push)

このインスタンスにコードをデプロイする

% git push heroku master
Initializing repository, done.
Counting objects: 41, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (32/32), done.
Writing objects: 100% (41/41), 79.76 KiB | 0 bytes/s, done.
Total 41 (delta 4), reused 0 (delta 0)

-----> Fetching custom git buildpack... done
Initial commit だよーん
-----> Revel app detected
-----> Installing Go 1.1.2... done
       Installing Virtualenv... done
       Installing Mercurial... done
       Installing Bazaar... done
-----> Running: go get -tags heroku ./...
-----> Discovering process types
       Procfile declares types -> (none)
       Default types for Revel -> web

-----> Compressing... done, 48.1MB
-----> Launching... done, v5
       http://limitless-depths-8649.herokuapp.com/ deployed to Heroku

To git@heroku.com:limitless-depths-8649.git
 * [new branch]      master -> master

と、こうなる

f:id:otiai10:20140710001026j:plain

It works!

番外: サブドメインをかっちょよくしたい

どうせなら、自動で割り当てられるダサい名前じゃなくて、tainaka.herokuapp.comとかにしたいじゃないですかー

heroku.comのダッシュボードのアプリ設定から、Nameを変更する

f:id:otiai10:20140710001348j:plain

確保したインスタンスの名前が変わっているはずなので、git remoteで登録されているherokuの向き先を変える必要がある

% git remote -v
heroku  git@heroku.com:limitless-depths-8649.git (fetch)
heroku  git@heroku.com:limitless-depths-8649.git (push)
% git remote rm heroku
% heroku git:remote -a tainaka
Git remote heroku added
% git remote -v
heroku  git@heroku.com:tainaka.git (fetch)
heroku  git@heroku.com:tainaka.git (push)

あらためてここに向けてデプロイする

% git push heroku master

そうすっと、

f:id:otiai10:20140710001733j:plain

tainaka.herokuapp.comでアプリが動いている

Thanks to id:nekova

いろいろつまずいたポイントをだらだらと

% heroku create
  • loginを求められる. PW忘れた. 泣く
  • PW更新した
% heroku create
!    You\'ve reached the limit of 5 apps for unverified accounts.
!    Add a credit card to verify your account.
  • 5つまでと怒られる. 泣く
  • 怒りの全削除
% heroku create -b https://github.com/robfig/heroku-buildpack-go-revel.git
http://immense-island-8507.herokuapp.com/ | git@heroku.com:immense-island-8507.git
Git remote heroku added
  • いけた
% git push heroku master
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
  • Permissionが無いと怒られる.
  • heroku.comでssh keyを登録する
git push heroku master

 !  No such app as immense-island-8507.

fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
  • そんなアプリ無ぇと怒られる
  • heroku.comでつくったやつ削除
  • git remote rm herokuたぶんこれが前のcreateしたときのままだったんだと思う
% git push heroku master
  • いけた

雑感

食わず嫌いで苦手意識のあったherokuだけど、理解してしまえば単なるgitリポジトリのhostとアプリのビルドだけっぽいし、今後活用していきたい。自鯖でけっこうメモリ食ってるアプリケーションを引っ越ししたいんだけど、サーバスペックとかどうやって確認したらいいんだろう

DRYな備忘録

クラウド開発徹底攻略 (WEB+DB PRESS plus)

クラウド開発徹底攻略 (WEB+DB PRESS plus)

プログラミング言語Go (ADDISON-WESLEY PROFESSIONAL COMPUTING SERIES)

プログラミング言語Go (ADDISON-WESLEY PROFESSIONAL COMPUTING SERIES)

*1:revel frameworkの作者. さいしょはrobfigのリポジトリだったけど、半年前くらいにrevel/revelになった. そんときgo install revelの方法も変わったっぽいので注意