rustf 1.0.0

A simple MVC web framework inspired by Total.js
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! Built-in event handlers for common startup tasks
//!
//! Provides ready-to-use event handlers for database seeding, directory setup,
//! cleanup operations, and other common application initialization tasks.

use super::EventContext;
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime};
use tokio::fs;

/// Database seeder event handler
///
/// Automatically runs SQL seed files from a directory when the application starts.
///
/// # Example
/// ```rust,ignore
/// app.on("ready", database_seeder("seeds/"));
/// ```
pub fn database_seeder(
    seed_dir: impl Into<PathBuf>,
) -> impl Fn(
    EventContext,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = crate::Result<()>> + Send>>
       + Send
       + Sync
       + 'static {
    let seed_dir = seed_dir.into();

    move |ctx| {
        let seed_dir = seed_dir.clone();
        Box::pin(async move {
            // Only run seeds in development by default
            if !ctx.is_development() {
                log::info!("Skipping database seeding (not in development mode)");
                return Ok(());
            }

            log::info!("Running database seeds from: {:?}", seed_dir);

            if !seed_dir.exists() {
                log::warn!("Seed directory does not exist: {:?}", seed_dir);
                return Ok(());
            }

            let mut entries = fs::read_dir(&seed_dir).await?;
            let mut seed_files = Vec::new();

            while let Some(entry) = entries.next_entry().await? {
                let path = entry.path();
                if path.extension().and_then(|s| s.to_str()) == Some("sql") {
                    seed_files.push(path);
                }
            }

            // Sort files to ensure consistent execution order
            seed_files.sort();

            for seed_file in seed_files {
                log::info!("Executing seed file: {:?}", seed_file.file_name());
                let _sql = fs::read_to_string(&seed_file).await?;

                // Execute the SQL
                // NOTE: Awaiting DB::execute method implementation for direct SQL execution
                // Currently logs the action for debugging purposes
                log::info!("Would execute SQL from: {:?}", seed_file.file_name());
            }

            log::info!("Database seeding completed");

            Ok(())
        })
    }
}

/// Directory setup event handler
///
/// Ensures required directories exist with proper permissions.
///
/// # Example
/// ```rust,ignore
/// app.on("startup", directory_setup(&["uploads", "temp", "logs", "cache"]));
/// ```
pub fn directory_setup(
    directories: &'static [&'static str],
) -> impl Fn(
    EventContext,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = crate::Result<()>> + Send>>
       + Send
       + Sync
       + 'static {
    move |_ctx| {
        Box::pin(async move {
            log::info!("Setting up application directories");

            for dir in directories {
                let path = Path::new(dir);

                if !path.exists() {
                    log::info!("Creating directory: {}", dir);
                    fs::create_dir_all(path).await?;

                    // Set permissions on Unix systems
                    #[cfg(unix)]
                    {
                        use std::os::unix::fs::PermissionsExt;
                        let mut perms = fs::metadata(path).await?.permissions();
                        perms.set_mode(0o755); // rwxr-xr-x
                        fs::set_permissions(path, perms).await?;
                    }
                }
            }

            log::info!("Directory setup completed");
            Ok(())
        })
    }
}

/// Cleanup manager event handler
///
/// Removes old temporary files and performs other cleanup tasks.
///
/// # Example
/// ```rust,ignore
/// app.on("startup", cleanup_manager("temp/", Duration::from_secs(86400))); // 24 hours
/// ```
pub fn cleanup_manager(
    temp_dir: impl Into<PathBuf>,
    max_age: Duration,
) -> impl Fn(
    EventContext,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = crate::Result<()>> + Send>>
       + Send
       + Sync
       + 'static {
    let temp_dir = temp_dir.into();

    move |_ctx| {
        let temp_dir = temp_dir.clone();
        Box::pin(async move {
            log::info!("Running cleanup manager");

            if !temp_dir.exists() {
                return Ok(());
            }

            let mut entries = fs::read_dir(&temp_dir).await?;
            let now = SystemTime::now();
            let mut removed_count = 0;

            while let Some(entry) = entries.next_entry().await? {
                let path = entry.path();
                let metadata = match entry.metadata().await {
                    Ok(m) => m,
                    Err(e) => {
                        log::warn!("Failed to get metadata for {:?}: {}", path, e);
                        continue;
                    }
                };

                if let Ok(modified) = metadata.modified() {
                    if let Ok(age) = now.duration_since(modified) {
                        if age > max_age {
                            if metadata.is_dir() {
                                if let Err(e) = fs::remove_dir_all(&path).await {
                                    log::warn!("Failed to remove directory {:?}: {}", path, e);
                                }
                            } else if let Err(e) = fs::remove_file(&path).await {
                                log::warn!("Failed to remove file {:?}: {}", path, e);
                            }

                            removed_count += 1;
                        }
                    }
                }
            }

            log::info!(
                "Cleanup completed: removed {} old files/directories",
                removed_count
            );
            Ok(())
        })
    }
}

/// Log rotation event handler
///
/// Rotates log files to prevent them from growing too large.
///
/// # Example
/// ```rust,ignore
/// app.on("startup", log_rotation("logs/", 10)); // Keep 10 old log files
/// ```
pub fn log_rotation(
    log_dir: impl Into<PathBuf>,
    max_files: usize,
) -> impl Fn(
    EventContext,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = crate::Result<()>> + Send>>
       + Send
       + Sync
       + 'static {
    let log_dir = log_dir.into();

    move |_ctx| {
        let log_dir = log_dir.clone();
        Box::pin(async move {
            log::info!("Running log rotation");

            if !log_dir.exists() {
                return Ok(());
            }

            let mut entries = fs::read_dir(&log_dir).await?;
            let mut log_files = Vec::new();

            while let Some(entry) = entries.next_entry().await? {
                let path = entry.path();
                if path.extension().and_then(|s| s.to_str()) == Some("log") {
                    if let Ok(metadata) = entry.metadata().await {
                        if let Ok(modified) = metadata.modified() {
                            log_files.push((path, modified));
                        }
                    }
                }
            }

            // Sort by modification time (oldest first)
            log_files.sort_by_key(|(_, time)| *time);

            // Remove old log files if we have too many
            if log_files.len() > max_files {
                let to_remove = log_files.len() - max_files;
                for (path, _) in log_files.iter().take(to_remove) {
                    log::info!("Removing old log file: {:?}", path.file_name());
                    if let Err(e) = fs::remove_file(path).await {
                        log::warn!("Failed to remove log file {:?}: {}", path, e);
                    }
                }
            }

            log::info!("Log rotation completed");
            Ok(())
        })
    }
}

/// Environment check event handler
///
/// Validates that the application is running in the expected environment.
///
/// # Example
/// ```rust,ignore
/// app.on("startup", environment_check("production"));
/// ```
pub fn environment_check(
    expected_env: &'static str,
) -> impl Fn(
    EventContext,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = crate::Result<()>> + Send>>
       + Send
       + Sync
       + 'static {
    move |ctx| {
        Box::pin(async move {
            let current_env = ctx.env();

            if current_env != expected_env {
                log::warn!(
                    "Environment mismatch! Expected: '{}', Current: '{}'",
                    expected_env,
                    current_env
                );

                // You might want to fail hard in production
                if expected_env == "production" {
                    return Err(crate::error::Error::internal(format!(
                        "Application configured for production but running in '{}' mode",
                        current_env
                    )));
                }
            } else {
                log::info!("Environment check passed: {}", current_env);
            }

            Ok(())
        })
    }
}

/// Configuration validator event handler
///
/// Validates critical configuration settings at startup.
///
/// # Example
/// ```rust,ignore
/// app.on("config.loaded", configuration_validator);
/// ```
pub fn configuration_validator(
    ctx: EventContext,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = crate::Result<()>> + Send>> {
    Box::pin(async move {
        log::info!("Validating configuration");

        let config = &ctx.config;

        // Check for common configuration issues
        if ctx.is_production() {
            // Ensure database URL is configured
            if config.database.url.is_none() {
                return Err(crate::error::Error::internal(
                    "Database URL not configured for production".to_string(),
                ));
            }
        }

        // Validate directories exist or can be created
        let dirs_to_check = vec![&config.views.directory, &config.static_files.directory];

        for dir in dirs_to_check {
            let path = Path::new(dir);
            if !path.exists() {
                log::warn!("Directory does not exist and will be created: {}", dir);
            }
        }

        log::info!("Configuration validation completed");
        Ok(())
    })
}

/// Health check event handler
///
/// Performs basic health checks on application startup.
///
/// # Example
/// ```rust,ignore
/// app.on("ready", health_check);
/// ```
pub fn health_check(
    _ctx: EventContext,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = crate::Result<()>> + Send>> {
    Box::pin(async move {
        log::info!("Running health checks");

        // Check database connectivity
        #[cfg(feature = "database")]
        {
            use crate::db::DB;
            match DB::ping().await {
                Ok(true) => log::info!("✓ Database health check passed"),
                Ok(false) => log::info!("âš  Database not configured (skipping health check)"),
                Err(e) => {
                    log::error!("✗ Database health check failed: {}", e);
                    return Err(crate::error::Error::internal(format!(
                        "Database health check failed: {}",
                        e
                    )));
                }
            }
        }

        // Check disk space (simplified check - placeholder for user implementation)
        log::info!("✓ Disk space check (placeholder)");

        // Check memory usage (simplified - placeholder for user implementation)
        log::info!("✓ Memory usage check (placeholder)");

        log::info!("Health checks completed");
        Ok(())
    })
}

/// Log shutdown event handler
///
/// Logs shutdown notification with timing information.
///
/// # Example
/// ```rust,ignore
/// app.on("shutdown", log_shutdown);
/// ```
pub fn log_shutdown(
    ctx: EventContext,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = crate::Result<()>> + Send>> {
    Box::pin(async move {
        let shutdown_timeout = ctx.config.server.shutdown_timeout;
        log::info!(
            "🛑 Shutting down {} application (timeout: {}s)",
            ctx.env(),
            shutdown_timeout
        );

        // Log current connections or pending tasks if available
        log::info!("Waiting for active connections to close...");

        Ok(())
    })
}

/// Flush logs event handler
///
/// Ensures all buffered log messages are written before shutdown.
///
/// # Example
/// ```rust,ignore
/// app.on_priority("shutdown", 100, flush_logs); // Run late in shutdown
/// ```
pub fn flush_logs(
    _ctx: EventContext,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = crate::Result<()>> + Send>> {
    Box::pin(async move {
        log::info!("Flushing log buffers...");

        // Force flush of log messages
        // Note: Most log implementations auto-flush, but this ensures it
        log::logger().flush();

        // Give a small delay to ensure writes complete
        tokio::time::sleep(std::time::Duration::from_millis(50)).await;

        Ok(())
    })
}

/// Save application state event handler
///
/// Saves critical application state that should persist across restarts.
///
/// # Example
/// ```rust,ignore
/// app.on_priority("shutdown", -50, save_state("state.json")); // Run early
/// ```
pub fn save_state(
    state_file: impl Into<PathBuf>,
) -> impl Fn(
    EventContext,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = crate::Result<()>> + Send>>
       + Send
       + Sync
       + 'static {
    let state_file = state_file.into();

    move |_ctx| {
        let state_file = state_file.clone();
        Box::pin(async move {
            log::info!("Saving application state to {:?}", state_file);

            // Create state directory if needed
            if let Some(parent) = state_file.parent() {
                if !parent.exists() {
                    fs::create_dir_all(parent).await?;
                }
            }

            // Here you would serialize and save your application state
            // For now, we'll just write a timestamp
            let state_data = format!(
                "{{\"shutdown_at\": \"{:?}\", \"version\": \"1.0.0\"}}\n",
                SystemTime::now()
            );

            fs::write(&state_file, state_data).await?;
            log::info!("Application state saved successfully");

            Ok(())
        })
    }
}

/// Cleanup temporary files on shutdown
///
/// Removes temporary files and directories during shutdown.
///
/// # Example
/// ```rust,ignore
/// app.on("shutdown", cleanup_temp_files("temp/"));
/// ```
pub fn cleanup_temp_files(
    temp_dir: impl Into<PathBuf>,
) -> impl Fn(
    EventContext,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = crate::Result<()>> + Send>>
       + Send
       + Sync
       + 'static {
    let temp_dir = temp_dir.into();

    move |ctx| {
        let temp_dir = temp_dir.clone();
        Box::pin(async move {
            // Only clean temp files in development by default
            if !ctx.is_development() {
                return Ok(());
            }

            log::info!("Cleaning up temporary files in {:?}", temp_dir);

            if !temp_dir.exists() {
                return Ok(());
            }

            // Remove the entire temp directory in development
            if let Err(e) = fs::remove_dir_all(&temp_dir).await {
                log::warn!("Failed to remove temp directory: {}", e);
            } else {
                log::info!("Temporary files cleaned up");
            }

            Ok(())
        })
    }
}

/// Notification event handler for shutdown
///
/// Sends notifications to external services about shutdown.
///
/// # Example
/// ```rust,ignore
/// app.on_priority("shutdown", -100, notify_shutdown("https://api.example.com/webhook"));
/// ```
pub fn notify_shutdown(
    webhook_url: &'static str,
) -> impl Fn(
    EventContext,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = crate::Result<()>> + Send>>
       + Send
       + Sync
       + 'static {
    move |ctx| {
        Box::pin(async move {
            log::info!("Notifying external services of shutdown");

            // Placeholder for actual implementation
            // You might use reqwest or similar to send the notification
            let _ = (webhook_url, ctx.env()); // Suppress unused variable warnings

            Ok(())
        })
    }
}