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
# Cloud Providers Guide

Armature provides unified cloud provider integrations for AWS, GCP, and Azure with dynamic service loading and dependency injection.

## Table of Contents

- [Overview]#overview
- [Features]#features
- [AWS Integration]#aws-integration
- [GCP Integration]#gcp-integration
- [Azure Integration]#azure-integration
- [Dependency Injection]#dependency-injection
- [Best Practices]#best-practices
- [Summary]#summary

## Overview

The cloud provider crates (`armature-aws`, `armature-gcp`, `armature-azure`) provide:

- **Dynamic Service Loading**: Only compile and load the services you need
- **Feature Flags**: Fine-grained control over dependencies
- **Lazy Initialization**: Services are created on-demand
- **DI Integration**: Register cloud services in your application's DI container
- **Unified Configuration**: Environment-based and programmatic configuration

## Features

- ✅ AWS: S3, DynamoDB, SQS, SNS, SES, Lambda, Secrets Manager, KMS, Cognito, and more
- ✅ GCP: Cloud Storage, Pub/Sub, Firestore, Spanner, BigQuery
- ✅ Azure: Blob Storage, Queue Storage, Cosmos DB, Service Bus, Key Vault
- ✅ Dynamic service loading based on configuration
- ✅ Feature flags for each service
- ✅ DI container integration

## AWS Integration

### Installation

Add to your `Cargo.toml`:

```toml
[dependencies]
armature-aws = { version = "0.1", features = ["s3", "dynamodb", "sqs"] }
```

### Available Features

| Feature | Services |
|---------|----------|
| `s3` | S3 Object Storage |
| `dynamodb` | DynamoDB NoSQL Database |
| `sqs` | Simple Queue Service |
| `sns` | Simple Notification Service |
| `ses` | Simple Email Service |
| `lambda` | Lambda Functions |
| `secrets-manager` | Secrets Manager |
| `ssm` | Systems Manager Parameter Store |
| `cloudwatch` | CloudWatch Metrics/Logs |
| `kinesis` | Kinesis Data Streams |
| `kms` | Key Management Service |
| `cognito` | Cognito User Pools |
| `storage` | S3 + DynamoDB |
| `messaging` | SQS + SNS + Kinesis |
| `all` | All services |

### Basic Usage

```rust
use armature_aws::{AwsServices, AwsConfig};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Configure services
    let config = AwsConfig::builder()
        .region("us-east-1")
        .enable_s3()
        .enable_dynamodb()
        .build();

    // Initialize (lazy - clients created on first access)
    let services = AwsServices::new(config).await?;

    // Use S3
    let s3 = services.s3()?;
    let buckets = s3.list_buckets().send().await?;
    println!("Buckets: {:?}", buckets);

    // Use DynamoDB
    let dynamo = services.dynamodb()?;
    let tables = dynamo.list_tables().send().await?;
    println!("Tables: {:?}", tables);

    Ok(())
}
```

### Environment Configuration

```rust
// Reads AWS_REGION, AWS_DEFAULT_REGION, AWS_ENDPOINT_URL
let config = AwsConfig::from_env()
    .enable_s3()
    .enable_sqs()
    .build();
```

### LocalStack Support

```rust
let config = AwsConfig::builder()
    .region("us-east-1")
    .localstack() // Sets endpoint to http://localhost:4566
    .enable_s3()
    .build();
```

## GCP Integration

### Installation

```toml
[dependencies]
armature-gcp = { version = "0.1", features = ["storage", "pubsub"] }
```

### Available Features

| Feature | Services |
|---------|----------|
| `storage` | Cloud Storage |
| `pubsub` | Pub/Sub Messaging |
| `firestore` | Firestore Database |
| `spanner` | Cloud Spanner |
| `bigquery` | BigQuery Analytics |
| `secret-manager` | Secret Manager |
| `data` | Storage + Firestore + Spanner + BigQuery |
| `all` | All services |

### Basic Usage

```rust
use armature_gcp::{GcpServices, GcpConfig};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = GcpConfig::builder()
        .project_id("my-project")
        .enable_storage()
        .enable_pubsub()
        .build();

    let services = GcpServices::new(config).await?;

    // Use Cloud Storage
    let storage = services.storage()?;

    // Use Pub/Sub
    let pubsub = services.pubsub()?;

    Ok(())
}
```

### Environment Configuration

```rust
// Reads GOOGLE_CLOUD_PROJECT, GOOGLE_APPLICATION_CREDENTIALS
let config = GcpConfig::from_env()
    .enable_storage()
    .build();
```

## Azure Integration

### Installation

```toml
[dependencies]
armature-azure = { version = "0.1", features = ["blob", "cosmos"] }
```

### Available Features

| Feature | Services |
|---------|----------|
| `blob` | Blob Storage |
| `queue` | Queue Storage |
| `cosmos` | Cosmos DB |
| `servicebus` | Service Bus |
| `keyvault` | Key Vault |
| `storage` | Blob + Queue |
| `all` | All services |

### Basic Usage

```rust
use armature_azure::{AzureServices, AzureConfig};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = AzureConfig::builder()
        .storage_account("mystorageaccount")
        .enable_blob()
        .enable_cosmos()
        .cosmos_endpoint("https://mydb.documents.azure.com:443/")
        .build();

    let services = AzureServices::new(config).await?;

    // Use Blob Storage
    let blob = services.blob_service()?;

    // Use Cosmos DB
    let cosmos = services.cosmos()?;

    Ok(())
}
```

### Environment Configuration

```rust
// Reads AZURE_STORAGE_ACCOUNT, AZURE_COSMOS_ENDPOINT, etc.
let config = AzureConfig::from_env()
    .enable_blob()
    .build();
```

## Dependency Injection

The cloud provider crates integrate seamlessly with Armature's DI system.

### Registering Cloud Services

```rust
use armature_framework::prelude::*;
use armature_aws::{AwsServices, AwsConfig};

#[module]
struct CloudModule;

#[module_impl]
impl CloudModule {
    // Register AWS services as a singleton
    #[provider(singleton)]
    async fn aws_services() -> Arc<AwsServices> {
        let config = AwsConfig::from_env()
            .enable_s3()
            .enable_sqs()
            .enable_dynamodb()
            .build();
        AwsServices::new(config).await.unwrap()
    }

    // Expose individual clients from the service container
    #[provider]
    fn s3_client(services: &Arc<AwsServices>) -> aws_sdk_s3::Client {
        services.s3().unwrap()
    }

    #[provider]
    fn sqs_client(services: &Arc<AwsServices>) -> aws_sdk_sqs::Client {
        services.sqs().unwrap()
    }

    #[provider]
    fn dynamodb_client(services: &Arc<AwsServices>) -> aws_sdk_dynamodb::Client {
        services.dynamodb().unwrap()
    }
}
```

### Using in Controllers

```rust
#[controller("/files")]
struct FileController;

#[controller_impl]
impl FileController {
    #[post("/upload")]
    async fn upload(
        &self,
        #[inject] s3: aws_sdk_s3::Client,
        body: Bytes,
    ) -> Result<Json<UploadResponse>, HttpError> {
        s3.put_object()
            .bucket("my-bucket")
            .key("uploaded-file")
            .body(body.into())
            .send()
            .await
            .map_err(|e| HttpError::internal(e.to_string()))?;

        Ok(Json(UploadResponse { success: true }))
    }
}
```

### Multi-Cloud Setup

```rust
#[module]
struct MultiCloudModule;

#[module_impl]
impl MultiCloudModule {
    #[provider(singleton)]
    async fn aws() -> Arc<AwsServices> {
        let config = AwsConfig::from_env()
            .enable_s3()
            .build();
        AwsServices::new(config).await.unwrap()
    }

    #[provider(singleton)]
    async fn gcp() -> Arc<GcpServices> {
        let config = GcpConfig::from_env()
            .enable_storage()
            .build();
        GcpServices::new(config).await.unwrap()
    }

    #[provider(singleton)]
    async fn azure() -> Arc<AzureServices> {
        let config = AzureConfig::from_env()
            .enable_blob()
            .build();
        AzureServices::new(config).await.unwrap()
    }
}
```

### Using with Other Armature Crates

Other Armature crates can depend on cloud providers through DI:

```rust
// In armature-storage, use S3 from armature-aws
use armature_aws::aws_sdk_s3;

pub struct S3StorageBackend {
    client: aws_sdk_s3::Client,
    bucket: String,
}

impl S3StorageBackend {
    // Client injected from DI container
    pub fn new(client: aws_sdk_s3::Client, bucket: String) -> Self {
        Self { client, bucket }
    }
}

// In your application module
#[module_impl]
impl StorageModule {
    #[provider]
    fn s3_backend(
        client: aws_sdk_s3::Client,
    ) -> S3StorageBackend {
        S3StorageBackend::new(client, "my-bucket".to_string())
    }
}
```

## Best Practices

### 1. Use Feature Flags

Only enable the services you need to minimize compile times and binary size:

```toml
# Good - only what you need
armature-aws = { version = "0.1", features = ["s3", "sqs"] }

# Avoid unless you need everything
armature-aws = { version = "0.1", features = ["all"] }
```

### 2. Use Environment Configuration

```rust
// Prefer environment-based config for flexibility
let config = AwsConfig::from_env()
    .enable_s3()
    .build();
```

### 3. Register Services as Singletons

```rust
#[provider(singleton)]  // Important!
async fn aws_services() -> Arc<AwsServices> {
    // ...
}
```

### 4. Handle Missing Services Gracefully

```rust
match services.s3() {
    Ok(client) => { /* use client */ }
    Err(AwsError::ServiceNotConfigured(_)) => {
        tracing::warn!("S3 not configured, skipping");
    }
    Err(e) => return Err(e.into()),
}
```

### 5. Use Local Emulators for Testing

```rust
#[cfg(test)]
let config = AwsConfig::builder()
    .localstack()
    .enable_s3()
    .build();

#[cfg(test)]
let config = AzureConfig::builder()
    .use_emulator()
    .enable_blob()
    .build();
```

## Summary

**Key Concepts:**

1. **Dynamic Loading**: Services compile and initialize only when enabled
2. **Feature Flags**: Fine-grained dependency control
3. **DI Integration**: Cloud services live in the application DI container
4. **Lazy Initialization**: Clients created on first access
5. **Environment Config**: Reads from standard cloud environment variables

**Crate Feature Groups:**

| Crate | Storage | Messaging | Security |
|-------|---------|-----------|----------|
| AWS | `storage` | `messaging` | `security` |
| GCP | `data` | `messaging` | - |
| Azure | `storage` | `messaging` | `security` |