Skip to content

Enable support for environment specific configs in modules #204

@dmitrypol

Description

@dmitrypol

When building modules developers frequently have to specify different config values in their local dev or test environments vs when module is loaded on production servers.

Pass env like this MODULE LOAD /path/here/libmodule1.dylib dev

static ENV_CONFIG: OnceCell<EnvConfig> = OnceCell::new();

fn init(_ctx: &Context, args: &[ValkeyString]) -> Status {
    // if env arg passed, use it
    let env_name = match args.get(0) {
        Some(tmp) => tmp.to_string(),
        None => "".to_string(),
    };
    let _ = ENV_CONFIG.set(EnvConfig::new(env_name.as_str()));
    Status::Ok
}

#[derive(Debug, Deserialize)]
pub(crate) struct EnvConfig {
    pub(crate) foo: String,
    pub(crate) bar: i32,
}

impl EnvConfig {
    pub(crate) fn new(env: &str) -> Self {
        let env_config = match env {
            "dev" => EnvConfig {
                foo: "dev_foo".to_string(),
                bar: 1
            },          
            _ => EnvConfig {
                // default to prod
                foo: "prod_value".to_string(),
                bar: 2
            },
        };
        env_config
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions