elixir, phoenix導入メモ
elixirを試したいので入れた。 大まかな流れは Installation · Phoenix の通り。
ただしMacだとbrewで簡単に入るけど、今後のためにexenv
で入れてみた。
Rubyでいうところのrbenv
。
erlang
brew install erlang
erlangにもerlenv
というのがあるらしいけど、ビルドをしてくれるコマンドがまだないとのことだったので使わなかった。
あとで本格的に使うときに考えよう。
exenv -> elixir
exenv自体はbrewで入れた。
exenv install
をするにはelixir-build
も必要。rbenv
っぽい。
brew install exenv brew install elixir-build # PATHの設定とかまでは自動でやってくれなかった。 echo 'export PATH="$HOME/.exenv/bin:$PATH"' >> ~/.bash_profile echo 'eval "$(exenv init -)"' >> ~/.bash_profile exec $SHELL
elixirを入れる。
# インストールできるバージョンを確認。 exenv install -l # インストール exenv install 1.0.5 # バージョン確認 elixir -v Elixir 1.0.5 mix -v Mix 1.0.5
node, npm
phoenixではアセットのコンパイルにnpm(とbrunch.io)を使うらしいのでnodeも入れておく。 最近v4.0.0が出たらしいのでついでに改めて入れてみる。 phoenixがちゃんと対応しているかは知らず。
nodebrew install v4.0.0 nodebrew alias default v4.0.0 nodebrew use v4.0.0
しかしアセットをnodeで管理するのは時代を感じるし理にかなってる。 Railsも本家でこういう対応してくれないだろうか。。。
phoenix
phoenixのインストールはちょっと見慣れない感じで以下のようにやるらしい。
mix archive.install https://github.com/phoenixframework/phoenix/releases/download/v1.0.2/phoenix_new-1.0.2.ez
mixというのはelixirに付いてくるビルドツールでnpmのようなもの??
Web上のアーカイブ(中身はZIPらしい)を持ってきて$HOME/.mix/archives/
に配置してくれるらしい。
以上で準備できた。簡単。
Hello, Phoenix
とりあえず動作確認まで。
mix phoenix.new hello_phoenix * creating hello_phoenix/config/config.exs * creating hello_phoenix/config/dev.exs * creating hello_phoenix/config/prod.exs * creating hello_phoenix/config/prod.secret.exs * creating hello_phoenix/config/test.exs * creating hello_phoenix/lib/hello_phoenix.ex * creating hello_phoenix/lib/hello_phoenix/endpoint.ex * creating hello_phoenix/test/controllers/page_controller_test.exs * creating hello_phoenix/test/views/error_view_test.exs * creating hello_phoenix/test/views/page_view_test.exs * creating hello_phoenix/test/views/layout_view_test.exs * creating hello_phoenix/test/support/conn_case.ex * creating hello_phoenix/test/support/channel_case.ex * creating hello_phoenix/test/test_helper.exs * creating hello_phoenix/web/channels/user_socket.ex * creating hello_phoenix/web/controllers/page_controller.ex * creating hello_phoenix/web/templates/layout/app.html.eex * creating hello_phoenix/web/templates/page/index.html.eex * creating hello_phoenix/web/views/error_view.ex * creating hello_phoenix/web/views/layout_view.ex * creating hello_phoenix/web/views/page_view.ex * creating hello_phoenix/web/router.ex * creating hello_phoenix/web/web.ex * creating hello_phoenix/mix.exs * creating hello_phoenix/README.md * creating hello_phoenix/lib/hello_phoenix/repo.ex * creating hello_phoenix/test/support/model_case.ex * creating hello_phoenix/priv/repo/seeds.exs * creating hello_phoenix/.gitignore * creating hello_phoenix/brunch-config.js * creating hello_phoenix/package.json * creating hello_phoenix/web/static/css/app.css * creating hello_phoenix/web/static/js/app.js * creating hello_phoenix/web/static/js/socket.js * creating hello_phoenix/web/static/assets/robots.txt * creating hello_phoenix/web/static/assets/images/phoenix.png * creating hello_phoenix/web/static/assets/favicon.ico Fetch and install dependencies? [Yn] * running npm install && node node_modules/brunch/bin/brunch build We are all set! Run your Phoenix application: $ cd hello_phoenix $ mix deps.get $ mix ecto.create $ mix phoenix.server You can also run your app inside IEx (Interactive Elixir) as: $ iex -S mix phoenix.server
途中で「依存関係をインストールする?」と聞かれてyesと答えるとこうなる。
noにしてもプロンプトに表示されるように、あとでnpm install && node node_modules/brunch/bin/brunch build
すれば良いらしい。
で続きもプロンプトに出ているように以下のコマンドを打つ。
mix deps.get Could not find hex, which is needed to build dependency :phoenix Shall I install hex? [Yn] 2015-09-14 23:18:39 URL:https://s3.amazonaws.com/s3.hex.pm/installs/1.0.0/hex.ez [269416/269416] -> "/Users/xxx/.mix/archives/hex.ez" [1] * creating /Users/xxx/.mix/archives/hex.ez Running dependency resolution Dependency resolution completed successfully cowboy: v1.0.3 cowlib: v1.0.1 decimal: v1.1.0 ecto: v1.0.2 fs: v0.9.2 phoenix: v1.0.2 phoenix_ecto: v1.2.0 phoenix_html: v2.2.0 phoenix_live_reload: v1.0.0 plug: v1.0.0 poison: v1.5.0 poolboy: v1.5.1 postgrex: v0.9.1 ranch: v1.1.0 ・・・以下略・・・
出だしにShall I install hex?
と聞かれるのでyesと答える。
hex
とはelixirのパッケージマネージャだそうだ。
あれ?ってことはmix
はmake
てきな位置付けなのかな?
このへんまだよくわからない。
次にmix ecto.create
。
ecto
はRailsでいうActiveRecord的なもの?
mix ecto.create Could not find rebar, which is needed to build dependency :fs I can install a local copy which is just used by mix Shall I install rebar? [Yn] * creating /Users/akiyoshi/.mix/rebar ==> fs (compile) Compiled src/sys/inotifywait.erl Compiled src/sys/fsevents.erl ・・・中略・・・ ** (Mix) The database for HelloPhoenix.Repo couldn't be created, reason given: "psql: FATAL: role \"postgres\" does not exist\n".
大量のファイルが生成されたが最後にエラー。 DBの設定ができてなかったみたい。 postgresユーザーをパスワードpostgresで作成する。
# postgresqlサーバー起動 pg_ctl -l /usr/local/var/postgres/server.log start # postgresユーザー作成 createuser -P -d postgres # 確認 psql -q -c'select * from pg_user' postgres
もういちどmix ecto.create
するとできた。
mix ecto.create The database for HelloPhoenix.Repo has been created.
ようやくサーバー起動
mix phoenix.server [info] Running HelloPhoenix.Endpoint with Cowboy on http://localhost:4000 14 Sep 23:37:53 - info: compiled 5 files into 2 files, copied 3 in 2398ms
http://localhost:4000/ にアクセスすると無事見れた。
参考
公式と以下の記事がとても参考になりました。感謝。