LoginSignup
17
17

More than 5 years have passed since last update.

Elixir Mix.Configの使い方

Last updated at Posted at 2015-05-08

データベースのユーザー名とパスワードなどの情報をハードコーディングしたくないときにconfig/config.exsファイルに保存するすることができる。ソースコードのなかで、Mix.Config.read!("config/config.exs")でファイルの中身を読み込む。

新しいプロジェクトproを作成する

$ mix new pro

pro/config/config.exsファイルの中に下の内容で保存する

use Mix.Config
config  :test_db1,
  username: "root",
  password: "123"

config :test_db2,
  username: "toor",
  password: "321"

lib/pro.exファイルの中に下の内容で保存する

defmodule Pro do

  def main() do

    # config.exsファイルの中の設定を取得する
    conf = Mix.Config.read!("config/config.exs")

    IO.inspect conf[:test_db1][:username]
    IO.inspect conf[:test_db1][:password]
    IO.inspect conf[:test_db2][:username]
    IO.inspect conf[:test_db2][:password]
  end
end

実行する

$ mix run -e "Pro.main()"
"root"
"123"
"toor"
"321"

これで設定ファイルconfig.exsの設定は取得することができた

下のリンクを参照して、Mix.Config.read!を使うことが分かる

17
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
17