#
Config
We use this crate to handle the environment variables. It allows us to use them in a more secured way than just using
the env::var("MY_VARIABLE")
standard function, as the app will not run if a variable is missing.
#
Add more variables
To add more variables, first add them to your .env
file. Then, you can add them to the crates/config/src/lib.rs
file,
more precisely to the get_from_env
function. You can also add a default value by passing a second parameter, and a
visibility in the third one (one of Public
, Private
).
#
Crate content
#
Structs
#[derive(Debug, PartialEq)]
pub enum Visibility {
Private,
Public,
}
#[derive(Debug, Clone)]
pub struct ApiConfig {
pub base_url: Url,
pub port: u16,
pub ip: String,
pub database: DbConfig,
pub jwt_secret: String,
pub jwt_token_duration_days: i32,
}
#[derive(Debug, Clone)]
pub struct DbConfig {
pub url: String,
}
#[derive(Debug, Clone)]
pub struct SentryConfig {
pub dsn: String,
pub traces_sample_rate: f32,
}
#[derive(Debug, Clone)]
pub struct StripeConfig {
pub secret_key: String,
pub webhook_key: String,
}
#[derive(Debug, Clone)]
pub struct Config {
pub api: ApiConfig,
pub sentry: SentryConfig,
pub stripe: StripeConfig,
}
#
Methods
fn get_from_env_log<T>(env_var: &str, default_value: Option<T>, visibility: &Visibility) -> String where T: Display
fn from_env<T>(env_var: &str, default_value: Option<T>, visibility: &Visibility) -> T where T: FromStr + Display, <T as FromStr>::Err: std::fmt::Debug
pub fn get_from_env() -> Self