Skip to main content

blinq_common/
config.rs

1use std::collections::HashMap;
2
3/// Configuration for the common crate that can be overridden by microservices
4#[derive(Debug, Clone)]
5pub struct CommonConfig {
6    /// Service name for logging and tracing
7    pub service_name: String,
8    /// Default log level
9    pub log_level: String,
10    /// Custom headers for CORS
11    pub cors_allowed_origins: Vec<String>,
12    /// Custom headers for CORS
13    pub cors_allowed_headers: Vec<String>,
14    /// Additional configuration that can be extended by microservices
15    pub custom: HashMap<String, String>,
16}
17
18impl Default for CommonConfig {
19    fn default() -> Self {
20        Self {
21            service_name: "blinq-service".to_string(),
22            log_level: "info".to_string(),
23            cors_allowed_origins: vec!["*".to_string()],
24            cors_allowed_headers: vec!["*".to_string()],
25            custom: HashMap::new(),
26        }
27    }
28}
29
30impl CommonConfig {
31    /// Create a new config with service name
32    pub fn new(service_name: impl Into<String>) -> Self {
33        Self {
34            service_name: service_name.into(),
35            ..Default::default()
36        }
37    }
38
39    /// Set log level
40    pub fn with_log_level(mut self, level: impl Into<String>) -> Self {
41        self.log_level = level.into();
42        self
43    }
44
45    /// Set CORS allowed origins
46    pub fn with_cors_origins(mut self, origins: Vec<String>) -> Self {
47        self.cors_allowed_origins = origins;
48        self
49    }
50
51    /// Set CORS allowed headers
52    pub fn with_cors_headers(mut self, headers: Vec<String>) -> Self {
53        self.cors_allowed_headers = headers;
54        self
55    }
56
57    /// Add custom configuration
58    pub fn with_custom(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
59        self.custom.insert(key.into(), value.into());
60        self
61    }
62}