cheerfulstoic
Global state on startup
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
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #performance










First 3 of 3 Posts
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