cheerfulstoic

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

Most Liked Switch mode

YellowApple

YellowApple

The normal place for this sort of configuration would be config/config.exs or some other .exs file 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):

# config.exs
json_loader = fn(path) -> do_the_needful end

config :my_app, json_config: json_loader.("some_file.json")

Last Post!

cheerfulstoic

cheerfulstoic OP

@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 are github_token and repo_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 a json file 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 in config/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

Where Next?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
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
spammy
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
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
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
michallepicki
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

Other Trending Topics Top

JesseHerrick
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
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
ausimian
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
type1fool
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
akoutmos
@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

We're in Beta

About us Mission Statement