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
use std::sync::Arc;
use crate::config::Config;
use crate::db::DbPool;
#[derive(Clone)]
/// Shared state passed to route handlers via Axum's `State` extractor.
pub struct AppContext {
/// Database connection pool.
pub db: DbPool,
/// Application configuration.
pub config: Arc<Config>,
}
impl AppContext {
/// Creates a new context from a connection pool and configuration.
pub fn new(db: DbPool, config: Config) -> Self {
Self {
db,
config: Arc::new(config),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::Environment;
#[test]
fn app_context_wraps_config_in_arc() {
let config = Config {
host: "127.0.0.1".to_string(),
port: 4000,
blixt_env: Environment::Test,
database_url: None,
jwt_secret: None,
};
// Can't easily create a DbPool without a live connection,
// but we can verify Config wrapping via a separate path.
let arc_config = Arc::new(config);
assert_eq!(arc_config.port, 4000);
assert_eq!(arc_config.blixt_env, Environment::Test);
}
}