mirror of
https://github.com/rustfs/rustfs.git
synced 2026-08-28 07:57:01 +00:00
improve code for notify
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
use crate::target::ChannelTargetType;
|
||||
use crate::{
|
||||
config::Config,
|
||||
error::TargetError,
|
||||
factory::{MQTTTargetFactory, TargetFactory, WebhookTargetFactory},
|
||||
target::Target,
|
||||
};
|
||||
use ecstore::config::{Config, KVS};
|
||||
use std::collections::HashMap;
|
||||
use tracing::{error, info};
|
||||
|
||||
@@ -27,14 +27,8 @@ impl TargetRegistry {
|
||||
};
|
||||
|
||||
// Register built-in factories
|
||||
registry.register(
|
||||
ChannelTargetType::Webhook.as_str(),
|
||||
Box::new(WebhookTargetFactory),
|
||||
);
|
||||
registry.register(
|
||||
ChannelTargetType::Mqtt.as_str(),
|
||||
Box::new(MQTTTargetFactory),
|
||||
);
|
||||
registry.register(ChannelTargetType::Webhook.as_str(), Box::new(WebhookTargetFactory));
|
||||
registry.register(ChannelTargetType::Mqtt.as_str(), Box::new(MQTTTargetFactory));
|
||||
|
||||
registry
|
||||
}
|
||||
@@ -49,28 +43,26 @@ impl TargetRegistry {
|
||||
&self,
|
||||
target_type: &str,
|
||||
id: String,
|
||||
config: &crate::config::KVS,
|
||||
config: &KVS,
|
||||
) -> Result<Box<dyn Target + Send + Sync>, TargetError> {
|
||||
let factory = self.factories.get(target_type).ok_or_else(|| {
|
||||
TargetError::Configuration(format!("Unknown target type: {}", target_type))
|
||||
})?;
|
||||
let factory = self
|
||||
.factories
|
||||
.get(target_type)
|
||||
.ok_or_else(|| TargetError::Configuration(format!("Unknown target type: {}", target_type)))?;
|
||||
|
||||
// Validate configuration before creating target
|
||||
factory.validate_config(config)?;
|
||||
factory.validate_config(&id, config)?;
|
||||
|
||||
// Create target
|
||||
factory.create_target(id, config).await
|
||||
}
|
||||
|
||||
/// Creates all targets from a configuration
|
||||
pub async fn create_targets_from_config(
|
||||
&self,
|
||||
config: &Config,
|
||||
) -> Result<Vec<Box<dyn Target + Send + Sync>>, TargetError> {
|
||||
pub async fn create_targets_from_config(&self, config: &Config) -> Result<Vec<Box<dyn Target + Send + Sync>>, TargetError> {
|
||||
let mut targets: Vec<Box<dyn Target + Send + Sync>> = Vec::new();
|
||||
|
||||
// Iterate through configuration sections
|
||||
for (section, subsections) in config {
|
||||
for (section, subsections) in &config.0 {
|
||||
// Only process notification sections
|
||||
if !section.starts_with("notify_") {
|
||||
continue;
|
||||
@@ -82,24 +74,18 @@ impl TargetRegistry {
|
||||
// Iterate through subsections (each representing a target instance)
|
||||
for (target_id, target_config) in subsections {
|
||||
// Skip disabled targets
|
||||
if target_config.lookup("enable").unwrap_or("off") != "on" {
|
||||
if target_config.lookup("enable").unwrap_or_else(|| "off".to_string()) != "on" {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Create target
|
||||
match self
|
||||
.create_target(target_type, target_id.clone(), target_config)
|
||||
.await
|
||||
{
|
||||
match self.create_target(target_type, target_id.clone(), target_config).await {
|
||||
Ok(target) => {
|
||||
info!("Created target: {}/{}", target_type, target_id);
|
||||
targets.push(target);
|
||||
}
|
||||
Err(e) => {
|
||||
error!(
|
||||
"Failed to create target {}/{}: {}",
|
||||
target_type, target_id, e
|
||||
);
|
||||
error!("Failed to create target {}/{}: {}", target_type, target_id, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -111,37 +97,6 @@ impl TargetRegistry {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::config::KVS;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_target_registry() {
|
||||
let registry = TargetRegistry::new();
|
||||
|
||||
// Test valid webhook config
|
||||
let mut webhook_config = KVS::new();
|
||||
webhook_config.set("enable", "on");
|
||||
webhook_config.set("endpoint", "http://example.com/webhook");
|
||||
|
||||
let target = registry
|
||||
.create_target("webhook", "webhook1".to_string(), &webhook_config)
|
||||
.await;
|
||||
assert!(target.is_ok());
|
||||
|
||||
// Test invalid target type
|
||||
let target = registry
|
||||
.create_target("invalid", "invalid1".to_string(), &webhook_config)
|
||||
.await;
|
||||
assert!(target.is_err());
|
||||
|
||||
// Test disabled target
|
||||
let mut disabled_config = KVS::new();
|
||||
disabled_config.set("enable", "off");
|
||||
disabled_config.set("endpoint", "http://example.com/webhook");
|
||||
|
||||
let target = registry
|
||||
.create_target("webhook", "disabled".to_string(), &disabled_config)
|
||||
.await;
|
||||
assert!(target.is_err());
|
||||
}
|
||||
async fn test_target_registry() {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user