Skip to content

.env variables #258

Description

@korir248

Hi, I'm trying to use the config crate in my project and can't seem to make nested env variables work Could you please upload a .env,example to see how you are working with the separators.

#[derive(Debug, Deserialize)]
pub struct AppConfig {
    pub database: DatabaseConfig,
    pub upload: UploadConfig,
    pub salt: String,
}

#[derive(Debug, Deserialize)]
pub struct UploadConfig {
    pub max_file_size: usize,
    pub allowed_types: Vec<String>,
    pub storage_prefix: String,
}
#[derive(Debug, Deserialize)]
pub struct DatabaseConfig {
    pub url: String,
}

impl AppConfig {
    pub fn from_env() -> Result<AppConfig, ConfigError> {
        let config = Config::builder()
            .add_source(Environment::default().separator("_").try_parsing(true))
            .add_source(
                Environment::with_prefix("UPLOAD")
                    .prefix_separator("_")
                    .separator("_")
                    .try_parsing(true)
                    .with_list_parse_key("ALLOWED_TYPES"),
            )
            .build()?;

        // println!("Config values: {:#?}", config);

        config.try_deserialize()
    }
}

with my .env having

DATABASE_URL=
SALT=
UPLOAD_MAX_FILE_SIZE=
UPLOAD_ALLOWED_TYPES=
UPLOAD_STORAGE_PREFIX=

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions