Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rubik/hydroconf
Effortless configuration management for Rust, inspired by Dynaconf.
https://github.com/rubik/hydroconf
12-factor configuration configuration-management
Last synced: about 2 months ago
JSON representation
Effortless configuration management for Rust, inspired by Dynaconf.
- Host: GitHub
- URL: https://github.com/rubik/hydroconf
- Owner: rubik
- License: isc
- Created: 2020-08-03T08:27:43.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-10-12T06:10:18.000Z (about 1 year ago)
- Last Synced: 2024-10-13T08:56:38.116Z (2 months ago)
- Topics: 12-factor, configuration, configuration-management
- Language: Rust
- Homepage:
- Size: 74.2 KB
- Stars: 48
- Watchers: 3
- Forks: 6
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Hydroconf is a configuration management library for Rust, based on [config-rs]
and heavily inspired by Python's [dynaconf].# Features
* Inspired by the [12-factor] configuration principles
* Effective separation of sensitive information (secrets)
* Layered system for multi environments (e.g. development, staging, production,
etc.)
* Sane defaults, with a 1-line configuration loading
* Read from [JSON], [TOML], [YAML], [HJSON], [INI] filesThe [config-rs] library is a great building block, but it does not provide a
default mechanism to load configuration and merge secrets, while keeping the
different environments separated. Hydroconf fills this gap.[config-rs]: https://github.com/mehcode/config-rs
[dynaconf]: https://github.com/rochacbruno/dynaconf
[12-factor]: https://12factor.net/config
[JSON]: https://github.com/serde-rs/json
[TOML]: https://github.com/toml-lang/toml
[YAML]: https://github.com/chyh1990/yaml-rust
[HJSON]: https://github.com/hjson/hjson-rust
[INI]: https://github.com/zonyitoo/rust-ini# Quickstart
Suppose you have the following file structure:
```
├── config
│ ├── .secrets.toml
│ └── settings.toml
└── your-executable
````settings.toml`:
```toml
[default]
pg.port = 5432
pg.host = 'localhost'[production]
pg.host = 'db-0'
````.secrets.toml`:
```toml
[default]
pg.password = 'a password'[production]
pg.password = 'a strong password'
```Then, in your executable source (make sure to add `serde = { version = "1.0",
features = ["derive"] }` to your dependencies):```rust
use serde::Deserialize;
use hydroconf::Hydroconf;#[derive(Debug, Deserialize)]
struct Config {
pg: PostgresConfig,
}#[derive(Debug, Deserialize)]
struct PostgresConfig {
host: String,
port: u16,
password: String,
}fn main() {
let conf: Config = match Hydroconf::default().hydrate() {
Ok(c) => c,
Err(e) => {
println!("could not read configuration: {:#?}", e);
std::process::exit(1);
}
};println!("{:#?}", conf);
}
```If you compile and execute the program (making sure the executable is in the
same directory where the `config` directory is), you will see the following:```sh
$ ./your-executable
Config {
pg: PostgresConfig {
host: "localhost",
port: 5432,
password: "a password"
}
}
```Hydroconf will select the settings in the `[default]` table by default. If you
set `ENV_FOR_HYDRO` to `production`, Hydroconf will overwrite them with the
production ones:```sh
$ ENV_FOR_HYDRO=production ./your-executable
Config {
pg: PostgresConfig {
host: "db-0",
port: 5432,
password: "a strong password"
}
}
```Settings can always be overridden with environment variables:
```bash
$ HYDRO_PG__PASSWORD="an even stronger password" ./your-executable
Config {
pg: PostgresConfig {
host: "localhost",
port: 5432,
password: "an even stronger password"
}
}
```The description of all Hydroconf configuration options and how the program
configuration is loaded can be found in the
[documentation](https://docs.rs/hydroconf).