use std::path::PathBuf;
use std::sync::Arc;
use console_auth::{BootstrapAuthority, M2mState, SessionService, SessionState};
use console_store::{CredentialCipher, Store};
#[derive(Clone)]
pub struct AppState(Arc<Inner>);
struct Inner {
store: Store,
sessions: SessionService,
bootstrap: BootstrapAuthority,
artifact_dir: PathBuf,
node_client: reqwest::Client,
stream_client: reqwest::Client,
cipher: CredentialCipher,
}
impl AppState {
pub fn new(store: Store, bootstrap: BootstrapAuthority) -> Self {
Self::new_with_artifact_dir(store, bootstrap, PathBuf::from("./data/artifacts"))
}
pub fn new_with_artifact_dir(
store: Store,
bootstrap: BootstrapAuthority,
artifact_dir: PathBuf,
) -> Self {
Self::new_full(
store,
bootstrap,
artifact_dir,
CredentialCipher::ephemeral(),
)
}
pub fn new_full(
store: Store,
bootstrap: BootstrapAuthority,
artifact_dir: PathBuf,
cipher: CredentialCipher,
) -> Self {
Self(Arc::new(Inner {
sessions: SessionService::new(store.clone()),
store,
bootstrap,
node_client: console_proxy::node_http_client().expect("node http client"),
stream_client: console_proxy::node_stream_client().expect("stream http client"),
artifact_dir,
cipher,
}))
}
pub fn store(&self) -> &Store {
&self.0.store
}
pub fn session_service(&self) -> &SessionService {
&self.0.sessions
}
pub fn artifact_dir(&self) -> &std::path::Path {
&self.0.artifact_dir
}
pub fn node_client(&self) -> &reqwest::Client {
&self.0.node_client
}
pub fn stream_client(&self) -> &reqwest::Client {
&self.0.stream_client
}
pub fn cipher(&self) -> &CredentialCipher {
&self.0.cipher
}
}
impl SessionState for AppState {
fn sessions(&self) -> &SessionService {
&self.0.sessions
}
}
impl M2mState for AppState {
fn store(&self) -> &Store {
&self.0.store
}
fn bootstrap(&self) -> &BootstrapAuthority {
&self.0.bootstrap
}
}