feat(config): add drive config persistence to .env file

Added functionality to write drive configuration (MinIO settings) to a .env file when the AppConfig is created. The new write_drive_config_to_env function handles the file operations and logs warnings if the write fails. This change ensures drive configuration is persisted for future use while maintaining backward compatibility.
This commit is contained in:
Rodrigo Rodriguez (Pragmatismo) 2025-10-30 13:14:06 -03:00
parent e93935d5a3
commit b25b658cfd

View file

@ -3,6 +3,8 @@ use diesel::sql_types::Text;
use log::{info, warn}; use log::{info, warn};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::HashMap; use std::collections::HashMap;
use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
@ -223,6 +225,11 @@ impl AppConfig {
endpoint: get_str("AI_ENDPOINT", "https://api.openai.com"), endpoint: get_str("AI_ENDPOINT", "https://api.openai.com"),
}; };
// Write drive config to .env file
if let Err(e) = write_drive_config_to_env(&minio) {
warn!("Failed to write drive config to .env: {}", e);
}
AppConfig { AppConfig {
drive: minio, drive: minio,
server: ServerConfig { server: ServerConfig {
@ -374,6 +381,22 @@ impl AppConfig {
} }
} }
fn write_drive_config_to_env(drive: &DriveConfig) -> std::io::Result<()> {
let mut file = OpenOptions::new()
.append(true)
.create(true)
.open(".env")?;
writeln!(file,"")?;
writeln!(file, "DRIVE_SERVER={}", drive.server)?;
writeln!(file, "DRIVE_ACCESSKEY={}", drive.access_key)?;
writeln!(file, "DRIVE_SECRET={}", drive.secret_key)?;
writeln!(file, "DRIVE_USE_SSL={}", drive.use_ssl)?;
writeln!(file, "DRIVE_ORG_PREFIX={}", drive.org_prefix)?;
Ok(())
}
fn parse_database_url(url: &str) -> (String, String, String, u32, String) { fn parse_database_url(url: &str) -> (String, String, String, u32, String) {
if let Some(stripped) = url.strip_prefix("postgres://") { if let Some(stripped) = url.strip_prefix("postgres://") {
let parts: Vec<&str> = stripped.split('@').collect(); let parts: Vec<&str> = stripped.split('@').collect();