This repository has been archived on 2024-07-25. You can view files and clone it, but cannot push or open issues or pull requests.
enve/README.md

137 lines
3.6 KiB
Markdown
Raw Normal View History

2022-07-22 15:05:10 +03:00
# enve
2022-07-18 14:36:24 +03:00
2022-07-22 15:05:10 +03:00
[![CI](https://github.com/pleshevskiy/enve/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/pleshevskiy/enve/actions/workflows/ci.yml)
2020-11-12 10:45:06 +03:00
[![unsafe forbidden](https://img.shields.io/badge/unsafe-forbidden-success.svg)](https://github.com/rust-secure-code/safety-dance/)
2022-07-22 15:05:10 +03:00
[![Documentation](https://docs.rs/pleshevskiy/badge.svg)](https://docs.rs/enve)
[![Crates.io](https://img.shields.io/crates/v/enve)](https://crates.io/crates/enve)
![Crates.io](https://img.shields.io/crates/l/enve)
2019-12-24 19:37:55 +03:00
2019-12-23 15:24:36 +03:00
Easy build a configs from environment variables and use it in globally.
We recommend you start with the [documentation].
2020-01-10 08:23:06 +03:00
## Motivation
2022-07-18 14:36:24 +03:00
I began to use rust with web programming experience where environment variables
are widely used and often there are more then 50 of them. First I looked at
already created libraries. But there it's necessary to initialise structure that
needs to be moved to each function where you need variable. It uses little bit
memory, but configuration lifetime is as long as application lifetime. Because
of it I decided to create my own library.
2020-01-10 08:23:06 +03:00
2020-03-12 23:20:34 +03:00
## Installation
2022-07-21 18:55:16 +03:00
The MSRV is 1.39.0
2020-03-12 23:20:34 +03:00
2022-07-22 15:05:10 +03:00
Add `enve = { version = "1.0", features = ["mod"] }` as a dependency in
2022-07-18 14:36:24 +03:00
`Cargo.toml`.
2020-03-12 23:20:34 +03:00
`Cargo.toml` example:
```toml
[package]
name = "my-crate"
version = "0.1.0"
authors = ["Me <user@rust-lang.org>"]
[dependencies]
2022-07-22 15:05:10 +03:00
enve = { version = "1.0", features = ["mod"] }
2020-03-12 23:20:34 +03:00
```
2021-04-16 10:35:42 +03:00
## Basic usage
2019-12-23 15:24:36 +03:00
```rust
use std::env;
//use dotenv::dotenv;
2019-12-23 15:24:36 +03:00
2022-07-22 15:05:10 +03:00
enve::mod! {
2020-01-10 23:42:41 +03:00
DEBUG: bool => false,
2021-02-23 23:44:17 +03:00
2020-01-10 23:42:41 +03:00
#[env_name = "APP_HOST"]
2020-01-07 22:36:50 +03:00
HOST: String => "127.0.0.1",
2021-02-23 23:44:17 +03:00
2021-04-16 10:35:42 +03:00
database {
URL < (
"postgres://",
POSTGRES_USERNAME => "user",
":",
POSTGRES_PASSWORD => "pass",
"@",
POSTGRES_HOST => "localhost:5432",
"/",
POSTGRES_DB => "test",
),
pool {
MAX_SIZE: usize => 15,
},
},
sentry {
DSN: Option<&'static str>,
},
feature {
static CORS: bool => false,
static GRAPHQL_PLAYGROUND: bool => false,
},
2019-12-23 15:24:36 +03:00
}
fn main () {
2020-07-08 16:50:41 +03:00
// dotenv().expect("dotenv setup to be successful");
// or
2021-04-16 10:35:42 +03:00
env::set_var("FEATURE_CORS", "true");
2021-02-23 23:44:17 +03:00
2020-03-12 23:20:34 +03:00
config::init();
assert_eq!(config::HOST(), String::from("127.0.0.1"));
2021-04-16 10:35:42 +03:00
assert_eq!(config::database::URL(), String::from("postgres://user:pass@localhost:5432/test"));
assert_eq!(config::database::pool::MAX_SIZE(), 15);
assert_eq!(config::sentry::DSN(), None);
assert_eq!(config::feature::CORS(), true);
2019-12-23 15:24:36 +03:00
}
```
2022-07-18 14:36:24 +03:00
Macro is an optional feature, disabled by default. You can use this library
without macro
```rust
use std::env;
// use dotenv::dotenv;
fn main() {
2020-07-08 16:50:41 +03:00
// dotenv().expect("dotenv setup to be successful");
// or
env::set_var("DATABASE_URL", "postgres://127.0.0.1:5432/test");
2022-07-22 15:05:10 +03:00
let database_url = enve::get::<String>("DATABASE_URL").unwrap();
let new_profile: bool = enve::get("FEATURE_NEW_PROFILE").unwrap_or_default();
let articles_per_page: u32 = enve::get_or_set_default("ARTICLES_PER_PAGE", 10);
}
```
2019-12-24 19:37:55 +03:00
## Running tests
```bash
2021-02-23 23:44:17 +03:00
cargo test --all-features
2019-12-24 19:37:55 +03:00
```
## Available features
2022-07-18 14:36:24 +03:00
- **macro** - Activates `config!` macros for easy configure web application.
2022-07-22 15:05:10 +03:00
- **number** - Group for features: `int`, `uint` and `float`.
- **bool** - impl EnvString for `bool` type `serde_json` package). ⚠
**_DEPRECATED_**
2019-12-24 19:37:55 +03:00
## License
2022-07-22 15:05:10 +03:00
[MIT] © [pleshevskiy](https://github.com/pleshevskiy)
2019-12-24 19:37:55 +03:00
## Contributors
2022-07-18 14:36:24 +03:00
[pleshevskiy](https://github.com/pleshevskiy) (Dmitriy Pleshevskiy) creator,
maintainer.
2019-12-23 15:24:36 +03:00
2022-07-22 15:05:10 +03:00
[documentation]: https://docs.rs/enve
[MIT]: https://github.com/icetemple/enve-rs/blob/master/LICENSE