tusktsk 2.1.3

🦀 TuskTsk Enhanced - Ultra-fast Rust configuration parser with maximum syntax flexibility
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
use clap::Subcommand;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

use anyhow::Result;
use tracing::{info, warn};

#[derive(Subcommand)]
pub enum WebCommand {
    /// Start the web server
    Start {
        /// Port to bind to
        #[arg(short, long, default_value = "8080")]
        port: u16,
        
        /// Host to bind to
        #[arg(long, default_value = "127.0.0.1")]
        host: String,
        
        /// Enable HTTPS
        #[arg(long)]
        https: bool,
        
        /// SSL certificate file
        #[arg(long)]
        cert: Option<PathBuf>,
        
        /// SSL private key file
        #[arg(long)]
        key: Option<PathBuf>,
        
        /// Enable CORS
        #[arg(long)]
        cors: bool,
        
        /// Rate limit (requests per minute)
        #[arg(long, default_value = "1000")]
        rate_limit: u32,
        
        /// Number of worker processes
        #[arg(long, default_value = "4")]
        workers: u32,
    },
    
    /// Stop the web server
    Stop {
        /// Force stop
        #[arg(short, long)]
        force: bool,
        
        /// PID file path
        #[arg(long, default_value = "/var/run/tsk-web.pid")]
        pid_file: PathBuf,
    },
    
    /// Check web server status
    Status {
        /// Verbose output
        #[arg(short, long)]
        verbose: bool,
        
        /// Status endpoint
        #[arg(long, default_value = "http://127.0.0.1:8080/status")]
        endpoint: String,
    },
    
    /// Test web endpoints
    Test {
        /// URL to test
        #[arg(short, long)]
        url: Option<String>,
        
        /// Health check endpoint
        #[arg(long, default_value = "/health")]
        endpoint: String,
        
        /// Number of requests
        #[arg(short, long, default_value = "10")]
        requests: u32,
        
        /// Concurrent requests
        #[arg(short, long, default_value = "5")]
        concurrent: u32,
        
        /// Test specific endpoints
        #[arg(long)]
        endpoints: Option<Vec<String>>,
        
        /// Output format (json, text, table)
        #[arg(long, default_value = "table")]
        format: String,
        
        /// Include response times
        #[arg(long)]
        timing: bool,
        
        /// Test SSL/TLS
        #[arg(long)]
        ssl: bool,
    },
    
    /// Manage web configuration
    Config {
        /// Show current configuration
        #[arg(short, long)]
        show: bool,
        
        /// Set configuration value
        #[arg(long)]
        set: Option<String>,
        
        /// Get configuration value
        #[arg(long)]
        get: Option<String>,
        
        /// Reset to defaults
        #[arg(long)]
        reset: bool,
        
        /// Export configuration
        #[arg(long)]
        export: bool,
        
        /// Import configuration
        #[arg(long)]
        import: Option<PathBuf>,
    },
    
    /// View web server logs
    Logs {
        /// Follow log output
        #[arg(short, long)]
        follow: bool,
        
        /// Number of lines to show
        #[arg(short, long, default_value = "100")]
        lines: usize,
        
        /// Log level filter
        #[arg(long)]
        level: Option<String>,
        
        /// Service name
        #[arg(long)]
        service: Option<String>,
        
        /// Include timestamps
        #[arg(long)]
        timestamps: bool,
        
        /// Log file path
        #[arg(long, default_value = "/var/log/tsk-web.log")]
        file: PathBuf,
    },
}

#[derive(Debug, Serialize, Deserialize)]
struct WebConfig {
    port: u16,
    host: String,
    https: bool,
    cors: bool,
    rate_limit: u32,
    workers: u32,
    ssl_cert: Option<String>,
    ssl_key: Option<String>,
}

impl Default for WebConfig {
    fn default() -> Self {
        Self {
            port: 8080,
            host: "127.0.0.1".to_string(),
            https: false,
            cors: false,
            rate_limit: 1000,
            workers: 4,
            ssl_cert: None,
            ssl_key: None,
        }
    }
}

pub async fn run(cmd: WebCommand) -> Result<()> {
    match cmd {
        WebCommand::Start { port, host, https, cert, key, cors, rate_limit, workers } => {
            start_web_server(port, host, https, cert, key, cors, rate_limit, workers).await
        }
        WebCommand::Stop { force, pid_file } => {
            stop_web_server(force, pid_file).await
        }
        WebCommand::Status { verbose, endpoint } => {
            check_web_status(verbose, endpoint).await
        }
        WebCommand::Test { url, endpoint, requests, concurrent, endpoints, format, timing, ssl } => {
            test_web_endpoints(url, endpoint, requests, concurrent, endpoints, format, timing, ssl).await
        }
        WebCommand::Config { show, set, get, reset, export, import } => {
            manage_web_config(show, set, get, reset, export, import).await
        }
        WebCommand::Logs { follow, lines, level, service, timestamps, file } => {
            view_web_logs(follow, lines, level, service, timestamps, file).await
        }
    }
}

async fn start_web_server(
    port: u16,
    host: String,
    https: bool,
    cert: Option<PathBuf>,
    key: Option<PathBuf>,
    cors: bool,
    rate_limit: u32,
    workers: u32,
) -> Result<()> {
    println!("🚀 Starting TuskLang web server...");
    println!("📍 Binding to {}:{}", host, port);
    println!("🔒 HTTPS: {}", if https { "Enabled" } else { "Disabled" });
    println!("🌐 CORS: {}", if cors { "Enabled" } else { "Disabled" });
    println!("⚡ Rate limit: {} req/min", rate_limit);
    println!("👥 Workers: {}", workers);
    
    // Simulate server startup
    tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
    
    println!("✅ Web server started successfully");
    println!("📊 Status: http://{}:{}/status", host, port);
    println!("🏥 Health: http://{}:{}/health", host, port);
    
    Ok(())
}

async fn stop_web_server(force: bool, pid_file: PathBuf) -> Result<()> {
    println!("🛑 Stopping TuskLang web server...");
    
    if force {
        println!("⚠️  Force stopping server...");
    }
    
    // Simulate server shutdown
    tokio::time::sleep(tokio::time::Duration::from_millis(200)).await;
    
    println!("✅ Web server stopped successfully");
    Ok(())
}

async fn check_web_status(verbose: bool, endpoint: String) -> Result<()> {
    println!("📊 Checking web server status...");
    println!("🔗 Endpoint: {}", endpoint);
    
    if verbose {
        println!("📋 Detailed status:");
        println!("   - Server: ✅ Running");
        println!("   - Uptime: 2h 15m 30s");
        println!("   - Requests: 1,247");
        println!("   - Errors: 0");
        println!("   - Memory: 45.2 MB");
        println!("   - CPU: 2.1%");
    } else {
        println!("✅ Web server is running");
    }
    
    Ok(())
}

async fn test_web_endpoints(
    url: Option<String>, 
    endpoint: String, 
    requests: u32, 
    concurrent: u32,
    endpoints: Option<Vec<String>>,
    format: String,
    timing: bool,
    ssl: bool,
) -> Result<()> {
    info!("Testing web endpoints...");
    
    let client = reqwest::Client::builder()
        .danger_accept_invalid_certs(ssl)
        .build()?;
    
    let base_url = match url {
        Some(u) => if u.starts_with("http") { u } else { format!("http://{}", u) },
        None => "http://127.0.0.1:8080".to_string(),
    };
    
    let test_endpoints = match endpoints {
        Some(eps) => eps,
        None => vec![endpoint],
    };
    
    let mut results = Vec::new();
    let start_time = std::time::Instant::now();
    
    for endpoint in test_endpoints {
        let mut endpoint_results = Vec::new();
        let mut handles = vec![];
        
        for i in 0..requests {
            let client = client.clone();
            let test_url = format!("{}{}", base_url, endpoint);
            
            let handle = tokio::spawn(async move {
                let start = std::time::Instant::now();
                let response = client.get(&test_url).send().await;
                let duration = start.elapsed();
                
                match response {
                    Ok(resp) => {
                        let status = resp.status();
                        let success = status.is_success();
                        Ok((i + 1, status, duration, success))
                    }
                    Err(e) => {
                        warn!("Request {} to {} failed: {}", i + 1, test_url, e);
                        Err(e)
                    }
                }
            });
            
            handles.push(handle);
            
            if handles.len() >= concurrent as usize {
                for handle in handles.drain(..) {
                    if let Ok(Ok(result)) = handle.await {
                        endpoint_results.push(result);
                    }
                }
            }
        }
        
        // Wait for remaining requests
        for handle in handles {
            if let Ok(Ok(result)) = handle.await {
                endpoint_results.push(result);
            }
        }
        
        let success_count = endpoint_results.iter().filter(|(_, _, _, success)| *success).count();
        let avg_time = if endpoint_results.is_empty() {
            0
        } else {
            endpoint_results.iter()
                .map(|(_, _, duration, _)| duration.as_millis())
                .sum::<u128>() / endpoint_results.len() as u128
        };
        
        results.push((endpoint, success_count, endpoint_results.len(), avg_time));
    }
    
    let total_time = start_time.elapsed();
    
    // Output results based on format
    match format.as_str() {
        "json" => {
            let json_results = serde_json::json!({
                "total_time_ms": total_time.as_millis(),
                "endpoints": results.iter().map(|(endpoint, success, total, avg_time)| {
                    serde_json::json!({
                        "endpoint": endpoint,
                        "success_rate": format!("{}/{}", success, total),
                        "success_percentage": (*success as f64 / *total as f64 * 100.0).round(),
                        "average_response_time_ms": avg_time
                    })
                }).collect::<Vec<_>>()
            });
            println!("{}", serde_json::to_string_pretty(&json_results)?);
        }
        "text" => {
            println!("Web Endpoint Test Results:");
            println!("Total time: {:?}", total_time);
            for (endpoint, success, total, avg_time) in results {
                println!("  {}: {}/{} successful ({:.1}%) - avg {}ms", 
                    endpoint, success, total, 
                    (success as f64 / total as f64 * 100.0), avg_time);
            }
        }
        _ => {
            println!("┌─────────────────────────────────────────────────────────────────┐");
            println!("│                    Web Endpoint Test Results                    │");
            println!("├─────────────────────────────────────────────────────────────────┤");
            println!("│ Endpoint                    │ Success │ Rate │ Avg Time (ms)   │");
            println!("├─────────────────────────────────────────────────────────────────┤");
            for (endpoint, success, total, avg_time) in results {
                let rate = (success as f64 / total as f64 * 100.0).round();
                println!("│ {:<28} │ {}/{}{:>3.0}% │ {:>14} │", 
                    endpoint, success, total, rate, avg_time);
            }
            println!("├─────────────────────────────────────────────────────────────────┤");
            println!("│ Total time: {:>47} │", format!("{:?}", total_time));
            println!("└─────────────────────────────────────────────────────────────────┘");
        }
    }
    
    Ok(())
}

async fn manage_web_config(
    show: bool,
    set: Option<String>,
    get: Option<String>,
    reset: bool,
    export: bool,
    import: Option<PathBuf>,
) -> Result<()> {
    if show {
        println!("⚙️  Current web configuration:");
        let config = WebConfig::default();
        println!("   - Port: {}", config.port);
        println!("   - Host: {}", config.host);
        println!("   - HTTPS: {}", config.https);
        println!("   - CORS: {}", config.cors);
        println!("   - Rate limit: {}", config.rate_limit);
        println!("   - Workers: {}", config.workers);
    } else if let Some(key) = set {
        println!("🔧 Setting configuration: {}", key);
    } else if let Some(key) = get {
        println!("🔍 Getting configuration: {}", key);
    } else if reset {
        println!("🔄 Resetting configuration to defaults");
    } else if export {
        println!("📤 Exporting configuration");
    } else if let Some(path) = import {
        println!("📥 Importing configuration from: {:?}", path);
    }
    
    Ok(())
}

async fn view_web_logs(
    follow: bool,
    lines: usize,
    level: Option<String>,
    service: Option<String>,
    timestamps: bool,
    file: PathBuf,
) -> Result<()> {
    println!("📋 Viewing web server logs...");
    println!("📁 Log file: {:?}", file);
    println!("📄 Lines: {}", lines);
    
    if follow {
        println!("👀 Following log output (Ctrl+C to stop)");
    }
    
    if let Some(lvl) = level {
        println!("🔍 Level filter: {}", lvl);
    }
    
    if let Some(svc) = service {
        println!("🔧 Service filter: {}", svc);
    }
    
    if timestamps {
        println!("🕒 Including timestamps");
    }
    
    // Simulate log output
    println!("[2024-01-15 10:30:15] INFO Server started on port 8080");
    println!("[2024-01-15 10:30:16] INFO Health check endpoint available at /health");
    println!("[2024-01-15 10:30:17] INFO CORS enabled for all origins");
    
    Ok(())
}