Home
Slides
Blog
slide.seike460.com
Home
Slides
Blog
Home
Slides
Engineers Day Fukuoka 2018 Autumn
Engineers Day Fukuoka 2018 Autumn
Engineers Day Fukuoka 2018 Autumn
2018年11月10日
General
Engineering
Architecture
Best Practices
Keyboard Shortcuts
←
→
Navigate slides
Space
Next slide
F
Fullscreen
ESC
Exit fullscreen
Home
First slide
End
Last slide
B
.
Pause
S
Speaker notes
?
Reveal.js help
Close
## Replace PHP Using Elixir FUKUOKA Engineers Day 2018 ~Autumn~ 清家史郎(@seike460)
###### Who? Fusic Co., Ltd.  清家史郎  @seike460    
### tech - Program Language - PHP - Go - Elixir - infrastructure - Server - infrastructure as code - Network - Other - Serverless(AWS) - Vue.js
###### products ##### Go - s3ry (s3 prompt cli) - https://github.com/seike460/s3ry - utakata (Serverless Slack Notificatier) - https://github.com/seike460/utakata
##### Community - PHP - fukuoka.php - Go - fukuoka.go - Elixir - fukuoka.ex
###### Organizer - Serverless Meetup Fukuoka 11月(多分)やります!!! (運営メンバに宣言したけど了承はとってない)
#### Agenda - Elixir? - Why Elixir? - Elixir's merit - Elixir's demerit
#### Elixir? - 強い動的型付け - 関数型言語 - Erlangの仮想環境 (BEAM) 上で動作 - Erlang? - BEAM?
#### Erlang? - 大量の並列処理 - ある程度のリアルタイム性を備える - 障害への耐性 - 無停止で稼働する
#### BEAM? - Erlangの仮想環境(JVMのようなもの) - 超軽量プロセス - 信頼性への最適化 - let-it-crash(エラーを許容するシステム)
#### why Elixir? - RubyLikeな構文 - 書きやすさ - Railsに影響を受けたWAF - 導入障壁の低さ - Elixir自体の書きやすさ - パターンマッチ、パイプライン演算子
#### why Elixir2? - Erlang VM上で動作 - Erlang と BEAM の恩恵を受けれる - 大量の並列処理 - 高信頼性、耐障害性 - Testの書きやすさ - 関数型言語 - doctest ### Replace PHP Using Elixir!
##### 何をReplaceしてるの? - Mist  - Mail Iinakji Seiri Tool - メールサーバの入替で機能が合わなくなった - 今年中に頑張る
- 本日触れる要素 - Elixir - 言語 - mix - プロジェクト管理ツール - Phoenix - WAF - ecto - ORM
#### Architecture 
#### Elixir - brew ``` brew install elixir ``` ``` iex Erlang/OTP 21 [erts-10.0.4] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe] [dtrace] Interactive Elixir (1.7.1) - press Ctrl+C to exit (type h() ENTER for help) iex(1)> ``` - Docker ``` docker run -it --rm elixir Erlang/OTP 20 [erts-9.3.3.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false] Interactive Elixir (1.6.6) - press Ctrl+C to exit (type h() ENTER for help) iex(1)> ```
##### mix PHPで言うならComposer、RubyならRubyGemsの様なパッケージ管理ツール プロジェクト作成 ``` mix new seike460 cd seike460 vim mix.exs ```
読み込むパッケージを設定 ``` defp deps do [ {:phoenix, "~> 1.3.3"}, {:phoenix_pubsub, "~> 1.0"}, {:phoenix_ecto, "~> 3.2"}, {:postgrex, ">= 0.0.0"}, {:phoenix_html, "~> 2.10"}, {:phoenix_live_reload, "~> 1.0", only: :dev}, {:gettext, "~> 0.11"}, {:excoveralls, "~> 0.9", only: :test}, {:ecto_soft_delete, "0.1.0"}, {:mix_test_watch, "~> 0.8", only: :dev, runtime: false}, {:slack, "~> 0.15.0"}, {:cowboy, "~> 1.0"} ] end ``` パッケージ取得 ``` mix deps.get ```
##### Phoenix hexインストール Elixir, Erlang 向けのパッケージ管理ツール ``` mix local.hex mix archive.install https://github.com/phoenixframework/archives/raw/master/phx_new.ez mix phx.server ```
##### ecto mix.exs編集 ``` vim mix.exs ``` ectoを追記 ``` defp deps do [{:ecto, "~> 2.0"}, {:postgrex, "~> 0.11"}] end ``` パッケージ取得 ``` mix deps.get ```
ectoが接続するDB設定 dev環境の設定を行う場合 ``` vim config/dev.exs ``` ``` config :mist, Mist.Repo, adapter: Ecto.Adapters.Postgres, username: "postgres", password: "postgres", database: "mist", hostname: "postgres", pool_size: 20 ```
##### HTML eexファイルへPhoenixからパラメーターを渡してHTML構築 以下の様な形式でパラメーターの埋め込みが可能 ``` <%= link "New Mail", to: mail_path(@conn, :new) %> ```
##### API PhoenixをAPIにして フロントエンド( Vue.js + Vuex )に対して情報を渡す事も出来る 弊社古賀がqiita書いてました  - Qiita - [Phoenix + Vue.js入門](https://qiita.com/koga1020/items/c02a0fd5ae11fb5da1e0) - twitter - [koga1020_](https://twitter.com/koga1020_)
##### Elixir's merit - パターンマッチ - 「パターン」の照合をするもの - パイプライン演算子 - 関数の戻り値をパイプライン演算子を利用して次の関数に渡す - シェル芸好きにはすごくやりやすい - テストとの親和性 - 関数型言語である - doctest
##### 書き方 ``` def create(conn, %{"mail" => mail_params}) do Repo.transaction fn -> case Mails.create_mail(mail_params) do {:ok, mail} -> case Applications.create_application(%{type: :mail, type_id: mail.id, status: mail_params.status, apply_name: mail_params.apply_name}) do {:ok, application} -> conn |> put_flash(:info, "メールの申請が行われました") |> redirect(to: mail_path(conn, :show, mail)) {:error, %Ecto.Changeset{} = changeset} -> exit(changeset) render(conn, "new.html", changeset: changeset) end {:error, %Ecto.Changeset{} = changeset} -> render(conn, "new.html", changeset: changeset) end end end ```
##### パイプライン演算子 shell芸っぽいやつ ``` def create_mail(attrs \\ %{}) do %Mail{} |> Mail.changeset(attrs) |> Repo.insert() end def changeset(mail, attrs) do mail |> cast(attrs, [:email, :name, :in_use]) |> validate_required([:email, :name, :in_use]) end ``` foo(bar(baz(new_function(other_function())))) こんな関数呼び出しを以下のように出来る other_function() |> new_function() |> baz() |> bar() |> foo()
##### パターンマッチ 「パターン」の照合をするもので、マッチするならそれに伴い処理が実行される - controllers ``` case Mails.create_mail(mail_params) do {:ok, mail} -> … OKだった時の処理 {:error, %Ecto.Changeset{} = changeset} -> … errorだった時の処理 end ``` - Mails.create_mail ``` def create_mail(attrs \\ %{}) do %Mail{} |> Mail.changeset(attrs) |> Repo.insert() end ``` Repo.insertは {:ok, mail} or {:error, changeset} を返す
###### オブジェクト指向  - methodのテスト範囲 - 実行中プログラムから渡されたデータ - objectのデータ - どこにどんな風に反映させるのか
###### 関数型  - methodのテスト範囲 - 実行中プログラムから渡されたデータ - どんな風に返すのか
##### doctest - Docコメントがテストになる - Docコメントがドキュメントになる ``` @doc """ Gets a single mail. Raises `Ecto.NoResultsError` if the Mail does not exist. ## Examples iex> get_mail!(123) %Mail{} iex> get_mail!(456) ** (Ecto.NoResultsError) """ ```
- mix test.watch https://github.com/lpil/mix-test.watch ``` def deps do [{:mix_test_watch, "~> 0.8", only: :dev, runtime: false}] end ``` - ファイルを更新する度にテストしてくれる - 全てのコードでテストを書きながら安全なプログラム ~~理想~~ ``` $ mix test.watch ```
##### Elixir's demerit - ライブラリ系は少し弱い - ここは今から増えて行くし、自分で書けば良い - ドキュメント - 英語読めれば、大丈夫だ問題ない ~~読めるとは言っていない~~ - fukuoka.exの人たちが頑張ってる、期待 - https://qiita.com/organizations/fukuokaex
#### 纏め - 今回正解の動きありきで 置き換えしているのでTDD的に進めれてる - 置換前と置換後から2つの言語の理解が進む - スッキリ書けるので、書いてて気持ち良い - PHP、Goに並ぶ第三言語として進めるつもりでいる - みんな、Elixir書こう!
##### 宣伝  - fukuoka.ex#14:Elixir/Phoenixもくもく会~入門もあるよ(19:00開始) - https://fukuokaex.connpass.com/event/100659/ - リモートで参加可能! - (僕もリモート参加しました、東京の人と一緒にモクモクできたりします - 今回経験者が教えてくれるっぽい、行きたい - Serverless Confと重なり行けない…
Thank you! Fusicは技術が大好きなエンジニアを募集しています  https://fusic.github.io/
Swipe to navigate
Previous
Next
Related Slides
Fukuoka LT 2019
2019/12/20
View
kixs vol.8
2019/10/26
View
ちびでべろっぱぁ〜ず #11
2019/8/3
View