diff --git a/services/feature-flags/src/main.rs b/services/feature-flags/src/main.rs index 407272ab1..b7ab173b1 100644 --- a/services/feature-flags/src/main.rs +++ b/services/feature-flags/src/main.rs @@ -1,28 +1,29 @@ use anyhow::Result; use tracing::{info, Level}; use tracing_subscriber::EnvFilter; pub mod config; pub mod constants; pub mod database; +pub mod service; fn configure_logging() -> Result<()> { let filter = EnvFilter::builder() .with_default_directive(Level::INFO.into()) .with_env_var(constants::LOG_LEVEL_ENV_VAR) .from_env_lossy(); let subscriber = tracing_subscriber::fmt().with_env_filter(filter).finish(); tracing::subscriber::set_global_default(subscriber)?; Ok(()) } #[tokio::main] async fn main() -> Result<()> { config::parse_cmdline_args(); configure_logging()?; let _aws_config = config::load_aws_config().await; info!("Starting the service"); Ok(()) } diff --git a/services/feature-flags/src/service.rs b/services/feature-flags/src/service.rs new file mode 100644 index 000000000..8fcd4a9d7 --- /dev/null +++ b/services/feature-flags/src/service.rs @@ -0,0 +1,57 @@ +use crate::database::{DatabaseClient, FeatureConfig, Platform}; +use comm_services_lib::database::Error; +use std::collections::HashSet; + +pub struct FeatureFlagsService { + _db: DatabaseClient, +} + +impl FeatureFlagsService { + pub fn new(db_client: DatabaseClient) -> Self { + FeatureFlagsService { _db: db_client } + } + + pub async fn _enabled_features_set( + db: &DatabaseClient, + platform: Platform, + code_version: i32, + is_staff: bool, + ) -> Result, Error> { + let features_config = db.get_features_configuration(platform).await?; + Ok( + features_config + .into_values() + .filter_map(|config| { + Self::_feature_name_if_enabled(code_version, is_staff, config) + }) + .collect(), + ) + } + + fn _feature_name_if_enabled( + code_version: i32, + is_staff: bool, + feature_config: FeatureConfig, + ) -> Option { + feature_config + .config + .keys() + .filter(|version| *version <= &code_version) + .max() + .and_then(|version| feature_config.config.get(version)) + .map(|config| { + if is_staff { + config.staff + } else { + config.non_staff + } + }) + .and_then(|is_enabled| { + if is_enabled { + Some(feature_config.name) + } else { + None + } + }) + } +}