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
#![allow(dead_code)]

//! Ferron Integration Example
//!
//! This example demonstrates a complete setup of an Armature application
//! with Ferron reverse proxy configuration.
//!
//! ## What This Example Does
//!
//! 1. Starts an Armature HTTP API server
//! 2. Generates Ferron reverse proxy configuration
//! 3. Shows how to use service discovery for dynamic backends
//! 4. Demonstrates health check integration
//!
//! ## Running This Example
//!
//! ```bash
//! cargo run --example ferron_integration
//! ```
//!
//! Then test the API:
//! ```bash
//! curl http://localhost:<port>/api/status
//! curl http://localhost:<port>/api/users
//! curl http://localhost:<port>/health
//! ```

#[path = "common/mod.rs"]
mod common;

use armature::logging::LogConfig;
use armature::prelude::*;
use armature_ferron::{
    Backend, FerronConfig, HealthCheckConfig, LoadBalanceStrategy, LoadBalancer, Location,
    RateLimitConfig, ServiceRegistry,
};
use common::find_available_port;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::RwLock;

// ============================================================================
// Domain Models
// ============================================================================

#[derive(Debug, Clone, Serialize, Deserialize)]
struct User {
    id: u64,
    name: String,
    email: String,
    role: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct ApiStatus {
    status: String,
    version: String,
    uptime_seconds: u64,
    requests_served: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct HealthResponse {
    healthy: bool,
    checks: Vec<HealthCheck>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct HealthCheck {
    name: String,
    status: String,
    latency_ms: u64,
}

// ============================================================================
// Services
// ============================================================================

#[injectable]
#[derive(Clone, Debug)]
struct UserService {
    users: Arc<RwLock<Vec<User>>>,
}

impl Default for UserService {
    fn default() -> Self {
        Self::new()
    }
}

impl UserService {
    fn new() -> Self {
        let users = vec![
            User {
                id: 1,
                name: "Alice".to_string(),
                email: "alice@example.com".to_string(),
                role: "admin".to_string(),
            },
            User {
                id: 2,
                name: "Bob".to_string(),
                email: "bob@example.com".to_string(),
                role: "user".to_string(),
            },
            User {
                id: 3,
                name: "Charlie".to_string(),
                email: "charlie@example.com".to_string(),
                role: "user".to_string(),
            },
        ];
        Self {
            users: Arc::new(RwLock::new(users)),
        }
    }

    async fn get_all(&self) -> Vec<User> {
        self.users.read().await.clone()
    }

    async fn get_by_id(&self, id: u64) -> Option<User> {
        self.users.read().await.iter().find(|u| u.id == id).cloned()
    }
}

#[injectable]
#[derive(Clone, Debug)]
struct StatsService {
    start_time: std::time::Instant,
    request_count: Arc<RwLock<u64>>,
}

impl Default for StatsService {
    fn default() -> Self {
        Self::new()
    }
}

impl StatsService {
    fn new() -> Self {
        Self {
            start_time: std::time::Instant::now(),
            request_count: Arc::new(RwLock::new(0)),
        }
    }

    async fn increment_requests(&self) {
        let mut count = self.request_count.write().await;
        *count += 1;
    }

    async fn get_status(&self) -> ApiStatus {
        ApiStatus {
            status: "healthy".to_string(),
            version: "1.0.0".to_string(),
            uptime_seconds: self.start_time.elapsed().as_secs(),
            requests_served: *self.request_count.read().await,
        }
    }
}

// ============================================================================
// Controllers
// ============================================================================

#[controller("/api")]
#[derive(Default, Clone)]
struct ApiController;

#[routes]
impl ApiController {
    #[get("/status")]
    async fn get_status() -> Result<HttpResponse, Error> {
        let status = ApiStatus {
            status: "healthy".to_string(),
            version: "1.0.0".to_string(),
            uptime_seconds: 0,
            requests_served: 1,
        };
        HttpResponse::json(&status)
    }

    #[get("/users")]
    async fn get_users() -> Result<HttpResponse, Error> {
        let users = vec![
            User {
                id: 1,
                name: "Alice".to_string(),
                email: "alice@example.com".to_string(),
                role: "admin".to_string(),
            },
            User {
                id: 2,
                name: "Bob".to_string(),
                email: "bob@example.com".to_string(),
                role: "user".to_string(),
            },
            User {
                id: 3,
                name: "Charlie".to_string(),
                email: "charlie@example.com".to_string(),
                role: "user".to_string(),
            },
        ];
        HttpResponse::json(&users)
    }

    #[get("/users/:id")]
    async fn get_user(req: HttpRequest) -> Result<HttpResponse, Error> {
        let id: u64 = req
            .param("id")
            .and_then(|s| s.parse().ok())
            .ok_or_else(|| Error::validation("Invalid user ID"))?;

        let users = vec![
            User {
                id: 1,
                name: "Alice".to_string(),
                email: "alice@example.com".to_string(),
                role: "admin".to_string(),
            },
            User {
                id: 2,
                name: "Bob".to_string(),
                email: "bob@example.com".to_string(),
                role: "user".to_string(),
            },
            User {
                id: 3,
                name: "Charlie".to_string(),
                email: "charlie@example.com".to_string(),
                role: "user".to_string(),
            },
        ];

        match users.into_iter().find(|u| u.id == id) {
            Some(user) => HttpResponse::json(&user),
            None => Err(Error::not_found(format!("User {} not found", id))),
        }
    }
}

#[controller("")]
#[derive(Default, Clone)]
struct HealthController;

#[routes]
impl HealthController {
    #[get("/health")]
    async fn health() -> Result<HttpResponse, Error> {
        let response = HealthResponse {
            healthy: true,
            checks: vec![
                HealthCheck {
                    name: "database".to_string(),
                    status: "ok".to_string(),
                    latency_ms: 5,
                },
                HealthCheck {
                    name: "cache".to_string(),
                    status: "ok".to_string(),
                    latency_ms: 1,
                },
            ],
        };
        HttpResponse::json(&response)
    }

    #[get("/ready")]
    async fn ready() -> Result<HttpResponse, Error> {
        HttpResponse::json(&serde_json::json!({
            "ready": true
        }))
    }

    #[get("/live")]
    async fn live() -> Result<HttpResponse, Error> {
        Ok(HttpResponse::ok().with_body(b"OK".to_vec()))
    }
}

// ============================================================================
// Module Configuration
// ============================================================================

#[module(
    controllers: [ApiController, HealthController]
)]
#[derive(Default)]
struct AppModule;

// ============================================================================
// Ferron Configuration Generator
// ============================================================================

fn generate_ferron_config(
    domain: &str,
    app_port: u16,
) -> std::result::Result<FerronConfig, armature_ferron::FerronError> {
    FerronConfig::builder()
        .domain(domain)
        .backend_url(format!("http://127.0.0.1:{}", app_port))
        .tls_auto(true)
        // API routes with rate limiting
        .location(
            Location::new("/api")
                .proxy(format!("http://127.0.0.1:{}/api", app_port))
                .rate_limit(RateLimitConfig::new(100).burst(200)),
        )
        // Health endpoints (no rate limit for probes)
        .location(Location::new("/health").proxy(format!("http://127.0.0.1:{}/health", app_port)))
        .location(Location::new("/ready").proxy(format!("http://127.0.0.1:{}/ready", app_port)))
        .location(Location::new("/live").proxy(format!("http://127.0.0.1:{}/live", app_port)))
        // Security headers
        .header("X-Frame-Options", "DENY")
        .header("X-Content-Type-Options", "nosniff")
        .header("X-XSS-Protection", "1; mode=block")
        .header("Referrer-Policy", "strict-origin-when-cross-origin")
        .gzip(true)
        .build()
}

fn generate_load_balanced_config(
    domain: &str,
    backend_ports: &[u16],
) -> std::result::Result<FerronConfig, armature_ferron::FerronError> {
    let mut lb = LoadBalancer::new()
        .strategy(LoadBalanceStrategy::RoundRobin)
        .health_check_interval(30)
        .health_check_path("/health")
        .health_check_threshold(3);

    for port in backend_ports {
        lb = lb.backend(Backend::new(format!("http://127.0.0.1:{}", port)));
    }

    FerronConfig::builder()
        .domain(domain)
        .load_balancer(lb)
        .tls_auto(true)
        .header("X-Frame-Options", "DENY")
        .header("X-Content-Type-Options", "nosniff")
        .gzip(true)
        .build()
}

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

#[tokio::main]
async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
    // Initialize logging
    let _guard = LogConfig::default().init();

    let port = find_available_port();

    println!("🔄 Ferron Integration Example");
    println!("==============================\n");

    // 1. Generate Ferron configuration
    println!("📝 Step 1: Generate Ferron Configuration");
    println!("-----------------------------------------\n");

    let ferron_config = generate_ferron_config("api.example.com", port)?;
    println!("Single-backend configuration:\n");
    println!("{}", ferron_config.to_kdl()?);

    // 2. Generate load-balanced configuration
    println!("\n📝 Step 2: Load-Balanced Configuration");
    println!("---------------------------------------\n");

    let lb_config = generate_load_balanced_config("api.example.com", &[port, port + 1, port + 2])?;
    println!("Load-balanced configuration:\n");
    println!("{}", lb_config.to_kdl()?);

    // 3. Service Discovery Demo
    println!("\n📝 Step 3: Service Discovery");
    println!("-----------------------------\n");

    let registry = ServiceRegistry::new();

    // Register this instance
    let instance_id = registry
        .register("armature-api", &format!("http://127.0.0.1:{}", port))
        .await?;
    println!("Registered instance: {}", instance_id);

    // Simulate additional instances
    registry
        .register("armature-api", &format!("http://127.0.0.1:{}", port + 1))
        .await?;
    registry
        .register("armature-api", &format!("http://127.0.0.1:{}", port + 2))
        .await?;

    let instances = registry.get_instances("armature-api").await;
    println!("Total registered instances: {}", instances.len());

    let urls = registry.get_urls("armature-api").await;
    println!("Backend URLs: {:?}", urls);

    // 4. Health Check Configuration
    println!("\n📝 Step 4: Health Check Setup");
    println!("------------------------------\n");

    let health_config = HealthCheckConfig::new()
        .path("/health")
        .timeout(Duration::from_secs(5))
        .interval(Duration::from_secs(30))
        .unhealthy_threshold(3)
        .healthy_threshold(2);

    println!("Health check configuration:");
    println!("  Endpoint: /health");
    println!("  Timeout: {:?}", health_config.timeout);
    println!("  Interval: {:?}", health_config.interval);
    println!(
        "  Unhealthy after: {} failures",
        health_config.unhealthy_threshold
    );
    println!(
        "  Healthy after: {} successes",
        health_config.healthy_threshold
    );

    // 5. Start the Armature server
    println!("\n🚀 Step 5: Starting Armature Server");
    println!("------------------------------------\n");

    println!("Server running on http://127.0.0.1:{}", port);
    println!("\n📋 Available Endpoints:");
    println!("  GET  /api/status    - API status and statistics");
    println!("  GET  /api/users     - List all users");
    println!("  GET  /api/users/:id - Get user by ID");
    println!("  GET  /health        - Health check (for Ferron)");
    println!("  GET  /ready         - Readiness probe");
    println!("  GET  /live          - Liveness probe");

    println!("\n💡 Test Commands:");
    println!("  curl http://127.0.0.1:{}/api/status", port);
    println!("  curl http://127.0.0.1:{}/api/users", port);
    println!("  curl http://127.0.0.1:{}/api/users/1", port);
    println!("  curl http://127.0.0.1:{}/health", port);

    println!("\n📄 To use with Ferron:");
    println!("  1. Save the configuration above to /etc/ferron/ferron.conf");
    println!("  2. Start Ferron: ferron -c /etc/ferron/ferron.conf");
    println!("  3. Access via: https://api.example.com");

    println!("\nPress Ctrl+C to stop\n");

    // Start the server
    let app = Application::create::<AppModule>().await;
    app.listen(port).await?;

    Ok(())
}