config.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. use serde::{Deserialize, Serialize};
  2. pub const ENV_CDK_MINTD_CACHE_BACKEND: &str = "CDK_MINTD_CACHE_BACKEND";
  3. #[cfg(feature = "redis")]
  4. pub const ENV_CDK_MINTD_CACHE_REDIS_URL: &str = "CDK_MINTD_CACHE_REDIS_URL";
  5. #[cfg(feature = "redis")]
  6. pub const ENV_CDK_MINTD_CACHE_REDIS_KEY_PREFIX: &str = "CDK_MINTD_CACHE_REDIS_KEY_PREFIX";
  7. pub const ENV_CDK_MINTD_CACHE_TTI: &str = "CDK_MINTD_CACHE_TTI";
  8. pub const ENV_CDK_MINTD_CACHE_TTL: &str = "CDK_MINTD_CACHE_TTL";
  9. #[derive(Debug, Clone, Serialize, Deserialize, Default)]
  10. #[serde(tag = "backend")]
  11. #[serde(rename_all = "lowercase")]
  12. pub enum Backend {
  13. #[default]
  14. Memory,
  15. #[cfg(feature = "redis")]
  16. Redis(super::backend::RedisConfig),
  17. }
  18. impl Backend {
  19. pub fn from_env_str(backend_str: &str) -> Option<Self> {
  20. match backend_str.to_lowercase().as_str() {
  21. "memory" => Some(Self::Memory),
  22. #[cfg(feature = "redis")]
  23. "redis" => {
  24. // Get Redis configuration from environment
  25. let connection_string = std::env::var(ENV_CDK_MINTD_CACHE_REDIS_URL)
  26. .unwrap_or_else(|_| "redis://127.0.0.1:6379".to_string());
  27. let key_prefix = std::env::var(ENV_CDK_MINTD_CACHE_REDIS_KEY_PREFIX).ok();
  28. Some(Self::Redis(super::backend::RedisConfig {
  29. connection_string,
  30. key_prefix,
  31. }))
  32. }
  33. _ => None,
  34. }
  35. }
  36. }
  37. /// Cache configuration.
  38. #[derive(Debug, Clone, Serialize, Deserialize, Default)]
  39. pub struct Config {
  40. /// Cache backend.
  41. #[serde(default)]
  42. #[serde(flatten)]
  43. pub backend: Backend,
  44. /// Time to live for the cache entries.
  45. pub ttl: Option<u64>,
  46. /// Time for the cache entries to be idle.
  47. pub tti: Option<u64>,
  48. }
  49. impl Config {
  50. /// Config from env
  51. pub fn from_env(mut self) -> Self {
  52. use std::env;
  53. // Parse backend
  54. if let Ok(backend_str) = env::var(ENV_CDK_MINTD_CACHE_BACKEND) {
  55. if let Some(backend) = Backend::from_env_str(&backend_str) {
  56. self.backend = backend;
  57. // If Redis backend is selected, parse Redis configuration
  58. #[cfg(feature = "redis")]
  59. if matches!(self.backend, Backend::Redis(_)) {
  60. let connection_string = env::var(ENV_CDK_MINTD_CACHE_REDIS_URL)
  61. .unwrap_or_else(|_| "redis://127.0.0.1:6379".to_string());
  62. let key_prefix = env::var(ENV_CDK_MINTD_CACHE_REDIS_KEY_PREFIX).ok();
  63. self.backend = Backend::Redis(super::backend::RedisConfig {
  64. connection_string,
  65. key_prefix,
  66. });
  67. }
  68. }
  69. }
  70. // Parse TTL
  71. if let Ok(ttl_str) = env::var(ENV_CDK_MINTD_CACHE_TTL) {
  72. if let Ok(ttl) = ttl_str.parse() {
  73. self.ttl = Some(ttl);
  74. }
  75. }
  76. // Parse TTI
  77. if let Ok(tti_str) = env::var(ENV_CDK_MINTD_CACHE_TTI) {
  78. if let Ok(tti) = tti_str.parse() {
  79. self.tti = Some(tti);
  80. }
  81. }
  82. self
  83. }
  84. }