keyforkd: expose start_and_run_server_on and Mnemonic

This commit is contained in:
Ryan Heywood 2023-09-11 22:41:37 -05:00
parent 0f4bc3c78d
commit e33b67a9e3
Signed by: ryan
GPG Key ID: 8E401478A3FBEF72
2 changed files with 31 additions and 21 deletions

View File

@ -1,6 +1,9 @@
use std::{collections::HashMap, path::PathBuf};
use std::{
collections::HashMap,
path::{Path, PathBuf},
};
use keyfork_mnemonic_util::Mnemonic;
pub use keyfork_mnemonic_util::Mnemonic;
use tower::ServiceBuilder;
#[cfg(feature = "tracing")]
@ -14,11 +17,34 @@ use error::KeyforkdError;
use server::UnixServer;
use service::Keyforkd;
pub async fn start_and_run_server(mnemonic: Mnemonic) -> Result<(), Box<dyn std::error::Error>> {
pub async fn start_and_run_server_on(
mnemonic: Mnemonic,
socket_path: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
let service = ServiceBuilder::new()
.layer(middleware::BincodeLayer::new())
.service(Keyforkd::new(mnemonic));
let mut server = match UnixServer::bind(socket_path) {
Ok(s) => s,
Err(e) => {
#[cfg(feature = "tracing")]
debug!(%e, "Encountered error attempting to bind socket: {}", socket_path.display());
return Err(e.into());
}
};
match server.run(service).await {
Ok(_) => (),
Err(e) => {
#[cfg(feature = "tracing")]
debug!(%e, "Encountered error while running");
}
}
Ok(())
}
pub async fn start_and_run_server(mnemonic: Mnemonic) -> Result<(), Box<dyn std::error::Error>> {
let runtime_vars = std::env::vars()
.filter(|(key, _)| ["XDG_RUNTIME_DIR", "KEYFORKD_SOCKET_PATH"].contains(&key.as_str()))
.collect::<HashMap<String, String>>();
@ -50,21 +76,5 @@ pub async fn start_and_run_server(mnemonic: Mnemonic) -> Result<(), Box<dyn std:
runtime_path.display()
);
let mut server = match UnixServer::bind(&runtime_path) {
Ok(s) => s,
Err(e) => {
#[cfg(feature = "tracing")]
debug!(%e, "Encountered error attempting to bind socket: {}", runtime_path.display());
return Err(e.into());
}
};
match server.run(service).await {
Ok(_) => (),
Err(e) => {
#[cfg(feature = "tracing")]
debug!(%e, "Encountered error while running");
}
}
Ok(())
start_and_run_server_on(mnemonic, &runtime_path).await
}

View File

@ -46,7 +46,7 @@ impl Service<DerivationRequest> for Keyforkd {
#[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))]
fn call(&mut self, req: DerivationRequest) -> Self::Future {
let mnemonic = self.mnemonic.clone();
Box::pin(async {
Box::pin(async move {
let len = req.path().len();
if len < 2 {
return Err(KeyforkdRequestError::InvalidDerivationLength(len));