rush_sync_server/setup/
setup_toml.rs

1// Enhanced src/setup/setup_toml.rs
2use crate::core::prelude::*;
3use std::path::PathBuf;
4use tokio::fs;
5
6// Enhanced DEFAULT_CONFIG with server and logging sections
7const DEFAULT_CONFIG: &str = r#"[general]
8max_messages = 1000
9typewriter_delay = 5
10input_max_length = 100
11max_history = 30
12poll_rate = 16
13log_level = "info"
14current_theme = "dark"
15
16[language]
17current = "en"
18
19# =====================================================
20# SERVER MANAGEMENT CONFIGURATION
21# =====================================================
22[server]
23port_range_start = 8080      # Starting port for auto-allocation
24port_range_end = 8180        # Maximum port for auto-allocation
25max_concurrent = 10          # Maximum simultaneous servers
26shutdown_timeout = 5         # Graceful shutdown timeout (seconds)
27startup_delay_ms = 500       # Delay after server creation (milliseconds)
28workers = 1                  # Actix workers per server
29auto_open_browser = true     # Autostart Browser
30
31# =====================================================
32# LOGGING CONFIGURATION
33# =====================================================
34[logging]
35max_file_size_mb = 100       # Log rotation size (100MB per file)
36max_archive_files = 9        # Archive generations (9 backups)
37compress_archives = true     # GZIP compression for archives
38log_requests = true          # Enable request logging
39log_security_alerts = true  # Enable security monitoring
40log_performance = true       # Enable performance metrics
41
42# =====================================================
43# THEME DEFINITIONS
44# =====================================================
45[theme.dark]
46output_bg = "Black"
47output_text = "White"
48output_cursor = "PIPE"
49output_cursor_color = "White"
50input_bg = "White"
51input_text = "Black"
52input_cursor_prefix = "/// "
53input_cursor = "PIPE"
54input_cursor_color = "Black"
55
56[theme.light]
57output_bg = "White"
58output_text = "Black"
59output_cursor = "PIPE"
60output_cursor_color = "Black"
61input_bg = "Black"
62input_text = "White"
63input_cursor_prefix = "/// "
64input_cursor = "PIPE"
65input_cursor_color = "White"
66
67[theme.green]
68output_bg = "Black"
69output_text = "Green"
70output_cursor = "BLOCK"
71output_cursor_color = "Green"
72input_bg = "LightGreen"
73input_text = "Black"
74input_cursor_prefix = "$ "
75input_cursor = "BLOCK"
76input_cursor_color = "Black"
77
78[theme.blue]
79output_bg = "White"
80output_text = "LightBlue"
81output_cursor = "UNDERSCORE"
82output_cursor_color = "Blue"
83input_bg = "Blue"
84input_text = "White"
85input_cursor_prefix = "> "
86input_cursor = "UNDERSCORE"
87input_cursor_color = "White"
88"#;
89
90pub async fn ensure_config_exists() -> Result<PathBuf> {
91    let exe_path = std::env::current_exe().map_err(AppError::Io)?;
92    let base_dir = exe_path
93        .parent()
94        .ok_or_else(|| AppError::Validation(get_translation("system.config.dir_error", &[])))?;
95
96    let config_dir = base_dir.join(".rss");
97    if !config_dir.exists() {
98        fs::create_dir_all(&config_dir)
99            .await
100            .map_err(AppError::Io)?;
101    }
102
103    let config_path = config_dir.join("rush.toml");
104    if !config_path.exists() {
105        fs::write(&config_path, DEFAULT_CONFIG)
106            .await
107            .map_err(AppError::Io)?;
108
109        log::info!(
110            "{}",
111            get_translation(
112                "system.config.file_created",
113                &[&config_path.display().to_string()]
114            )
115        );
116    }
117
118    Ok(config_path)
119}
120
121pub fn get_config_paths() -> Vec<PathBuf> {
122    let mut paths = Vec::new();
123    if let Ok(exe_path) = std::env::current_exe() {
124        if let Some(base_dir) = exe_path.parent() {
125            paths.push(base_dir.join(".rss/rush.toml"));
126            paths.push(base_dir.join("rush.toml"));
127            paths.push(base_dir.join("config/rush.toml"));
128        }
129    }
130    #[cfg(debug_assertions)]
131    {
132        paths.push(PathBuf::from("rush.toml"));
133        paths.push(PathBuf::from("src/rush.toml"));
134    }
135    paths
136}