2017-04-11 / syui

elixir

ElmとPhoenixで作成するWebアプリ

Elmというのは、Reactのようなフレームワークで、見通しが良いのが特徴(だと思っています)。昔、Hello World程度に使ったことがありました。Elmは色々と面倒なところはありますが、分かりやすさはあります(一見して)。

PhoenixはErlangを書きやすくしたElixirのWebフレームワークで、リアルタイムのリロード関連の処理がやりやすかった記憶。

これらを組み合わせて何か作ってる人はかなり少ないですが、探してみると幾つかの記事が見つかりました。

それを参考に何か作ろうかなーとか思っていたので紹介。

https://medium.com/@diamondgfx/setting-up-elm-with-phoenix-be3a9f55bac2

https://ubiteku.oinker.me/2016/11/28/elixir-phoenix-and-elm/

https://github.com/cotoami/cotoami

これを見る限りではcotoamiというプロジェクトがあって、何やらmd共有とチャットサービスみたいなものが実験的に稼働しているみたいな感じなのかな。

$ git clone https://github.com/cotoami/cotoami
$ cd cotoami
$ npm i
$ mix deps.get
$ mix ecto.create && mix ecto.migrate
$ mix phoenix.server

それぞれのバージョン。

$ mix -v
Mix 1.5.0-dev

$ mix phoenix.new -v
Phoenix v1.2.1

$ psql -V
psql (PostgreSQL) 9.5.3

$ elm -v
0.18.0

macでやるとerlang,elixirのバージョンとかpostgrexなどの設定とかでかなりハマる。

warning: variable “deps” does not exist and is being expanded to “deps()”

こんな感じでエラーが出る場合、mix.exsを編集して変数の語尾に()をつけることで回避可能。

psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket

以下はpidなどを削除。

FATAL: lock file “postmaster.pid” already exists

$ rm /usr/local/var/postgres/postmaster.pid
$ launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
$ ps aux | grep postgres

以下はプロセスのKILLのワンライナー

$ ps aux | grep postgres | sed 's/  //g' | cut -d ' ' -f 2|sed '1d' | xargs kill 

起動など。

$ initdb /usr/local/var/postgres -E utf8
$ pg_ctl -D /usr/local/var/postgres -l logfile start
$ createuser -P -d postgres
$ psql -l

[error] Failed to connect to Redis

多分、npm iでredisはインストールされてれると思うけど一応。

$ npm i redis
$ redis-server
$ mix phoenix.server

というか、ここまでやってみて、Erlang、かなりつらいです。Rubyといい勝負というか、そんな感じで。また、PhoenixもRailsのような辛さが見え隠れしている印象で、最近、Golangを触っていたせいか、正直、ErlangやRubyなどの言語はGolangと比較してなんとなく辛いというか、そう感じるところがあったりして、しかし、サーバーサイドやデータベースなどを考慮するなら、こちらの選択もありだと思うので、慣れていきたいなと思いつつ、やっぱり慣れの問題なのかもしれませんね。おわり。