はじめに
はじめまして、サーバーサイドをRubyで書いているさ犬です。
今回は巷で人気が出て来ている関数型言語Elixir(エリクサー)のMacでの環境構築とwebアプリケーションフレームワークphoenix(フェニックス)を使ってアプリの雛形を作ってみようと思います。
環境
macOS 10.12
Homebrew 1.0.8
node v6.9.0
対象者
- Railsでのアプリケーション開発経験あり
- 新しい技術を触ってみたい人
導入
$ brew install erlang
$ brew install elixir-build
$ brew install exenv
$ echo 'export PATH="$HOME/.exenv/bin:$PATH"' >> ~/.zshrc
$ echo 'eval "$(exenv init -)"' >> ~/.zshrc
$ exec $SHELL
$ exenv install -l # installできるversion確認
$ exenv install 1.3.4
$ elixir -v
Erlang/OTP 19 [erts-8.1] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
Elixir 1.3.4
これでElixirのインストールが完了。
phoenixの導入準備をします。
$ mix local.hex # パッケージ管理ツールをインストール
# phoenixをインストール
$ mix archive.install https://github.com/phoenixframework/archives/raw/master/phoenix_new.ez
これでphoenixの準備が完了しました。
開発
ではPhoenixアプリケーションを作っていきます。
mix phoenix.new
のオプションを確認します。
$ mix phoenix.new
mix phoenix.new
Creates a new Phoenix project.
It expects the path of the project as argument.
┃ mix phoenix.new PATH [--module MODULE] [--app APP]
A project at the given PATH will be created. The application name and module
name will be retrieved from the path, unless --module or --app is given.
Options
• --app - the name of the OTP application
• --module - the name of the base module in the generated skeleton
• --database - specify the database adapter for ecto. Values can be
postgres, mysql, mssql, or mongodb. Defaults to postgres.
• --no-brunch - do not generate brunch files for static asset building.
When choosing this option, you will need to manually handle JavaScript
dependencies if building HTML apps
• --no-ecto - do not generate ecto files for the model layer
• --no-html - do not generate HTML views.
• --binary-id - use binary_id as primary key type in ecto models
Examples
┃ mix phoenix.new hello_world
Is equivalent to:
┃ mix phoenix.new hello_world --module HelloWorld
Without brunch:
┃ mix phoenix.new ~/Workspace/hello_world --no-brunch
今回はデータベースを mysql
で作るので、以下のコマンドを実行します。
$ mix phoenix.new phoenixSampleApp --database mysql
これでアプリの雛形ができました。
データベースも作っておきます。
$ cd phoenixSampleApp
$ mix ecto.create
あとはサーバーを起動して、 http://localhost:4000
にアクセスします。
$ mix phoenix.server
以下のような画面が出れば完成です
所感
コマンドさえ慣れれば、あとはrubyとrailsにほとんど似ているのでハードルは低い印象を受けました!