From e33b67a9e36b7dabb6874d7e570936e8ac4b03bb Mon Sep 17 00:00:00 2001 From: ryan Date: Mon, 11 Sep 2023 22:41:37 -0500 Subject: [PATCH] keyforkd: expose start_and_run_server_on and Mnemonic --- keyforkd/src/lib.rs | 50 ++++++++++++++++++++++++----------------- keyforkd/src/service.rs | 2 +- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/keyforkd/src/lib.rs b/keyforkd/src/lib.rs index 341beba..7d4eace 100644 --- a/keyforkd/src/lib.rs +++ b/keyforkd/src/lib.rs @@ -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> { +pub async fn start_and_run_server_on( + mnemonic: Mnemonic, + socket_path: &Path, +) -> Result<(), Box> { 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> { let runtime_vars = std::env::vars() .filter(|(key, _)| ["XDG_RUNTIME_DIR", "KEYFORKD_SOCKET_PATH"].contains(&key.as_str())) .collect::>(); @@ -50,21 +76,5 @@ pub async fn start_and_run_server(mnemonic: Mnemonic) -> Result<(), Box 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 } diff --git a/keyforkd/src/service.rs b/keyforkd/src/service.rs index 3d62ea8..d5da853 100644 --- a/keyforkd/src/service.rs +++ b/keyforkd/src/service.rs @@ -46,7 +46,7 @@ impl Service 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));