Skip to content

Commit

Permalink
fix: refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
conblem committed Feb 23, 2021
1 parent ac803cb commit 9f95b25
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ tracing = "0.1"
tracing-subscriber = { version = "0.2", features = ["parking_lot"] }
tracing-futures = { version = "0.2", features = ["futures-03"] }
sqlx = { version = "0.5", default-features = false, features = ["runtime-tokio-rustls", "macros", "migrate"] }
tokio = { version = "1.0", features = ["rt-multi-thread", "net", "signal", "macros", "test-util", "time"] }
tokio = { version = "1.0", features = ["rt-multi-thread", "net", "signal", "macros", "time"] }
tokio-stream = { version = "0.1", features = ["net"] }
tokio-util = "0.6"
tokio-rustls = "0.22"
Expand Down
2 changes: 1 addition & 1 deletion src/api/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl MetricsConfig {
}
}

// type magic to return a closure which returns a unable warp filter
// type magic to return a closure which returns a unnameable warp filter
trait MetricsWrapper<I>: Fn(I) -> <Self as MetricsWrapper<I>>::Output
where
I: Filter<Extract = (WarpResponse, MetricsConfig), Error = Rejection> + Clone + Send + 'static,
Expand Down
2 changes: 1 addition & 1 deletion src/api/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub(crate) fn wrap(
}
Ok(None) => {}
Err(e) => {
error!("Could net get remote.addr: {}", e);
error!("Could net get remote.real: {}", e);
}
}
Ok(conn)
Expand Down
20 changes: 8 additions & 12 deletions src/api/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::{anyhow, Result};
use futures_util::stream::{repeat, Stream};
use futures_util::{StreamExt, TryFutureExt, TryStreamExt};
use parking_lot::RwLock;
use rustls::internal::pemfile::{certs, pkcs8_private_keys};
use rustls::internal::pemfile::{certs, pkcs8_private_keys, rsa_private_keys};
use rustls::{NoClientAuth, ServerConfig};
use std::future::Future;
use std::sync::Arc;
Expand Down Expand Up @@ -45,7 +45,7 @@ where
L: Stream<Item = IoResult<I>> + Send + 'static,
I: Future<Output = IoResult<S>> + Send + 'static,
S: AsyncRead + AsyncWrite + Send + Unpin + 'static,
A: Fn() -> F + Clone + Send + 'static,
A: FnOnce() -> F + Clone + Send + 'static,
F: Future<Output = Result<TlsAcceptor>>,
{
listener
Expand All @@ -59,13 +59,13 @@ where
}

// used for expressing a closure with an impl return
trait Func: Fn() -> <Self as Func>::Output {
trait Func: FnOnce() -> <Self as Func>::Output {
type Output;
}

impl<F, O> Func for F
where
F: Fn() -> O,
F: FnOnce() -> O,
{
type Output = O;
}
Expand All @@ -83,13 +83,9 @@ where
let config = RwLock::new((None, Arc::new(server_config)));
let wrapper = Arc::new((facade, config));

// workarround to make closure Fn instead of FnOnce
move || {
let wrapper = Arc::clone(&wrapper);
async move {
let (facade, config) = &*wrapper;
load_cert(facade, config).await
}
|| async move {
let (facade, config) = &*wrapper;
load_cert(facade, config).await
}
}

Expand Down Expand Up @@ -141,7 +137,7 @@ fn create_server_config(db_cert: &Cert) -> Result<Arc<ServerConfig>> {
};

let mut privates =
pkcs8_private_keys(&mut private.as_bytes()).map_err(|_| anyhow!("Private is invalid"))?;
rsa_private_keys(&mut private.as_bytes()).map_err(|_| anyhow!("Private is invalid"))?;
let private = privates
.pop()
.ok_or_else(|| anyhow!("Private Vec is empty"))?;
Expand Down
13 changes: 12 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::Error;
use std::fmt::{Debug, Display};
use std::io::{Error as IoError, ErrorKind};
use std::time::{SystemTime, UNIX_EPOCH};
use uuid::Uuid;
Expand All @@ -18,8 +19,18 @@ pub fn now() -> u64 {
.as_secs()
}

pub(crate) fn error<I: Into<Error>, E: From<IoError>>(err: I) -> E {
pub(crate) fn error<I, E>(err: I) -> E
where
I: Into<Error>,
E: From<IoError> + Display + Debug + Send + Sync + 'static,
{
let err = err.into();

let err = match err.downcast::<E>() {
Ok(err) => return err,
Err(err) => err,
};

let err = match err.downcast::<IoError>() {
Ok(err) => err,
Err(err) => IoError::new(ErrorKind::Other, err),
Expand Down

0 comments on commit 9f95b25

Please sign in to comment.