armature-framework 0.2.2

A modern, type-safe HTTP framework for Rust inspired by Angular and NestJS. Features dependency injection, decorators, middleware, authentication (JWT/OAuth2/SAML), validation, OpenAPI/Swagger, caching, job queues, and observability.
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
//! Memory profiling server for leak detection
//!
//! This example creates a server instrumented for memory profiling.
//! It can be used with DHAT, Valgrind, or other memory profilers.
//!
//! # Usage
//!
//! With DHAT (recommended for Rust):
//! ```bash
//! cargo build --example memory_profile_server --release --features memory-profiling
//! ./target/release/examples/memory_profile_server
//! # Generate load, then Ctrl+C to stop and generate report
//! ```
//!
//! With Valgrind:
//! ```bash
//! cargo build --example memory_profile_server --release
//! valgrind --leak-check=full ./target/release/examples/memory_profile_server
//! ```

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::sync::RwLock;

// Conditionally use DHAT allocator
#[cfg(feature = "memory-profiling")]
#[global_allocator]
static ALLOC: dhat::Alloc = dhat::Alloc;

/// Request counter for tracking
static REQUEST_COUNT: AtomicU64 = AtomicU64::new(0);

/// Memory statistics for tracking allocations
#[derive(Debug, Default)]
struct MemoryTracker {
    /// Number of objects created
    objects_created: AtomicU64,
    /// Number of objects dropped
    objects_dropped: AtomicU64,
    /// Bytes allocated (approximate)
    bytes_allocated: AtomicU64,
    /// Bytes freed (approximate)
    bytes_freed: AtomicU64,
}

impl MemoryTracker {
    fn track_alloc(&self, size: usize) {
        self.objects_created.fetch_add(1, Ordering::Relaxed);
        self.bytes_allocated
            .fetch_add(size as u64, Ordering::Relaxed);
    }

    fn track_free(&self, size: usize) {
        self.objects_dropped.fetch_add(1, Ordering::Relaxed);
        self.bytes_freed.fetch_add(size as u64, Ordering::Relaxed);
    }

    fn stats(&self) -> MemoryStats {
        MemoryStats {
            objects_created: self.objects_created.load(Ordering::Relaxed),
            objects_dropped: self.objects_dropped.load(Ordering::Relaxed),
            bytes_allocated: self.bytes_allocated.load(Ordering::Relaxed),
            bytes_freed: self.bytes_freed.load(Ordering::Relaxed),
        }
    }
}

#[derive(Debug, Serialize)]
struct MemoryStats {
    objects_created: u64,
    objects_dropped: u64,
    bytes_allocated: u64,
    bytes_freed: u64,
}

static TRACKER: MemoryTracker = MemoryTracker {
    objects_created: AtomicU64::new(0),
    objects_dropped: AtomicU64::new(0),
    bytes_allocated: AtomicU64::new(0),
    bytes_freed: AtomicU64::new(0),
};

// ============================================================================
// Domain Models - Tracked for memory profiling
// ============================================================================

#[derive(Debug, Clone, Serialize, Deserialize)]
struct User {
    id: u64,
    name: String,
    email: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    metadata: Option<HashMap<String, String>>,
}

impl User {
    fn new(id: u64, name: String, email: String) -> Self {
        let size = std::mem::size_of::<Self>() + name.capacity() + email.capacity();
        TRACKER.track_alloc(size);

        Self {
            id,
            name,
            email,
            metadata: None,
        }
    }

    #[allow(dead_code)]
    fn with_metadata(mut self, metadata: HashMap<String, String>) -> Self {
        let size: usize = metadata
            .iter()
            .map(|(k, v)| k.capacity() + v.capacity())
            .sum();
        TRACKER.track_alloc(size);
        self.metadata = Some(metadata);
        self
    }
}

impl Drop for User {
    fn drop(&mut self) {
        let size = std::mem::size_of::<Self>() + self.name.capacity() + self.email.capacity();
        TRACKER.track_free(size);
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct Post {
    id: u64,
    user_id: u64,
    title: String,
    body: String,
    tags: Vec<String>,
}

impl Post {
    #[allow(dead_code)]
    fn new(id: u64, user_id: u64, title: String, body: String, tags: Vec<String>) -> Self {
        let size = std::mem::size_of::<Self>()
            + title.capacity()
            + body.capacity()
            + tags.iter().map(|t| t.capacity()).sum::<usize>();
        TRACKER.track_alloc(size);

        Self {
            id,
            user_id,
            title,
            body,
            tags,
        }
    }
}

impl Drop for Post {
    fn drop(&mut self) {
        let size = std::mem::size_of::<Self>()
            + self.title.capacity()
            + self.body.capacity()
            + self.tags.iter().map(|t| t.capacity()).sum::<usize>();
        TRACKER.track_free(size);
    }
}

// ============================================================================
// In-Memory Store (Potential leak source if not managed)
// ============================================================================

#[derive(Default)]
struct DataStore {
    users: RwLock<HashMap<u64, User>>,
    posts: RwLock<HashMap<u64, Post>>,
    // Simulated cache that could leak if not bounded
    cache: RwLock<HashMap<String, Vec<u8>>>,
}

impl DataStore {
    fn new() -> Self {
        Self::default()
    }

    async fn get_user(&self, id: u64) -> Option<User> {
        self.users.read().await.get(&id).cloned()
    }

    async fn create_user(&self, user: User) -> User {
        let mut users = self.users.write().await;
        users.insert(user.id, user.clone());
        user
    }

    /// Simulates a bounded cache that could leak if not properly managed
    #[allow(dead_code)]
    async fn cache_set(&self, key: String, value: Vec<u8>) {
        let mut cache = self.cache.write().await;

        // Bounded cache - evict old entries if too large
        const MAX_CACHE_ENTRIES: usize = 1000;
        if cache.len() >= MAX_CACHE_ENTRIES {
            // Simple eviction: remove first entry
            if let Some(first_key) = cache.keys().next().cloned() {
                cache.remove(&first_key);
            }
        }

        TRACKER.track_alloc(key.capacity() + value.capacity());
        cache.insert(key, value);
    }

    async fn stats(&self) -> StoreStats {
        StoreStats {
            user_count: self.users.read().await.len(),
            post_count: self.posts.read().await.len(),
            cache_entries: self.cache.read().await.len(),
            memory: TRACKER.stats(),
        }
    }
}

#[derive(Serialize)]
struct StoreStats {
    user_count: usize,
    post_count: usize,
    cache_entries: usize,
    memory: MemoryStats,
}

// ============================================================================
// Response Types
// ============================================================================

#[derive(Debug, Serialize)]
struct JsonResponse<T> {
    data: T,
    request_id: u64,
}

impl<T: Serialize> JsonResponse<T> {
    fn new(data: T) -> Self {
        Self {
            data,
            request_id: REQUEST_COUNT.fetch_add(1, Ordering::Relaxed),
        }
    }
}

// ============================================================================
// Simple HTTP Handler
// ============================================================================

async fn handle_request(
    store: &Arc<DataStore>,
    method: &str,
    path: &str,
    _body: &str,
) -> (u16, String, Vec<u8>) {
    REQUEST_COUNT.fetch_add(1, Ordering::Relaxed);

    match (method, path) {
        ("GET", "/health") => (200, "text/plain".to_string(), b"OK".to_vec()),

        ("GET", "/json") => {
            let response = serde_json::json!({
                "message": "Hello, World!",
                "timestamp": chrono_lite_timestamp(),
            });
            (
                200,
                "application/json".to_string(),
                serde_json::to_vec(&response).unwrap_or_default(),
            )
        }

        ("GET", "/stats") => {
            let stats = store.stats().await;
            let response = JsonResponse::new(stats);
            (
                200,
                "application/json".to_string(),
                serde_json::to_vec(&response).unwrap_or_default(),
            )
        }

        ("GET", p) if p.starts_with("/users/") => {
            let id_str = p.strip_prefix("/users/").unwrap_or("0");
            let id: u64 = id_str.parse().unwrap_or(0);

            match store.get_user(id).await {
                Some(user) => {
                    let response = JsonResponse::new(user);
                    (
                        200,
                        "application/json".to_string(),
                        serde_json::to_vec(&response).unwrap_or_default(),
                    )
                }
                None => (
                    404,
                    "application/json".to_string(),
                    br#"{"error":"User not found"}"#.to_vec(),
                ),
            }
        }

        ("GET", p) if p.starts_with("/heavy/") => {
            let size_str = p.strip_prefix("/heavy/").unwrap_or("1000");
            let size: usize = size_str.parse().unwrap_or(1000).min(10_000_000);

            // Allocate a vector of specified size
            let data: Vec<u8> = vec![0u8; size];
            TRACKER.track_alloc(data.len());

            // Simulate some processing
            let sum: u64 = data.iter().map(|&b| b as u64).sum();

            TRACKER.track_free(data.len());

            let response = serde_json::json!({
                "allocated_bytes": size,
                "checksum": sum
            });
            (
                200,
                "application/json".to_string(),
                serde_json::to_vec(&response).unwrap_or_default(),
            )
        }

        _ => (404, "text/plain".to_string(), b"Not Found".to_vec()),
    }
}

/// Simple timestamp without heavy dependencies
fn chrono_lite_timestamp() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs()
}

// ============================================================================
// Main
// ============================================================================

#[tokio::main]
async fn main() {
    // Initialize DHAT profiler if enabled
    #[cfg(feature = "memory-profiling")]
    let _profiler = dhat::Profiler::new_heap();

    println!("🧠 Armature Memory Profile Server");
    println!("==================================");
    println!();

    #[cfg(feature = "memory-profiling")]
    println!("📊 DHAT profiler enabled - memory stats will be written on exit");

    #[cfg(not(feature = "memory-profiling"))]
    println!("ℹ️  Build with --features memory-profiling for DHAT support");

    let store = Arc::new(DataStore::new());

    // Pre-populate some data
    for i in 0..100 {
        let user = User::new(i, format!("User {}", i), format!("user{}@example.com", i));
        store.create_user(user).await;
    }
    println!("✅ Pre-populated 100 users");

    println!();
    println!("Endpoints:");
    println!("  GET  /health       - Health check");
    println!("  GET  /json         - Simple JSON response");
    println!("  GET  /stats        - Memory statistics");
    println!("  GET  /users/:id    - Get user by ID");
    println!("  GET  /heavy/:size  - Allocate memory");
    println!();
    println!("🚀 Starting server on http://localhost:3000");
    println!("   Press Ctrl+C to stop and generate memory report");
    println!();

    // Start server
    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();

    // Handle graceful shutdown
    let (shutdown_tx, mut shutdown_rx) = tokio::sync::oneshot::channel::<()>();

    tokio::spawn(async move {
        tokio::signal::ctrl_c().await.ok();
        println!();
        println!("🛑 Shutdown signal received...");
        let _ = shutdown_tx.send(());
    });

    loop {
        tokio::select! {
            result = listener.accept() => {
                match result {
                    Ok((stream, _)) => {
                        let store = store.clone();
                        tokio::spawn(async move {
                            let _ = handle_connection(stream, store).await;
                        });
                    }
                    Err(e) => {
                        eprintln!("Accept error: {}", e);
                    }
                }
            }
            _ = &mut shutdown_rx => {
                println!("📊 Final memory statistics:");
                let stats = TRACKER.stats();
                println!("   Objects created: {}", stats.objects_created);
                println!("   Objects dropped: {}", stats.objects_dropped);
                println!("   Bytes allocated: {}", stats.bytes_allocated);
                println!("   Bytes freed: {}", stats.bytes_freed);
                println!("   Potential leak: {} bytes",
                    stats.bytes_allocated.saturating_sub(stats.bytes_freed));
                break;
            }
        }
    }

    println!();
    println!("🎉 Server stopped. Check memory report.");
}

/// Simplified connection handler
async fn handle_connection(
    stream: tokio::net::TcpStream,
    store: Arc<DataStore>,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let (reader, mut writer) = stream.into_split();
    let mut reader = BufReader::new(reader);
    let mut request_line = String::new();

    reader.read_line(&mut request_line).await?;

    let parts: Vec<&str> = request_line.split_whitespace().collect();
    if parts.len() < 2 {
        return Ok(());
    }

    let method = parts[0];
    let path = parts[1];

    // Read headers and body (simplified)
    let mut content_length: usize = 0;
    loop {
        let mut line = String::new();
        reader.read_line(&mut line).await?;
        if line.trim().is_empty() {
            break;
        }
        if line.to_lowercase().starts_with("content-length:") {
            content_length = line
                .split(':')
                .nth(1)
                .and_then(|s| s.trim().parse().ok())
                .unwrap_or(0);
        }
    }

    // Read body if present
    let body = if content_length > 0 {
        let mut buf = vec![0u8; content_length];
        tokio::io::AsyncReadExt::read_exact(&mut reader, &mut buf).await?;
        String::from_utf8_lossy(&buf).to_string()
    } else {
        String::new()
    };

    // Handle request
    let (status, content_type, body_bytes) = handle_request(&store, method, path, &body).await;

    // Write response
    let response = format!(
        "HTTP/1.1 {} {}\r\nContent-Type: {}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
        status,
        status_text(status),
        content_type,
        body_bytes.len()
    );

    writer.write_all(response.as_bytes()).await?;
    writer.write_all(&body_bytes).await?;
    writer.flush().await?;

    Ok(())
}

fn status_text(status: u16) -> &'static str {
    match status {
        200 => "OK",
        201 => "Created",
        400 => "Bad Request",
        404 => "Not Found",
        500 => "Internal Server Error",
        _ => "Unknown",
    }
}