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
pub mod header;
use std::any::Any;
use std::sync::Arc;
use futures::lock::Mutex;
use crate::service::context::Context;
#[derive(Clone)]
pub struct ServiceState {
context: Arc<Context>,
/// This member gives access to the service own state (added when it is
/// created, with the `ServiceBuilder::http_with_state` API).
///
/// One can retrieve the proper service state structure like the example:
///
/// ```
/// use std::sync::Arc;
///
/// use axum::extract::State;
/// use mikros::Mutex;
/// use mikros::http::ServiceState;
///
/// #[derive(Clone)]
/// pub struct AppState;
///
/// async fn handler(State(state): State<Arc<Mutex<ServiceState>>>) -> String {
/// if let Some(app_state) = &state.lock().await.app_state {
/// let mut locked = app_state.as_ref().lock().await;
/// let svc_state = locked.downcast_mut::<AppState>().unwrap();
///
/// // svc_state can be manipulated from here.
/// }
///
/// "Ok".to_string()
/// }
/// ```
///
pub app_state: Option<Arc<Mutex<dyn Any + Send + Sync>>>,
}
impl ServiceState {
pub(crate) fn new(context: Arc<Context>) -> Self {
Self {
context: context.clone(),
app_state: None,
}
}
pub(crate) fn new_with_state(
context: Arc<Context>,
internal_state: Arc<Mutex<dyn Any + Send + Sync>>,
) -> Self {
let mut s = Self::new(context);
s.app_state = Some(internal_state.clone());
s
}
/// Allows retrieving the mikros Context object from a handler state. Even
/// if the service was not initialized with state, the context will always
/// be available.
pub fn context(&self) -> Arc<Context> {
self.context.clone()
}
}