-
Notifications
You must be signed in to change notification settings - Fork 10
Refactor configuration module to use confique for environment variable management #267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
danyi1212
merged 2 commits into
release/0.9.0
from
dan/per-12291-pdp-refactor-config-module
Jun 5, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| use confique::Config; | ||
| use serde::Deserialize; | ||
|
|
||
| /// Specifies which cache store implementation to use | ||
|
|
@@ -12,115 +13,37 @@ pub enum CacheStore { | |
| } | ||
|
|
||
| /// Configuration for the caching subsystem | ||
| #[derive(Debug, Deserialize, Clone)] | ||
| #[derive(Debug, Config, Clone, Default)] | ||
| pub struct CacheConfig { | ||
| /// Cache TTL in seconds (default: 1 hour) | ||
| #[serde(default)] | ||
| #[config(env = "PDP_CACHE_TTL", default = 3600)] | ||
| pub ttl: u32, | ||
|
|
||
| /// Cache store type: "in-memory", "redis", or null (default) | ||
| #[serde(default)] | ||
| #[config(env = "PDP_CACHE_STORE", default = "none")] | ||
| pub store: CacheStore, | ||
|
|
||
| /// In-memory cache specific configuration | ||
| #[serde(default)] | ||
| #[config(nested)] | ||
| pub memory: InMemoryConfig, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you used to have default of ::default() here, what will happen here ?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you used to have default of ::default() here, what will happen here ? |
||
|
|
||
| /// Redis cache specific configuration | ||
| #[serde(default)] | ||
| #[config(nested)] | ||
| pub redis: RedisConfig, | ||
| } | ||
|
|
||
| impl Default for CacheConfig { | ||
| fn default() -> Self { | ||
| Self { | ||
| ttl: 3600, // 1 hour | ||
| store: CacheStore::None, | ||
| memory: InMemoryConfig::default(), | ||
| redis: RedisConfig::default(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl CacheConfig { | ||
| /// Creates a new configuration from environment variables | ||
| pub fn from_env(config: &Self) -> Self { | ||
| // Start with the provided configuration | ||
| let mut result = config.clone(); | ||
|
|
||
| // Apply TTL environment variable | ||
| if let Ok(ttl) = std::env::var("PDP_CACHE_TTL") { | ||
| if let Ok(parsed) = ttl.parse::<u32>() { | ||
| result.ttl = parsed; | ||
| } | ||
| } | ||
|
|
||
| // Apply store type from environment | ||
| result.store = match std::env::var("PDP_CACHE_STORE").as_deref() { | ||
| Ok("in-memory") => CacheStore::InMemory, | ||
| Ok("redis") => CacheStore::Redis, | ||
| Ok("none") => CacheStore::None, | ||
| _ => result.store.clone(), // Keep existing value if not specified | ||
| }; | ||
|
|
||
| // Apply sub-configurations from environment | ||
| result.memory = InMemoryConfig::from_env(&result.memory); | ||
| result.redis = RedisConfig::from_env(&result.redis); | ||
|
|
||
| result | ||
| } | ||
| } | ||
|
|
||
| /// In-memory cache configuration options | ||
| #[derive(Debug, Deserialize, Clone)] | ||
| #[derive(Debug, Config, Clone, Default)] | ||
| pub struct InMemoryConfig { | ||
| /// Maximum capacity in MiB (default: 128 MiB) | ||
| #[serde(default)] | ||
| #[config(env = "PDP_CACHE_MEMORY_CAPACITY", default = 128)] | ||
| pub capacity: usize, | ||
| } | ||
|
|
||
| impl Default for InMemoryConfig { | ||
| fn default() -> Self { | ||
| Self { | ||
| capacity: 128, // 128 MiB | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl InMemoryConfig { | ||
| /// Creates a new configuration from environment variables | ||
| pub fn from_env(config: &Self) -> Self { | ||
| // Start with the provided configuration | ||
| let mut result = config.clone(); | ||
|
|
||
| if let Ok(capacity) = std::env::var("PDP_CACHE_MEMORY_CAPACITY") { | ||
| if let Ok(parsed) = capacity.parse::<usize>() { | ||
| result.capacity = parsed; | ||
| } | ||
| } | ||
|
|
||
| result | ||
| } | ||
| } | ||
|
|
||
| /// Redis cache configuration options | ||
| #[derive(Debug, Deserialize, Clone, Default)] | ||
| #[derive(Debug, Config, Clone, Default)] | ||
| pub struct RedisConfig { | ||
| /// Redis connection string | ||
| #[serde(default)] | ||
| #[config(env = "PDP_CACHE_REDIS_URL", default = "")] | ||
| pub url: String, | ||
| } | ||
|
|
||
| impl RedisConfig { | ||
| /// Creates a new configuration from environment variables | ||
| pub fn from_env(config: &Self) -> Self { | ||
| // Start with the provided configuration | ||
| let mut result = config.clone(); | ||
|
|
||
| if let Ok(url) = std::env::var("PDP_CACHE_REDIS_URL") { | ||
| result.url = url; | ||
| } | ||
|
|
||
| result | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there isn't a prefix implementation or auto env generation here ?