rust-cli / config-rs

⚙️ Layered configuration system for Rust applications (with strong support for 12-factor applications).
https://docs.rs/config/latest/config/
Apache License 2.0
2.69k stars 218 forks source link

Enable placeholders on configuration files #345

Open FaveroFerreira opened 2 years ago

FaveroFerreira commented 2 years ago

Feature description

Ability to declare placeholder values in our configuration files that will be taken from the environment variables.

Use Case

Sometimes we do not decide how our environment variables will be named, so we don't get to define them with prefixes like APP__

It would be nice if we could declare placeholders in our configuration files where the values should be taken from our environment variables.

Example:

application_port: ${PORT} #value taken from PORT environment variable
database:
  host: "127.0.0.1"
  port: 5432
  username: "postgres"
  password: "${DB_PASS}" #value taken from DB_PASS environment variable
  database_name: "${DB_HOST}" #value taken from DB_HOST environment variable
  require_ssl: false
matthiasbeyer commented 2 years ago

Sounds like something for the "re-thinking" ideas list, thus I added it to the project. I guess we could add (optional) support for templating engines, which would easily resolve this feature request. Adding own logic that does such replacements would be overkill and out of scope for this crate, IMO, but I like the idea of exposing enough interfaces so that a user can easily add templating to the loading/parsing logic of config-rs!

FaveroFerreira commented 2 years ago

Nice to hear that!

Comming from an Java + Spring background, maybe I am kinda biased towards using placeholders the way I pointed out on the issue description.

Just out of curiosity, do you have a different approach in mind? Maybe I can try and implement it.

matthiasbeyer commented 2 years ago

I do not yet have anything specific in mind, no. If you like to experiment (emphasis on "experiment"), have a look at the rethinking issues. There's a branch linked somewhere where I am exploring my ideas.

holmofy commented 5 months ago

I also have a background in Java+Spring. Spring has a very good configuration mechanism.

https://docs.spring.io/spring-boot/how-to/properties-and-configuration.html

Recently, I saw that the Rust ecosystem has a loco.rs project, which also supports placeholder configuration.

The implementation of loco.rs is a good reference.

https://loco.rs/docs/getting-started/config/