feather 0.8.1

Feather: A minimal HTTP framework for Rust
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
# State Management in Feather


Feather provides a powerful context-based state management system. Learn how to store and access application-wide state.

## What is AppContext?


`AppContext` is a type-safe, thread-safe container for application state. Every request has access to the same context, allowing you to:

- Store configuration
- Maintain databases connections
- Share counters and metrics
- Manage user sessions
- Store any application-wide data

## Creating State


### Basic State Setup


```rust,ignore
use feather::{App, AppContext};

#[derive(Clone)]

struct AppConfig {
    database_url: String,
    api_key: String,
}

fn main() {
    let mut app = App::new();
    
    // Create and store state
    let config = AppConfig {
        database_url: "postgresql://localhost/db".to_string(),
        api_key: "secret-key".to_string(),
    };
    
    app.context().set_state(config); // inner data is provided by a Arc Pointer for Read-Only data you dont need to use `State`
}
```

### State Wrapper


For mutable state, wrap it in the `State<T>` struct:

```rust,ignore
use feather::State;

#[derive(Clone)]

struct Counter {
    count: i32,
}

app.context().set_state(State::new(Counter { count: 0 }));
```

For read-only state, you can store it directly:

```rust,ignore
#[derive(Clone)]

struct Config {
    name: String,
}

app.context().set_state(Config { 
    name: "MyApp".to_string() 
});
```

## Accessing State


### From Middleware


Access state using the `ctx` parameter:

```rust,ignore
app.get("/", middleware!(|_req, res, ctx| {
    let config = ctx.get_state::<State<AppConfig>>();
    config.with_scope(|c|{
        // State is now available in this scope
    });
    next!()
}));
```

### Type-Safe Retrieval

Feather uses the type system to manage state. Each state value is keyed by its type:

```rust,ignore
// Different types are stored separately
app.context().set_state(State::new(Config { ... }));
app.context().set_state(State::new(Counter { ... }));

// Access by type
let config = ctx.get_state::<State<Config>>();
let counter = ctx.get_state::<State<Counter>>();
```

### Cloning State

If your state type implements `Clone`, you can get a clone:

```rust,ignore
#[derive(Clone)]

struct User {
    id: u64,
    name: String,
}

let user_clone = ctx.get_state::<State<User>>().get_clone();

```

## Working with Mutable State


### with_mut_scope


The most ergonomic way to modify state:

```rust,ignore
#[derive(Clone)]

struct Counter {
    count: i32,
}

impl Counter {
    fn increment(&mut self) {
        self.count += 1;
    }
}

// In your middleware
let counter = ctx.get_state::<State<Counter>>();
counter.with_mut_scope(|c| {
    c.increment();
    c.increment();
});
```

### with_scope


For read-only accesses its cheaper and safer to use then `with_mut_scope`:
```rust,ignore
let counter = ctx.get_state::<State<Counter>>();
let current = counter.with_scope(|c| {
    println!("Current count: {}", c.count);
    c.count
});
```

### lock()


Get direct access to the lock guard.:

```rust,ignore
let counter = ctx.get_state::<State<Counter>>();
{
    let mut guard = counter.lock();
    guard.count += 1;
    guard.count += 1;  // Multiple operations with one lock
}
```

## Safe Concurrent Access


The `State<T>` wrapper uses `parking_lot::Mutex` for thread-safe access:

```rust,ignore
use feather::State;


#[derive(Clone)]

struct SharedData {
    value: String,
}

// State is automatically thread-safe
app.context().set_state(State::new(SharedData {
    value: "shared".to_string(),
}));

// Multiple threads can safely access it
app.get("/path1", middleware!(|_req, res, ctx| {
    let data = ctx.get_state::<State<SharedData>>();
    data.with_scope(|d| {
        println!("{}", d.value);
    });
    next!()
}));

app.get("/path2", middleware!(|_req, res, ctx| {
    let data = ctx.get_state::<State<SharedData>>();
    data.with_scope(|d| {
        println!("{}", d.value);
    });
    next!()
}));
```

## Optional State Access


Use `try_get_state()` to handle missing state:

```rust,ignore
let maybe_config = ctx.try_get_state::<State<Config>>();

match maybe_config {
    Some(config) => {
        config.with_scope(|c| {
            println!("Config found: {:?}", c);
        });
    }
    None => {
        res.send_text("Configuration not available");
    }
}
```

## Removing State


Remove state from context:

```rust,ignore
// Removes State<Config> if it exists
let removed = ctx.remove_state::<State<Config>>();

if removed {
    println!("State was removed");
} else {
    println!("State was not found");
}
```

## Common State Patterns


### Database Connection Pool


```rust,ignore
use feather::State;

#[derive(Clone)]

struct Database {
    connection_string: String,
}

impl Database {
    fn query(&self, sql: &str) -> Vec<String> {
        // Execute query
        vec![]
    }
}

fn main() {
    let mut app = App::new();
    
    let db = Database {
        connection_string: "postgresql://localhost/db".to_string(),
    };
    
    app.context().set_state(State::new(db));
    
    app.get("/users", middleware!(|_req, res, ctx| {
        let db = ctx.get_state::<State<Database>>();
        let users = db.with_scope(|database| {
            database.query("SELECT * FROM users")
        });
        
        res.send_text(format!("Users: {:?}", users));
        next!()
    }));
}
```

### Configuration Management


```rust,ignore
#[derive(Clone)]

struct Config {
    port: u16,
    host: String,
    debug: bool,
}

fn main() {
    let mut app = App::new();
    
    app.context().set_state(State::new(Config {
        port: 5050,
        host: "127.0.0.1".to_string(),
        debug: true,
    }));
}
```

### Metrics and Counters


```rust,ignore
#[derive(Clone)]

struct Metrics {
    requests: i64,
    errors: i64,
}

impl Metrics {
    fn record_request(&mut self) {
        self.requests += 1;
    }
    
    fn record_error(&mut self) {
        self.errors += 1;
    }
}

fn main() {
    let mut app = App::new();
    
    app.context().set_state(State::new(Metrics {
        requests: 0,
        errors: 0,
    }));
    
    // Record metrics in middleware
    app.use_middleware(middleware!(|_req, res, ctx| {
        let metrics = ctx.get_state::<State<Metrics>>();
        metrics.with_mut_scope(|m| {
            m.record_request();
        });
        next!()
    }));
}
```

### User Sessions (Simple Example)


```rust,ignore
use std::collections::HashMap;
use feather::State;

#[derive(Clone)]

struct Session {
    user_id: Option<u64>,
}

#[derive(Clone)]

struct Sessions {
    sessions: HashMap<String, Session>,
}

fn main() {
    let mut app = App::new();
    
    app.context().set_state(State::new(Sessions {
        sessions: HashMap::new(),
    }));
}
```

## State Lifetimes


State in Feather lives for the entire duration of the application:

```rust,ignore
let mut app = App::new();

// State stored here...
app.context().set_state(State::new(MyData { ... }));

// ...is available for all requests until app.listen() is called
app.listen("127.0.0.1:5050");  // Blocks forever
```

When `app.listen()` is called, the app starts serving requests and continues until the process exits.

## Deadlock Prevention


⚠️ **Important**: Do NOT access the same `State<T>` recursively:

```rust,ignore
// DON'T DO THIS - Will cause deadlock!
let state = ctx.get_state::<State<MyType>>();
state.with_scope(|data| {
    let state_again = ctx.get_state::<State<MyType>>();  // ❌ DEADLOCK!
    state_again.with_scope(|_| { /* ... */ });
});
```

Instead, refactor to avoid nested access:

```rust,ignore
// DO THIS - No deadlock
let state = ctx.get_state::<State<MyType>>();
let value = state.with_scope(|data| {
    // Extract what you need
    data.some_field.clone()
});

// Now you can access again if needed
let state = ctx.get_state::<State<MyType>>();
state.with_scope(|data| { /* ... */ });
```

## Best Practices


1. **Use `Clone` for state types if you can** - Makes it easier to work with
2. **Keep state simple** - Avoid deeply nested structures
3. **Use `with_scope()` or `with_mut_scope()`** - More ergonomic than `lock()`
4. **Extract values when needed** - Don't hold locks longer than necessary
5. **Use appropriate access patterns** - Read-only vs. mutable access
6. **Avoid recursive access** - Can cause deadlocks