use serde::{Deserialize, Serialize}; use std::fs; use std::path::PathBuf; use chrono::{DateTime, Utc}; #[derive(Debug, Serialize, Deserialize)] pub struct ServiceStatus { pub name: String, pub status: String, pub port: Option, pub pid: Option, } #[derive(Debug, Serialize, Deserialize)] pub struct BackupInfo { pub path: String, pub size: u64, pub created_at: DateTime, } #[derive(Debug, Serialize, Deserialize)] pub struct UserInfo { pub user_id: String, pub username: String, pub created_at: DateTime, } #[tauri::command] pub async fn start_all_services() -> Result<(), String> { Ok(()) } #[tauri::command] pub async fn stop_all_services() -> Result<(), String> { Ok(()) } #[tauri::command] pub async fn restart_all_services() -> Result<(), String> { stop_all_services().await?; tokio::time::sleep(tokio::time::Duration::from_secs(2)).await; start_all_services().await?; Ok(()) } #[tauri::command] pub async fn get_service_status() -> Result, String> { let services = vec![ ServiceStatus { name: "Web Server".to_string(), status: "Running".to_string(), port: Some(11438), pid: Some(1234), }, ServiceStatus { name: "SSH Server".to_string(), status: "Stopped".to_string(), port: Some(2222), pid: None, }, ServiceStatus { name: "NFS Server".to_string(), status: "Stopped".to_string(), port: None, pid: None, }, ServiceStatus { name: "SMB Server".to_string(), status: "Stopped".to_string(), port: None, pid: None, }, ]; Ok(services) } #[tauri::command] pub async fn create_backup() -> Result { let backup_dir = PathBuf::from("data/backups"); fs::create_dir_all(&backup_dir) .map_err(|e| format!("Failed to create backup directory: {}", e))?; let timestamp = Utc::now().format("%Y%m%d_%H%M%S"); let backup_path = backup_dir.join(format!("backup_{}.sqlite", timestamp)); let db_path = PathBuf::from("data/users/demo.sqlite"); if db_path.exists() { fs::copy(&db_path, &backup_path) .map_err(|e| format!("Failed to create backup: {}", e))?; Ok(backup_path.to_string_lossy().to_string()) } else { Err("Database file not found".to_string()) } } #[tauri::command] pub async fn restore_backup(backup_path: String) -> Result<(), String> { let backup = PathBuf::from(&backup_path); if !backup.exists() { return Err(format!("Backup file not found: {}", backup_path)); } let db_path = PathBuf::from("data/users/demo.sqlite"); fs::copy(&backup, &db_path) .map_err(|e| format!("Failed to restore backup: {}", e))?; Ok(()) } #[tauri::command] pub async fn list_backups() -> Result, String> { let backup_dir = PathBuf::from("data/backups"); if !backup_dir.exists() { return Ok(vec![]); } let mut backups = vec![]; let entries = fs::read_dir(&backup_dir) .map_err(|e| format!("Failed to read backup directory: {}", e))?; for entry in entries { let entry = entry.map_err(|e| format!("Failed to read backup entry: {}", e))?; let path = entry.path(); if path.extension().map(|ext| ext == "sqlite").unwrap_or(false) { let metadata = entry.metadata() .map_err(|e| format!("Failed to read backup metadata: {}", e))?; let created_at: DateTime = metadata.created() .map(|time| time.into()) .unwrap_or_else(|_| Utc::now()); backups.push(BackupInfo { path: path.to_string_lossy().to_string(), size: metadata.len(), created_at, }); } } backups.sort_by(|a, b| b.created_at.cmp(&a.created_at)); Ok(backups) } #[tauri::command] pub async fn list_users() -> Result, String> { let users_dir = PathBuf::from("data/users"); if !users_dir.exists() { return Ok(vec![]); } let mut users = vec![]; let entries = fs::read_dir(&users_dir) .map_err(|e| format!("Failed to read users directory: {}", e))?; for entry in entries { let entry = entry.map_err(|e| format!("Failed to read user entry: {}", e))?; let path = entry.path(); if path.extension().map(|ext| ext == "sqlite").unwrap_or(false) { if let Some(stem) = path.file_stem() { let user_id = stem.to_string_lossy().to_string(); let metadata = entry.metadata() .map_err(|e| format!("Failed to read user metadata: {}", e))?; let created_at: DateTime = metadata.created() .map(|time| time.into()) .unwrap_or_else(|_| Utc::now()); users.push(UserInfo { user_id: user_id.clone(), username: user_id, created_at, }); } } } Ok(users) }