Skip to main content

rust_webx_core/
request_context.rs

1//! Per-request ambient context for handler and persistence layers.
2
3use std::future::Future;
4
5tokio::task_local! {
6    static OPERATOR_ID: Option<String>;
7}
8
9/// Scoped operator identity from JWT `sub`, set by the HTTP dispatch pipeline.
10pub struct RequestContext;
11
12impl RequestContext {
13    /// Run an async block with the given operator id in scope.
14    pub async fn run<F, R>(operator_id: Option<String>, f: F) -> R
15    where
16        F: Future<Output = R>,
17    {
18        OPERATOR_ID.scope(operator_id, f).await
19    }
20
21    /// Current request operator id, if authenticated.
22    pub fn operator_id() -> Option<String> {
23        OPERATOR_ID.try_with(|id| id.clone()).ok().flatten()
24    }
25}