stevensonmt

stevensonmt

Using TOML for user-specified runtime config

I’m starting to work on my first real app and can’t quite understand the runtime configuration story. I would like to use a TOML file to hold the configuration data. My plan is to look for this config.toml file in the path $XDG_CONFIG_HOME/my_app/config.toml or perhaps even iterate through $XDG_CONFIG_DIRS appending /myapp/config.toml to each until one is found. If no file is found a default config will be included.

I would like to use the Toml library to parse the config but I’m not following the guidance on organizing the configuration from the docs. I’m also not clear on how one would access the config data later.

From here I gather I need a config/runtime.exs to fetch the configuration data. I assume this is where the code suggested in the Toml docs goes:

# config/runtime.exs

config_providers: [
  {Toml.Provider, [
    path: {:system, "XDG_CONFIG_DIR", "myapp.toml",
    transforms: [...]
  ]}
]

So let’s say I have something like this snippet of the example from the Toml docs:

# This is an example of something that would be ignored
title = "My config file"

# We're expecting something like this:
[myapp]
key = "value"

How do I later access myapp.key to use “value”?

I’m wondering if it’s Applicationg.get_env/3?

Marked As Solved

cmo

cmo

If you’re using the Toml config provider because you don’t want to use runtime.exs, then don’t use runtime.exs. They’re two different sources of config. One does not load the other. You could use both if you want to confuse yourself or if you want the runtime.exs stuff ever so slightly further out of reach than the config.toml.

And yes, config is retrieved with Application.get_env and friends.

Also Liked

cmo

cmo

You can also use something like Vapor.

stevensonmt

stevensonmt

Excellent. Thanks for your help. I’m trying not to assume end users would be familiar with Elixir but would be with TOML.

Where Next?

Popular in Questions Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New

Other popular topics Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New

We're in Beta

About us Mission Statement