Elixir 本身支援使用 Erlang 的模組進行使用,本篇以 hackney Erlang 模組為例。
建立專案
使用 mix new [appname] 建立一個專案,並進入該資料夾修改 mix.exs 檔案最後的 deps:
defmodule Myapp.MixProject do
use Mix.Project
def project do
[
app: :myapp,
version: "0.1.0",
elixir: "~> 1.11",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end
# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:hackney, github: "benoitc/hackney"} # 這是 Erlang 的模組,
{:plug_cowboy, "~> 2.0"}
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
end
end
其中, github 這個參數是用 repo 的帳號/repo 名稱, elixir 會自己找到並編譯。
然後,使用 mix deps.get 去進行外部模組編譯。
編譯完成後,用 iex -S mix 編譯整個專案並開啟。
(一開始使用 rebar3 編譯,發現那是編譯給 erlang 的,其實應該在各自專案編譯 elixir 的版本)
Reference:
https://hexdocs.pm/plug/readme.html#installation
https://github.com/benoitc/hackney
沒有留言:
張貼留言