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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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<P>(&mut self, passphrase: P) -> LairResult<()>
where
P: Into<sodoken::BufRead> + 'static + Send,
{
let passphrase = passphrase.into();
let store_factory = crate::store_sqlite::create_sql_pool_factory(
&self.config.store_file,
);
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
}
}
}