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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
//! Custom error pages and production error handling
//!
//! This module provides custom error page templates and production-safe error responses
//! that don't leak sensitive information like stack traces in production environments.

use crate::config::AppConfig;
use crate::error::{Error, Result};
use crate::http::Response;
use crate::views::ViewEngine;
use hyper::StatusCode;
use serde_json::{json, Value};
use std::sync::Arc;

/// Error page configuration and rendering
pub struct ErrorPages {
    view_engine: Arc<ViewEngine>,
    _config: Arc<AppConfig>,
    development_mode: bool,
}

impl ErrorPages {
    /// Create new error page handler
    pub fn new(view_engine: Arc<ViewEngine>, config: Arc<AppConfig>) -> Self {
        // Detailed error pages follow the application's environment, not the
        // build profile: a debug build configured for production must not
        // show internals.
        let development_mode = config.environment.is_development();

        Self {
            view_engine,
            _config: config,
            development_mode,
        }
    }

    /// Render a custom error page
    pub fn render_error_page(
        &self,
        status_code: u16,
        error: Option<&Error>,
        request_id: Option<&str>,
    ) -> Result<Response> {
        let template_name = self.get_error_template(status_code);
        let error_data = self.prepare_error_data(status_code, error, request_id);

        // Try to render custom error template
        match self
            .view_engine
            .render(&template_name, &error_data, Some("error"))
        {
            Ok(html) => Ok(Response::new(
                StatusCode::from_u16(status_code).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR),
            )
            .with_header("Content-Type", "text/html; charset=utf-8")
            .with_body(html.into_bytes())),
            Err(_) => {
                // Fallback to built-in error page if custom template fails
                log::warn!(
                    "Failed to render custom error template: {}, using fallback",
                    template_name
                );
                self.render_fallback_error_page(status_code, error, request_id)
            }
        }
    }

    /// Get the template name for a given status code
    fn get_error_template(&self, status_code: u16) -> String {
        match status_code {
            400 => "errors/400".to_string(),
            401 => "errors/401".to_string(),
            403 => "errors/403".to_string(),
            404 => "errors/404".to_string(),
            409 => "errors/409".to_string(),
            429 => "errors/429".to_string(),
            500 => "errors/500".to_string(),
            502 => "errors/502".to_string(),
            503 => "errors/503".to_string(),
            _ => "errors/generic".to_string(),
        }
    }

    /// Prepare error data for template rendering
    fn prepare_error_data(
        &self,
        status_code: u16,
        error: Option<&Error>,
        request_id: Option<&str>,
    ) -> Value {
        let status_text = self.get_status_text(status_code);
        let user_message = self.get_user_friendly_message(status_code);

        let mut data = json!({
            "status_code": status_code,
            "status_text": status_text,
            "message": user_message,
            "request_id": request_id.unwrap_or("unknown"),
            "timestamp": chrono::Utc::now().to_rfc3339(),
            "development_mode": self.development_mode
        });

        // Include error details only in development mode
        if self.development_mode {
            if let Some(err) = error {
                data["error_details"] = json!({
                    "message": err.to_string(),
                    "type": self.classify_error_type(err),
                });
            }
        }

        data
    }

    /// Render a fallback error page when custom templates fail
    fn render_fallback_error_page(
        &self,
        status_code: u16,
        error: Option<&Error>,
        request_id: Option<&str>,
    ) -> Result<Response> {
        let status_text = self.get_status_text(status_code);
        let user_message = self.get_user_friendly_message(status_code);
        let request_id = request_id.unwrap_or("unknown");

        let error_details = if self.development_mode && error.is_some() {
            format!(
                "<div class=\"error-details\">
                <h3>Development Mode - Error Details:</h3>
                <pre>{}</pre>
                <p><strong>Error Type:</strong> {}</p>
            </div>",
                html_escape(&error.unwrap().to_string()),
                self.classify_error_type(error.unwrap())
            )
        } else {
            String::new()
        };

        let html = format!(
            r#"<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{} - {}</title>
    <style>
        body {{
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            margin: 0;
            padding: 0;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }}
        .error-container {{
            background: rgba(255, 255, 255, 0.95);
            border-radius: 10px;
            padding: 2rem;
            max-width: 600px;
            margin: 1rem;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
            text-align: center;
        }}
        .error-code {{
            font-size: 6rem;
            font-weight: bold;
            color: #e74c3c;
            margin: 0;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
        }}
        .error-title {{
            font-size: 1.5rem;
            color: #2c3e50;
            margin: 0.5rem 0;
        }}
        .error-message {{
            font-size: 1rem;
            color: #7f8c8d;
            margin: 1rem 0;
            line-height: 1.6;
        }}
        .error-details {{
            background: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 5px;
            padding: 1rem;
            margin: 1rem 0;
            text-align: left;
        }}
        .error-details pre {{
            background: #343a40;
            color: #f8f9fa;
            padding: 1rem;
            border-radius: 3px;
            overflow-x: auto;
            font-size: 0.9rem;
        }}
        .request-info {{
            font-size: 0.8rem;
            color: #95a5a6;
            margin-top: 2rem;
            padding-top: 1rem;
            border-top: 1px solid #ecf0f1;
        }}
        .back-button {{
            display: inline-block;
            background: #3498db;
            color: white;
            text-decoration: none;
            padding: 0.75rem 1.5rem;
            border-radius: 5px;
            margin-top: 1rem;
            transition: background 0.3s;
        }}
        .back-button:hover {{
            background: #2980b9;
        }}
    </style>
</head>
<body>
    <div class="error-container">
        <h1 class="error-code">{}</h1>
        <h2 class="error-title">{}</h2>
        <p class="error-message">{}</p>
        {}
        <a href="javascript:history.back()" class="back-button">Go Back</a>
        <div class="request-info">
            Request ID: {} | Timestamp: {}
        </div>
    </div>
</body>
</html>"#,
            status_code,
            status_text,
            status_code,
            status_text,
            user_message,
            error_details,
            request_id,
            chrono::Utc::now().format("%Y-%m-%d %H:%M:%S UTC")
        );

        Ok(Response::new(
            StatusCode::from_u16(status_code).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR),
        )
        .with_header("Content-Type", "text/html; charset=utf-8")
        .with_body(html.into_bytes()))
    }

    /// Get human-readable status text for HTTP status codes
    fn get_status_text(&self, status_code: u16) -> &'static str {
        match status_code {
            400 => "Bad Request",
            401 => "Unauthorized",
            403 => "Forbidden",
            404 => "Not Found",
            409 => "Conflict",
            429 => "Too Many Requests",
            500 => "Internal Server Error",
            502 => "Bad Gateway",
            503 => "Service Unavailable",
            _ => "Error",
        }
    }

    /// Get user-friendly error message for status codes
    fn get_user_friendly_message(&self, status_code: u16) -> &'static str {
        match status_code {
            400 => "The request could not be understood by the server. Please check your input and try again.",
            401 => "You need to authenticate to access this resource. Please login and try again.",
            403 => "You don't have permission to access this resource.",
            404 => "The page or resource you're looking for could not be found. It may have been moved or deleted.",
            409 => "There was a conflict with your request. The resource may have been modified by another user.",
            429 => "Too many requests have been made. Please wait a moment and try again.",
            500 => "An internal server error occurred. Our team has been notified and is working to fix the issue.",
            502 => "Bad gateway error. The server is temporarily unavailable.",
            503 => "The service is temporarily unavailable. Please try again later.",
            _ => "An error occurred while processing your request.",
        }
    }

    /// Classify error type for development debugging
    fn classify_error_type(&self, error: &Error) -> &'static str {
        let error_str = error.to_string().to_lowercase();

        if error_str.contains("database") || error_str.contains("sql") {
            "Database Error"
        } else if error_str.contains("network") || error_str.contains("connection") {
            "Network Error"
        } else if error_str.contains("template") || error_str.contains("view") {
            "Template Error"
        } else if error_str.contains("validation") || error_str.contains("invalid") {
            "Validation Error"
        } else if error_str.contains("permission") || error_str.contains("unauthorized") {
            "Authorization Error"
        } else if error_str.contains("not found") || error_str.contains("missing") {
            "Resource Not Found"
        } else {
            "Application Error"
        }
    }

    /// Create a JSON error response for API endpoints
    pub fn create_json_error_response(
        &self,
        status_code: u16,
        error: Option<&Error>,
        request_id: Option<&str>,
    ) -> Result<Response> {
        let user_message = self.get_user_friendly_message(status_code);

        let mut error_data = json!({
            "error": true,
            "status": status_code,
            "message": user_message,
            "request_id": request_id.unwrap_or("unknown"),
            "timestamp": chrono::Utc::now().to_rfc3339()
        });

        // Include detailed error information only in development mode
        if self.development_mode {
            if let Some(err) = error {
                error_data["details"] = json!({
                    "error_message": err.to_string(),
                    "error_type": self.classify_error_type(err),
                });
            }
        }

        Ok(Response::new(
            StatusCode::from_u16(status_code).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR),
        )
        .with_header("Content-Type", "application/json")
        .with_body(serde_json::to_string(&error_data)?.into_bytes()))
    }

    /// Create appropriate error response based on request Accept header
    pub fn create_error_response(
        &self,
        status_code: u16,
        error: Option<&Error>,
        request_id: Option<&str>,
        accept_header: Option<&str>,
    ) -> Result<Response> {
        if let Some(accept) = accept_header {
            if accept.contains("application/json") {
                return self.create_json_error_response(status_code, error, request_id);
            }
        }

        // Default to HTML error page
        self.render_error_page(status_code, error, request_id)
    }
}

/// HTML escape utility function
fn html_escape(input: &str) -> String {
    input
        .replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
        .replace('\'', "&#x27;")
}

/// Health check endpoint implementation
pub struct HealthCheck {
    // Only read by the database connectivity check, which is feature-gated.
    #[cfg_attr(not(feature = "database"), allow(dead_code))]
    config: Arc<AppConfig>,
}

impl HealthCheck {
    pub fn new(config: Arc<AppConfig>) -> Self {
        Self { config }
    }

    /// Perform basic health check
    pub async fn check_health(&self) -> HealthCheckResult {
        let mut result = HealthCheckResult {
            status: "healthy".to_string(),
            timestamp: chrono::Utc::now().to_rfc3339(),
            version: env!("CARGO_PKG_VERSION").to_string(),
            checks: std::collections::HashMap::new(),
        };

        // Check database connectivity if configured
        #[cfg(feature = "database")]
        if self.config.database.url.is_some() {
            match self.check_database_health().await {
                Ok(_) => {
                    result.checks.insert(
                        "database".to_string(),
                        CheckStatus {
                            status: "healthy".to_string(),
                            message: Some("Database connection successful".to_string()),
                        },
                    );
                }
                Err(e) => {
                    result.status = "unhealthy".to_string();
                    result.checks.insert(
                        "database".to_string(),
                        CheckStatus {
                            status: "unhealthy".to_string(),
                            message: Some(format!("Database connection failed: {}", e)),
                        },
                    );
                }
            }
        }

        // Check memory usage
        result
            .checks
            .insert("memory".to_string(), self.check_memory_usage());

        result
    }

    /// Check database health
    #[cfg(feature = "database")]
    async fn check_database_health(&self) -> Result<()> {
        use crate::db::DB;

        // Check if database is initialized and available
        if !DB::is_initialized() {
            return Err(Error::template("Database not initialized".to_string()));
        }

        // Try to get a connection to verify it's working
        if DB::connection().is_none() {
            return Err(Error::template(
                "Database connection not available".to_string(),
            ));
        }

        Ok(())
    }

    /// Check memory usage
    fn check_memory_usage(&self) -> CheckStatus {
        // Basic memory usage check - in a real implementation you might use
        // system APIs to get actual memory usage
        CheckStatus {
            status: "healthy".to_string(),
            message: Some("Memory usage within acceptable limits".to_string()),
        }
    }

    /// Create a health check response
    pub async fn create_response(&self) -> Result<Response> {
        let health_result = self.check_health().await;
        let status_code = if health_result.status == "healthy" {
            StatusCode::OK
        } else {
            StatusCode::SERVICE_UNAVAILABLE
        };

        Ok(Response::new(status_code)
            .with_header("Content-Type", "application/json")
            .with_body(serde_json::to_string(&health_result)?.into_bytes()))
    }
}

/// Health check result structure
#[derive(serde::Serialize, serde::Deserialize)]
pub struct HealthCheckResult {
    pub status: String,
    pub timestamp: String,
    pub version: String,
    pub checks: std::collections::HashMap<String, CheckStatus>,
}

/// Individual check status
#[derive(serde::Serialize, serde::Deserialize)]
pub struct CheckStatus {
    pub status: String,
    pub message: Option<String>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::AppConfig;
    use crate::views::ViewEngine;

    fn create_test_error_pages() -> ErrorPages {
        let view_engine = Arc::new(ViewEngine::from_directory("views"));
        let config = Arc::new(AppConfig::default());
        ErrorPages::new(view_engine, config)
    }

    #[test]
    fn test_error_template_names() {
        let error_pages = create_test_error_pages();

        assert_eq!(error_pages.get_error_template(404), "errors/404");
        assert_eq!(error_pages.get_error_template(500), "errors/500");
        assert_eq!(error_pages.get_error_template(999), "errors/generic");
    }

    #[test]
    fn test_status_text() {
        let error_pages = create_test_error_pages();

        assert_eq!(error_pages.get_status_text(404), "Not Found");
        assert_eq!(error_pages.get_status_text(500), "Internal Server Error");
        assert_eq!(error_pages.get_status_text(999), "Error");
    }

    #[test]
    fn test_user_friendly_messages() {
        let error_pages = create_test_error_pages();

        let message_404 = error_pages.get_user_friendly_message(404);
        assert!(message_404.contains("could not be found"));

        let message_500 = error_pages.get_user_friendly_message(500);
        assert!(message_500.contains("internal server error"));
    }

    #[test]
    fn test_error_classification() {
        let error_pages = create_test_error_pages();

        let db_error = Error::template("Database connection failed".to_string());
        assert_eq!(error_pages.classify_error_type(&db_error), "Database Error");

        let template_error = Error::template("Template not found".to_string());
        assert_eq!(
            error_pages.classify_error_type(&template_error),
            "Template Error"
        );

        let generic_error = Error::internal("Something went wrong".to_string());
        assert_eq!(
            error_pages.classify_error_type(&generic_error),
            "Application Error"
        );
    }

    #[tokio::test]
    async fn test_fallback_error_page_generation() {
        let error_pages = create_test_error_pages();
        let error = Error::template("Test error".to_string());

        let response = error_pages
            .render_fallback_error_page(404, Some(&error), Some("test-123"))
            .unwrap();

        assert_eq!(response.status, StatusCode::NOT_FOUND);
        assert!(response
            .headers
            .iter()
            .any(|(k, v)| k == "Content-Type" && v.contains("text/html")));

        let body_str = String::from_utf8(response.body.into_vec().unwrap()).unwrap();
        assert!(body_str.contains("404"));
        assert!(body_str.contains("Not Found"));
        assert!(body_str.contains("test-123"));
    }

    #[tokio::test]
    async fn test_json_error_response() {
        let error_pages = create_test_error_pages();
        let error = Error::template("Test API error".to_string());

        let response = error_pages
            .create_json_error_response(400, Some(&error), Some("api-456"))
            .unwrap();

        assert_eq!(response.status, StatusCode::BAD_REQUEST);
        assert!(response
            .headers
            .iter()
            .any(|(k, v)| k == "Content-Type" && v.contains("application/json")));

        let body_str = String::from_utf8(response.body.into_vec().unwrap()).unwrap();
        let mut body_bytes = body_str.clone().into_bytes();
        let json_data: Value = simd_json::from_slice(&mut body_bytes).unwrap();

        assert_eq!(json_data["status"], 400);
        assert_eq!(json_data["error"], true);
        assert_eq!(json_data["request_id"], "api-456");
    }

    #[tokio::test]
    async fn test_health_check() {
        let config = Arc::new(AppConfig::default());
        let health_check = HealthCheck::new(config);

        let result = health_check.check_health().await;

        // Basic health check should always pass
        assert!(result.status == "healthy" || result.status == "unhealthy");
        assert!(!result.version.is_empty());
        assert!(result.checks.contains_key("memory"));
    }

    #[tokio::test]
    async fn test_health_check_response() {
        let config = Arc::new(AppConfig::default());
        let health_check = HealthCheck::new(config);

        let response = health_check.create_response().await.unwrap();

        assert!(
            response.status == StatusCode::OK || response.status == StatusCode::SERVICE_UNAVAILABLE
        );
        assert!(response
            .headers
            .iter()
            .any(|(k, v)| k == "Content-Type" && v.contains("application/json")));

        let body_str = String::from_utf8(response.body.into_vec().unwrap()).unwrap();
        let mut body_bytes = body_str.clone().into_bytes();
        let health_data: HealthCheckResult = simd_json::from_slice(&mut body_bytes).unwrap();

        assert!(!health_data.version.is_empty());
        assert!(!health_data.timestamp.is_empty());
    }

    #[test]
    fn test_html_escape() {
        assert_eq!(
            html_escape("<script>alert('xss')</script>"),
            "&lt;script&gt;alert(&#x27;xss&#x27;)&lt;/script&gt;"
        );
        assert_eq!(html_escape("Normal text"), "Normal text");
        assert_eq!(html_escape("\"quoted\""), "&quot;quoted&quot;");
        assert_eq!(html_escape("A & B"), "A &amp; B");
    }
}