ruffus 0.1.2

Fast, minimalist web framework for Rust inspired by Express.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
//! Complete REST API example with Ruffus
//! 
//! This example demonstrates a full-featured REST API with:
//! - CRUD operations
//! - Middleware (logging, auth, CORS)
//! - Router organization
//! - Error handling
//! - JSON request/response handling
//! - Path and query parameters

use async_trait::async_trait;
use ruffus::{App, Middleware, Next, Request, Response, Result, Router};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::Instant;
use http::StatusCode;

// ============================================================================
// Data Models
// ============================================================================

#[derive(Debug, Clone, Serialize, Deserialize)]
struct Task {
    id: u32,
    title: String,
    description: String,
    completed: bool,
    created_at: String,
}

#[derive(Debug, Deserialize)]
struct CreateTaskRequest {
    title: String,
    description: String,
}

#[derive(Debug, Deserialize)]
struct UpdateTaskRequest {
    title: Option<String>,
    description: Option<String>,
    completed: Option<bool>,
}

#[derive(Serialize)]
struct ApiResponse<T> {
    success: bool,
    data: Option<T>,
    message: Option<String>,
}

impl<T> ApiResponse<T> {
    fn success(data: T) -> Self {
        Self {
            success: true,
            data: Some(data),
            message: None,
        }
    }
}

impl ApiResponse<()> {
    fn error(message: String) -> Self {
        Self {
            success: false,
            data: None,
            message: Some(message),
        }
    }
}

// ============================================================================
// In-Memory Database
// ============================================================================

type Database = Arc<Mutex<HashMap<u32, Task>>>;

fn create_database() -> Database {
    let mut db = HashMap::new();
    
    // Seed with some initial data
    db.insert(1, Task {
        id: 1,
        title: "Learn Rust".to_string(),
        description: "Study the Rust programming language".to_string(),
        completed: false,
        created_at: "2024-01-01T10:00:00Z".to_string(),
    });
    
    db.insert(2, Task {
        id: 2,
        title: "Build a web framework".to_string(),
        description: "Create Ruffus, a minimalist web framework".to_string(),
        completed: true,
        created_at: "2024-01-02T11:00:00Z".to_string(),
    });
    
    db.insert(3, Task {
        id: 3,
        title: "Write documentation".to_string(),
        description: "Document all the features and examples".to_string(),
        completed: false,
        created_at: "2024-01-03T12:00:00Z".to_string(),
    });
    
    Arc::new(Mutex::new(db))
}

// ============================================================================
// Middleware
// ============================================================================

/// Logger middleware
struct Logger;

#[async_trait]
impl Middleware for Logger {
    async fn handle(&self, req: Request, next: Next) -> Result<Response> {
        let method = req.method().clone();
        let path = req.uri().path().to_string();
        let timestamp = chrono::Utc::now().format("%Y-%m-%d %H:%M:%S");
        
        println!("[{}] {} {}", timestamp, method, path);
        
        next.run(req).await
    }
}

/// Timer middleware
struct Timer;

#[async_trait]
impl Middleware for Timer {
    async fn handle(&self, req: Request, next: Next) -> Result<Response> {
        let start = Instant::now();
        let response = next.run(req).await;
        let duration = start.elapsed();
        
        println!("  ⏱️  Request completed in {:?}", duration);
        
        response
    }
}

/// CORS middleware
struct Cors;

#[async_trait]
impl Middleware for Cors {
    async fn handle(&self, req: Request, next: Next) -> Result<Response> {
        let response = next.run(req).await?;
        
        let response = response
            .header("Access-Control-Allow-Origin", "*")
            .header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, PATCH, OPTIONS")
            .header("Access-Control-Allow-Headers", "Content-Type, Authorization");
        
        Ok(response)
    }
}

/// Simple API key authentication
struct ApiKeyAuth {
    api_key: String,
}

impl ApiKeyAuth {
    fn new(api_key: String) -> Self {
        Self { api_key }
    }
}

#[async_trait]
impl Middleware for ApiKeyAuth {
    async fn handle(&self, req: Request, next: Next) -> Result<Response> {
        let api_key = req.headers()
            .get("x-api-key")
            .and_then(|v| v.to_str().ok());
        
        match api_key {
            Some(key) if key == self.api_key => {
                next.run(req).await
            }
            _ => {
                Response::json(&ApiResponse::<()>::error(
                    "Invalid or missing API key".to_string()
                )).map(|r| {
                    r.status(StatusCode::UNAUTHORIZED)
                })
            }
        }
    }
}

// ============================================================================
// Route Handlers
// ============================================================================

fn create_task_router(db: Database) -> Router {
    let mut router = Router::new("/tasks");

    // GET /tasks - List all tasks with optional filtering
    let db_clone = db.clone();
    router.get("", move |req: Request| {
        let db = db_clone.clone();
        async move {
            let db = db.lock().unwrap();
            
            // Check for completed filter in query params
            let completed_filter = req.query("completed");
            
            let tasks: Vec<Task> = db.values()
                .filter(|task| {
                    if let Some(filter) = completed_filter {
                        if filter == "true" {
                            task.completed
                        } else if filter == "false" {
                            !task.completed
                        } else {
                            true
                        }
                    } else {
                        true
                    }
                })
                .cloned()
                .collect();
            
            Response::json(&ApiResponse::success(tasks))
        }
    });

    // GET /tasks/:id - Get a specific task
    let db_clone = db.clone();
    router.get("/:id", move |req: Request| {
        let db = db_clone.clone();
        async move {
            let id = req.param("id").unwrap_or("0");
            let task_id: u32 = match id.parse() {
                Ok(id) => id,
                Err(_) => {
                    return Response::json(&ApiResponse::<()>::error(
                        "Invalid task ID".to_string()
                    )).map(|r| {
                        r.status(StatusCode::BAD_REQUEST)
                    });
                }
            };
            
            let db = db.lock().unwrap();
            
            match db.get(&task_id) {
                Some(task) => Response::json(&ApiResponse::success(task.clone())),
                None => Response::json(&ApiResponse::<()>::error(
                    "Task not found".to_string()
                )).map(|r| {
                    r.status(StatusCode::NOT_FOUND)
                }),
            }
        }
    });

    // POST /tasks - Create a new task
    let db_clone = db.clone();
    router.post("", move |mut req: Request| {
        let db = db_clone.clone();
        async move {
            let body: CreateTaskRequest = match req.json().await {
                Ok(body) => body,
                Err(e) => {
                    return Response::json(&ApiResponse::<()>::error(
                        format!("Invalid JSON: {}", e)
                    )).map(|r| {
                        r.status(StatusCode::BAD_REQUEST)
                    });
                }
            };
            
            // Validate
            if body.title.trim().is_empty() {
                return Response::json(&ApiResponse::<()>::error(
                    "Title cannot be empty".to_string()
                )).map(|r| {
                    r.status(StatusCode::BAD_REQUEST)
                });
            }
            
            let mut db = db.lock().unwrap();
            
            // Generate new ID
            let new_id = db.keys().max().unwrap_or(&0) + 1;
            
            let task = Task {
                id: new_id,
                title: body.title,
                description: body.description,
                completed: false,
                created_at: chrono::Utc::now().to_rfc3339(),
            };
            
            db.insert(new_id, task.clone());
            
            Response::json(&ApiResponse::success(task)).map(|r| {
                r.status(StatusCode::CREATED)
            })
        }
    });

    // PUT /tasks/:id - Update a task
    let db_clone = db.clone();
    router.put("/:id", move |mut req: Request| {
        let db = db_clone.clone();
        async move {
            let id = req.param("id").unwrap_or("0");
            let task_id: u32 = match id.parse() {
                Ok(id) => id,
                Err(_) => {
                    return Response::json(&ApiResponse::<()>::error(
                        "Invalid task ID".to_string()
                    )).map(|r| {
                        r.status(StatusCode::BAD_REQUEST)
                    });
                }
            };
            
            let body: UpdateTaskRequest = match req.json().await {
                Ok(body) => body,
                Err(e) => {
                    return Response::json(&ApiResponse::<()>::error(
                        format!("Invalid JSON: {}", e)
                    )).map(|r| {
                        r.status(StatusCode::BAD_REQUEST)
                    });
                }
            };
            
            let mut db = db.lock().unwrap();
            
            match db.get_mut(&task_id) {
                Some(task) => {
                    if let Some(title) = body.title {
                        if !title.trim().is_empty() {
                            task.title = title;
                        }
                    }
                    if let Some(description) = body.description {
                        task.description = description;
                    }
                    if let Some(completed) = body.completed {
                        task.completed = completed;
                    }
                    
                    Response::json(&ApiResponse::success(task.clone()))
                }
                None => Response::json(&ApiResponse::<()>::error(
                    "Task not found".to_string()
                )).map(|r| {
                    r.status(StatusCode::NOT_FOUND)
                }),
            }
        }
    });

    // DELETE /tasks/:id - Delete a task
    let db_clone = db.clone();
    router.delete("/:id", move |req: Request| {
        let db = db_clone.clone();
        async move {
            let id = req.param("id").unwrap_or("0");
            let task_id: u32 = match id.parse() {
                Ok(id) => id,
                Err(_) => {
                    return Response::json(&ApiResponse::<()>::error(
                        "Invalid task ID".to_string()
                    )).map(|r| {
                        r.status(StatusCode::BAD_REQUEST)
                    });
                }
            };
            
            let mut db = db.lock().unwrap();
            
            match db.remove(&task_id) {
                Some(_) => {
                    use serde_json::json;
                    Response::json(&json!({
                        "success": true,
                        "message": format!("Task {} deleted successfully", task_id)
                    }))
                }
                None => Response::json(&ApiResponse::<()>::error(
                    "Task not found".to_string()
                )).map(|r| {
                    r.status(StatusCode::NOT_FOUND)
                }),
            }
        }
    });

    router
}

// ============================================================================
// Main Application
// ============================================================================

#[tokio::main]
async fn main() {
    let mut app = App::new();
    
    // Initialize database
    let db = create_database();

    // Add global middleware
    app.use_middleware(Arc::new(Logger));
    app.use_middleware(Arc::new(Timer));
    app.use_middleware(Arc::new(Cors));

    // Root endpoint
    app.get("/", |_req: Request| async {
        use serde_json::json;
        Response::json(&json!({
            "name": "Ruffus Task API",
            "version": "1.0.0",
            "description": "A complete REST API example built with Ruffus",
            "endpoints": {
                "health": "GET /health",
                "tasks": {
                    "list": "GET /api/tasks?completed=true|false",
                    "get": "GET /api/tasks/:id",
                    "create": "POST /api/tasks",
                    "update": "PUT /api/tasks/:id",
                    "delete": "DELETE /api/tasks/:id"
                },
                "admin": {
                    "stats": "GET /api/admin/stats (requires X-API-Key header)"
                }
            }
        }))
    });

    // Health check endpoint
    app.get("/health", |_req: Request| async {
        use serde_json::json;
        Response::json(&json!({
            "status": "healthy",
            "timestamp": chrono::Utc::now().to_rfc3339()
        }))
    });

    // Create API router
    let mut api_router = Router::new("/api");
    
    // Mount task router
    api_router.mount("", create_task_router(db.clone()));
    
    // Admin endpoints with authentication
    let mut admin_router = Router::new("/admin");
    admin_router.use_middleware(Arc::new(ApiKeyAuth::new("secret-admin-key".to_string())));
    
    let db_clone = db.clone();
    admin_router.get("/stats", move |_req: Request| {
        let db = db_clone.clone();
        async move {
            let db = db.lock().unwrap();
            let total = db.len();
            let completed = db.values().filter(|t| t.completed).count();
            let pending = total - completed;
            
            use serde_json::json;
            Response::json(&json!({
                "total_tasks": total,
                "completed_tasks": completed,
                "pending_tasks": pending,
                "completion_rate": if total > 0 { 
                    (completed as f64 / total as f64) * 100.0 
                } else { 
                    0.0 
                }
            }))
        }
    });
    
    api_router.mount("", admin_router);
    
    // Mount API router
    app.mount("", api_router);

    println!("╔════════════════════════════════════════════════════════════╗");
    println!("║         Ruffus Full REST API Example                      ║");
    println!("╚════════════════════════════════════════════════════════════╝");
    println!();
    println!("Server running at: http://localhost:3000");
    println!();
    println!("📋 Available Endpoints:");
    println!("  GET    /                    - API information");
    println!("  GET    /health              - Health check");
    println!();
    println!("📝 Task Management:");
    println!("  GET    /api/tasks           - List all tasks");
    println!("  GET    /api/tasks?completed=true  - Filter completed tasks");
    println!("  GET    /api/tasks/:id       - Get specific task");
    println!("  POST   /api/tasks           - Create new task");
    println!("  PUT    /api/tasks/:id       - Update task");
    println!("  DELETE /api/tasks/:id       - Delete task");
    println!();
    println!("🔐 Admin (requires X-API-Key: secret-admin-key):");
    println!("  GET    /api/admin/stats     - Get statistics");
    println!();
    println!("💡 Example requests:");
    println!("  curl http://localhost:3000/api/tasks");
    println!("  curl -X POST http://localhost:3000/api/tasks \\");
    println!("       -H 'Content-Type: application/json' \\");
    println!("       -d '{{\"title\":\"New Task\",\"description\":\"Do something\"}}'");
    println!("  curl http://localhost:3000/api/admin/stats \\");
    println!("       -H 'X-API-Key: secret-admin-key'");
    println!();
    
    app.listen("127.0.0.1:3000").await.unwrap();
}