web-server-abstraction 1.0.2

An ergonomic abstraction layer over popular Rust web frameworks
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
# Web Server Abstraction


An ergonomic abstraction layer over popular Rust web frameworks# Use specific framework adapters

# [cfg(feature = "axum")]


let server = WebServer::with_axum_adapter();

# [cfg(feature = "actix-web")]


let server = WebServer::with_actix_adapter();

# [cfg(feature = "warp")]


let server = WebServer::with_warp_adapter();

# [cfg(feature = "rocket")]


let server = WebServer::with_rocket_adapter();

# [cfg(feature = "salvo")]


let server = WebServer::with_salvo_adapter();

# [cfg(feature = "poem")]


let server = WebServer::with_poem_adapter();

// Or use the mock adapter for testing
let server = WebServer::with_mock_adapter();ou to write web applications once and run them on any supported framework.

## Features


- **Framework Agnostic**: Write once, run on any supported framework
- **Type Safe**: Leverages Rust's type system for compile-time guarantees
- **Async First**: Built for modern async Rust with native async/await support
- **Middleware Support**: Composable middleware system for cross-cutting concerns
- **Tower Integration**: Built on the Tower ecosystem for compatibility
- **Ergonomic API**: Clean, intuitive API that's easy to learn and use
- **Advanced Routing**: Path parameters (`:id`) and wildcards (`*file`) support
- **WebSocket Ready**: Built-in WebSocket upgrade handling and message types
- **HTTP Method Shortcuts**: Convenient `.get()`, `.post()`, `.put()`, etc. methods
- **Rich Middleware**: 9+ built-in middleware types for common web patterns
- **Performance Optimized**: Comprehensive benchmarking and profiling infrastructure

## Supported Frameworks


| Framework | Feature Flag | Status |
|-----------|-------------|--------|
| Mock (Testing) | Default | ✅ Complete |
| Axum | `axum` | ✅ Complete |
| Actix-Web | `actix-web` | ✅ Complete |
| Warp | `warp` | ✅ Complete |
| Rocket | `rocket` | ✅ Complete |
| Salvo | `salvo` | ✅ Complete |
| Poem | `poem` | ✅ Complete |

> **Note**: All framework adapters are production-ready and fully tested with their latest versions.

## Quick Start


Add this to your `Cargo.toml`:

```toml
[dependencies]
web-server-abstraction = "1.0.2"  # Includes Axum support by default

# Or explicitly enable specific framework features

web-server-abstraction = { version = "1.0.2", features = ["axum"] }

# Enable multiple frameworks

web-server-abstraction = { version = "1.0.2", features = ["axum", "rocket", "poem"] }
```

### Basic Example


```rust
use web_server_abstraction::{
    WebServer, HttpMethod, Response, StatusCode,
    middleware::{LoggingMiddleware, CorsMiddleware},
};

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let server = WebServer::new()
        // Add middleware
        .middleware(LoggingMiddleware::new())
        .middleware(CorsMiddleware::new().allow_origin("*"))

        // Add routes
        .route("/", HttpMethod::GET, |_req| async {
            Ok(Response::ok().body("Hello, World!"))
        })
        .route("/health", HttpMethod::GET, |_req| async {
            Ok(Response::ok().body("OK"))
        })
        .route("/users", HttpMethod::POST, |req| async {
            // Parse JSON body
            let user: serde_json::Value = req.json().await?;
            Ok(Response::new(StatusCode::CREATED)
                .header("content-type", "application/json")
                .body(serde_json::to_string(&user)?))
        });

    // Bind and run the server
    let bound_server = server.bind("127.0.0.1:3000").await?;
    println!("Server running on http://127.0.0.1:3000");
    bound_server.run().await?;

    Ok(())
}
```

### Framework-Specific Adapters


```rust
use web_server_abstraction::WebServer;

// Use specific framework adapters
#[cfg(feature = "axum")]

let server = WebServer::with_axum_adapter();

#[cfg(feature = "actix-web")]

let server = WebServer::with_actix_adapter();

#[cfg(feature = "warp")]

let server = WebServer::with_warp_adapter();

// Or use the mock adapter for testing
let server = WebServer::with_mock_adapter();
```

## Architecture


### Core Abstractions


The crate is built around several key abstractions:

1. **WebServer**: The main entry point for building web applications
2. **Handler**: Trait for request handlers that convert requests to responses
3. **Middleware**: Composable middleware for cross-cutting concerns
4. **Adapter**: Framework-specific implementations that bridge to actual web frameworks

### Type System


```rust
// Core types are standardized across frameworks
pub struct Request { /* ... */ }
pub struct Response { /* ... */ }
pub enum HttpMethod { GET, POST, PUT, DELETE, /* ... */ }
pub struct StatusCode(pub u16);
```

### Middleware System


Built-in middleware includes:

- **LoggingMiddleware**: Request/response logging with configurable detail levels
- **CorsMiddleware**: Cross-Origin Resource Sharing with full configuration support
- **AuthMiddleware**: Authentication checks with bearer token validation
- **TimeoutMiddleware**: Request timeouts with configurable durations
- **RateLimitMiddleware**: Rate limiting with sliding window algorithm
- **CompressionMiddleware**: Response compression (gzip, deflate)
- **SecurityHeadersMiddleware**: Security headers (HSTS, CSP, X-Frame-Options, etc.)
- **MetricsMiddleware**: Request metrics collection and monitoring
- **CacheMiddleware**: Response caching with TTL and invalidation strategies

```rust
use web_server_abstraction::middleware::*;

let server = WebServer::new()
    .middleware(LoggingMiddleware::new().log_bodies(true))
    .middleware(CorsMiddleware::new()
        .allow_origin("https://example.com")
        .allow_methods(vec!["GET".to_string(), "POST".to_string()])
        .allow_credentials(true))
    .middleware(AuthMiddleware::new()
        .with_bearer_tokens(vec!["secret-token".to_string()]))
    .middleware(RateLimitMiddleware::new(100, Duration::from_secs(60)))
    .middleware(CompressionMiddleware::new().min_size(1024))
    .middleware(SecurityHeadersMiddleware::new())
    .middleware(MetricsMiddleware::new())
    .middleware(CacheMiddleware::new(Duration::from_secs(300)));
```

## Framework Integration Plan


### Current Design Principles


1. **Common HTTP Abstractions**: Use standardized types from the `http` crate
2. **Tower Compatibility**: Leverage the Tower ecosystem where possible
3. **Zero-Cost Abstractions**: Minimal runtime overhead
4. **Ergonomic APIs**: Focus on developer experience

### Adapter Implementation Strategy


Each framework adapter follows this pattern:

```rust
// 1. Convert our types to framework types
fn convert_request(req: Request) -> FrameworkRequest;
fn convert_response(resp: FrameworkResponse) -> Response;

// 2. Implement the adapter interface
impl FrameworkAdapter {
    pub async fn bind(&mut self, addr: &str) -> Result<()>;
    pub async fn run(self) -> Result<()>;
    pub fn route(&mut self, path: &str, method: HttpMethod, handler: HandlerFn);
    pub fn middleware(&mut self, middleware: Box<dyn Middleware>);
}
```

### Integration Status


#### Axum ✅ **COMPLETE**


- ✅ Basic route registration
- ✅ Request/response type conversion
- ✅ Middleware integration with Tower ServiceBuilder
- ✅ Full HTTP method support
- ✅ Async handler support

#### Actix-Web ✅ **COMPLETE**


- ✅ Route registration and handlers
- ✅ Request/response conversion
- ✅ HTTP server binding and running
- ✅ Built-in logging middleware
- ✅ Full HTTP method support

#### Warp ✅ **COMPLETE**


- ✅ Filter-based routing system
- ✅ Request/response handling
- ✅ Middleware composition
- ✅ Async handler support
- ✅ Server binding and execution

#### Rocket ✅ **COMPLETE**


- ✅ Production-ready adapter implementation
- ✅ Route registration with Rocket's Handler trait
- ✅ Request/response type conversion
- ✅ Middleware integration via Fairings
- ✅ Full HTTP method support
- ✅ Server configuration and binding
- ✅ Comprehensive error handling

#### Salvo ✅ **COMPLETE**


- ✅ Production-ready adapter implementation
- ✅ High-performance web framework integration
- ✅ Modular design with extractors
- ✅ Router and Service integration
- ✅ Middleware fairing system
- ✅ Full HTTP method support
- ✅ TcpListener binding and server execution

#### Poem ✅ **COMPLETE**


- ✅ Production-ready adapter implementation
- ✅ Fast and lightweight framework integration
- ✅ Type-safe Endpoint trait implementation
- ✅ Built-in middleware (Tracing, NormalizePath)
- ✅ Comprehensive request/response conversion
- ✅ Full HTTP method support
- ✅ TcpListener and Server integration

## Testing


The crate includes a mock adapter for easy testing:

```rust
#[tokio::test]

async fn test_my_routes() {
    use web_server_abstraction::MockAdapter;

    let server = WebServer::with_mock_adapter()
        .route("/test", HttpMethod::GET, |_| async {
            Ok(Response::ok().body("test"))
        });

    let bound_server = server.bind("127.0.0.1:0").await.unwrap();

    // Mock adapter provides testing utilities
    // (In practice, you'd make actual HTTP requests)
}
```

Run tests with:

```bash
cargo test
```

Run examples with:

```bash
cargo run --example basic_server
```

## Contributing


We welcome contributions! Areas where help is needed:

1. **Framework Adapters**: Implementing adapters for different frameworks
2. **Middleware**: Adding common middleware implementations
3. **Documentation**: Improving docs and examples
4. **Testing**: Adding comprehensive test coverage
5. **Performance**: Benchmarking and optimization

### Adding a New Framework Adapter


1. Create a new module in `src/adapters/`
2. Implement the required methods: `bind`, `run`, `route`, `middleware`
3. Add conversion functions between framework types and our types
4. Add a feature flag in `Cargo.toml`
5. Update the `AdapterType` enum in `core.rs`
6. Add tests and documentation

## Performance Considerations


- **Zero-cost abstractions**: The abstraction layer adds minimal overhead
- **Compile-time dispatch**: Framework adapters use static dispatch where possible
- **Memory efficiency**: Minimal allocations in hot paths
- **Async-first**: Built for modern async Rust performance characteristics

### Benchmarking and Profiling


The crate includes comprehensive benchmarking infrastructure:

```rust
use web_server_abstraction::benchmarks::{
    PerformanceProfiler, BenchmarkConfig, BenchmarkSuite
};

// Configure and run performance benchmarks
let config = BenchmarkConfig {
    duration: Duration::from_secs(30),
    concurrent_requests: 100,
    warmup_duration: Duration::from_secs(5),
};

let profiler = PerformanceProfiler::new(config);
let results = profiler.benchmark_scenario("load_test").await?;

// Analyze results with statistical metrics
println!("Average response time: {:?}", results.mean());
println!("95th percentile: {:?}", results.percentile(95.0));
println!("Requests per second: {}", results.requests_per_second());
```

Features include:

- **Statistical Analysis**: Mean, median, percentiles, standard deviation
- **Memory Profiling**: Memory usage tracking and leak detection
- **Scenario Comparison**: Compare performance across different configurations
- **Optimization Recommendations**: Automated performance suggestions
- **Async-first**: Built for modern async Rust performance characteristics

## License


Licensed under either of Apache License, Version 2.0 or MIT license at your option.

## Roadmap


### Version 0.1.0 ✅ **COMPLETED**


- [x] Core abstractions and API design
- [x] Mock adapter for testing
- [x] Basic middleware system
- [x] Documentation and examples

### Version 0.2.0 ✅ **COMPLETED**


- [x] Complete Axum adapter
- [x] Actix-Web adapter
- [x] Enhanced middleware ecosystem (9 middleware types implemented)
- [x] Performance benchmarks and profiling infrastructure

### Version 0.3.0 ✅ **COMPLETED**


- [x] Warp adapter
- [x] Advanced routing features (path parameters, wildcards)
- [x] WebSocket support (basic implementation)
- [x] HTTP method convenience functions (get, post, put, delete, patch)
- [x] Framework adapter scaffolding (Rocket, Salvo, Poem - basic structure in place)

### Version 1.0.0 ✅ **COMPLETED**


- [x] Complete Rocket adapter
- [x] Complete Salvo adapter
- [x] Complete Poem adapter
- [x] Comprehensive middleware library
- [x] Production-ready performance
- [x] Stable API
- [x] WebSocket support
- [x] Advanced routing features (wildcards, parameters)
- [x] Mountable interface support
- [x] Authentication integration## Why This Approach?

### Problem Statement


Many Rust crates need to support multiple web frameworks, leading to:

- Duplicate implementation effort
- Maintenance burden across multiple framework versions
- User lock-in to specific frameworks
- Inconsistent APIs across different framework integrations

### Solution Benefits


1. **Write Once, Run Anywhere**: Implement your web logic once
2. **Framework Flexibility**: Users can choose their preferred framework
3. **Easier Testing**: Mock adapter makes testing straightforward
4. **Future-Proof**: Easy to add support for new frameworks
5. **Consistent API**: Same interface regardless of underlying framework

### Trade-offs


- **Additional Abstraction**: One more layer between your code and the framework
- **Learning Curve**: New API to learn (though designed to be intuitive)
- **Feature Lag**: Advanced framework-specific features may not be immediately available

We believe the benefits outweigh these trade-offs for most use cases, especially for libraries and applications that need broad framework compatibility.