use crate::*;
use lair_keystore_api::ipc_keystore::IpcKeystoreServer;
use std::future::Future;
pub struct StandaloneServer {
config: LairServerConfig,
srv_hnd: Option<IpcKeystoreServer>,
}
impl StandaloneServer {
pub async fn new(config: LairServerConfig) -> LairResult<Self> {
{
let config = config.clone();
tokio::task::spawn_blocking(move || {
crate::pid_check::pid_check(&config)
})
.await
.map_err(one_err::OneErr::new)??;
}
if !tokio::fs::metadata(
config.store_file.parent().expect("invalid store_file dir"),
)
.await?
.is_dir()
{
return Err("invalid store file directory".into());
}
match tokio::fs::metadata(&config.store_file).await {
Err(_) => (),
Ok(m) => {
if !m.is_file() {
return Err("store file is not a file".into());
}
}
}
Ok(Self {
config,
srv_hnd: None,
})
}
pub async fn run(
&mut self,
passphrase: SharedLockedArray,
) -> LairResult<()> {
let store_factory = crate::store_sqlite::create_sql_pool_factory(
&self.config.store_file,
&self.config.database_salt,
);
let srv_hnd = IpcKeystoreServer::new(
self.config.clone(),
store_factory,
passphrase,
)
.await?;
println!(
"# lair-keystore connection_url # {} #",
srv_hnd.get_config().connection_url
);
println!("# lair-keystore running #");
self.srv_hnd = Some(srv_hnd);
Ok(())
}
pub fn store(
&self,
) -> impl Future<Output = LairResult<LairStore>> + 'static + Send {
let srv_hnd = self.srv_hnd.clone();
async move {
let srv_hnd = srv_hnd.ok_or_else(|| {
one_err::OneErr::new("server not yet running")
})?;
srv_hnd.store().await
}
}
}