cheerfulstoic
Hey! I’m really enjoying getting into Elixir / Phoenix, but!
In my application, I would like to fetch some configuration from a JSON file when the app starts up and then have that configuration globally available in the Phoenix app. I have a lib module with a function which gets the configuration data and I can call that from my Phoenix application’s ex file, but I’m not sure where to put the result (or even if it’s a good idea). Thoughts?
Thanks,
Brian
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
How do I tell Hologram not to escape HTML when substituting into templates?
{Textile.render(@product.long_description)}
escapes the HTM...
New
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used?
I’ve got two Github accoun...
New
Hello all. I have a question as to best practices for code organization when implementing a liveview with a lot of responsibilities.
Sup...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #elixirconf-us
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #elixirconf-eu
- #processes
- #security
- #api
- #forms
- #performance










Showing Posts 1 to 3- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
YellowApple
The normal place for this sort of configuration would be
config/config.exsor some other.exsfile loaded by it. In other words, using configuration formats other than just ordinary Elixir code is generally discouraged.That said, if you for some reason absolutely need to accept JSON configuration files specifically, the best place would still probably be something in or around
config/config.exs. You’d probably need to define that function there, then call it with whatever arguments needed to load your JSON config. The resulting map (or whatever internal format used after parsing) could then be assigned to a configuration key for your OTP app.For example (note the anonymous function in a variable; you can probably define a module in here, but whatever):
crusso
For an app I have, I wrote a GenServer that manages my preferences. It’s pretty simple, so it might not meet your needs. It loads up a prefs.json file at startup (you have to include it in your list of supervised children in your main application module.)
The app depends upon the ExActor GenServer helper as well as the Poison JSON library. You’ll have to add them as dependencies if you’d like to use the code, which you’re welcome to.
You can call into the module like so:
MyApp.Service.PrefsManager.get_value(“size”)
MyApp.Service.PrefsManager.set_value(“size”, 20)
cheerfulstoic
@YellowApple Thanks! I think I didn’t explain what I was doing well enough. I’ve definitely got some configuration variables in
config/config.exs. They aregithub_tokenandrepo_path. The idea of the app is that it will be able to receive webhooks from GitHub for certain events, so I go to the GitHub repo and get ajsonfile from the root which has configuration for the behaviors on how to handle the webhooks. After your response it occurs to me that I might be able to have the configuration inconfig/config.exs, but my concern was being able to put the configuration into a place / format that it would be easy for anybody to update. Thinking on it further, it actually probably makes sense anyway to get the configuration each time. Part of the plan would also be to pull Markdown files from the repo as well which would contain text which would be used as part of the webhooks. It doesn’t really make sense to store the configuration globally anyway because it should always be using the latest copy.@crusso Thanks, I had read about using processes to store ongoing state, so I might play with that as a solution