♻️ refactor: make `load_templates` private

main
_ 2021-04-17 20:50:48 -05:00
parent ae4f102bdf
commit c6f651387e
8 changed files with 24 additions and 36 deletions

4
Cargo.lock generated
View File

@ -1443,7 +1443,7 @@ dependencies = [
[[package]] [[package]]
name = "ptth_core" name = "ptth_core"
version = "1.4.0" version = "2.0.0"
dependencies = [ dependencies = [
"base64", "base64",
"ctrlc", "ctrlc",
@ -1529,7 +1529,7 @@ dependencies = [
[[package]] [[package]]
name = "ptth_relay" name = "ptth_relay"
version = "1.3.0" version = "2.0.0"
dependencies = [ dependencies = [
"base64", "base64",
"blake3", "blake3",

View File

@ -1,7 +1,7 @@
[package] [package]
name = "ptth_core" name = "ptth_core"
version = "1.4.0" version = "2.0.0"
authors = ["Trish"] authors = ["Trish"]
edition = "2018" edition = "2018"
license = "AGPL-3.0" license = "AGPL-3.0"

View File

@ -22,19 +22,6 @@ pub mod prelude;
pub const PTTH_MAGIC_HEADER: &str = "X-PTTH-2LJYXWC4"; pub const PTTH_MAGIC_HEADER: &str = "X-PTTH-2LJYXWC4";
// The arguments are in order so they are in order overall:
// e.g. prefix_match ("/prefix", "/prefix/middle/suffix") -> "/middle/suffix"
#[must_use]
#[deprecated (
since = "1.4.0",
note = "Use `str::strip_prefix instead`"
)]
pub fn prefix_match <'a> (prefix: &str, hay: &'a str) -> Option <&'a str>
{
hay.strip_prefix (prefix)
}
/// Generates 64 bytes of entropy and returns it as Base64 /// Generates 64 bytes of entropy and returns it as Base64
pub fn gen_key () -> String { pub fn gen_key () -> String {
@ -48,16 +35,17 @@ pub fn gen_key () -> String {
#[cfg (test)] #[cfg (test)]
mod tests { mod tests {
use super::*; use super::*;
#[test] #[test]
fn prefix () { fn test_gen_key () {
for (p, h, expected) in &[ // Smoke test
("/files/", "/files/a", Some ("a")),
("/files/", "/files/abc/def", Some ("abc/def")), let blank = base64::encode (&vec! [0_u8; 64]);
("/files/", "/files", None), let a = gen_key ();
("/files/", "/not_files", None), let b = gen_key ();
("/files/", "/files/", Some ("")),
] { assert_ne! (blank, a);
assert_eq! (prefix_match (*p, *h), *expected); assert_ne! (blank, b);
} assert_ne! (a, b);
} }
} }

View File

@ -35,4 +35,4 @@ tracing = "0.1.25"
tracing-futures = "0.2.4" tracing-futures = "0.2.4"
tracing-subscriber = "0.2.15" tracing-subscriber = "0.2.15"
ptth_core = { path = "../ptth_core", version = "1.4.0" } ptth_core = { path = "../ptth_core", version = "2.0.0" }

View File

@ -56,7 +56,6 @@ use tokio_stream::wrappers::ReceiverStream;
use ptth_core::{ use ptth_core::{
http_serde, http_serde,
prefix_match,
prelude::*, prelude::*,
}; };
@ -559,7 +558,7 @@ async fn handle_all (
Ok (response) Ok (response)
} }
pub fn load_templates (asset_root: &Path) fn load_templates (asset_root: &Path)
-> Result <Handlebars <'static>, RelayError> -> Result <Handlebars <'static>, RelayError>
{ {
let mut handlebars = Handlebars::new (); let mut handlebars = Handlebars::new ();
@ -606,7 +605,7 @@ async fn reload_config (
pub async fn run_relay ( pub async fn run_relay (
state: Arc <RelayState>, state: Arc <RelayState>,
handlebars: Arc <Handlebars <'static>>, asset_root: &Path,
shutdown_oneshot: oneshot::Receiver <()>, shutdown_oneshot: oneshot::Receiver <()>,
config_reload_path: Option <PathBuf> config_reload_path: Option <PathBuf>
) )
@ -617,6 +616,8 @@ pub async fn run_relay (
AuditEvent, AuditEvent,
}; };
let handlebars = Arc::new (load_templates (asset_root)?);
if let Some (x) = git_version::read ().await { if let Some (x) = git_version::read ().await {
info! ("ptth_relay Git version: {:?}", x); info! ("ptth_relay Git version: {:?}", x);
} }

View File

@ -55,7 +55,7 @@ async fn main () -> Result <(), Box <dyn Error>> {
forced_shutdown.wrap_server ( forced_shutdown.wrap_server (
run_relay ( run_relay (
Arc::new (RelayState::try_from (config)?), Arc::new (RelayState::try_from (config)?),
Arc::new (ptth_relay::load_templates (&PathBuf::new ())?), &PathBuf::new (),
shutdown_rx, shutdown_rx,
Some (config_path) Some (config_path)
) )

View File

@ -26,7 +26,6 @@ use crate::{
BlakeHashWrapper, BlakeHashWrapper,
KeyValidity, KeyValidity,
}, },
prefix_match,
relay_state::RelayState, relay_state::RelayState,
}; };
@ -160,7 +159,7 @@ async fn api_v1 (
let x = v1_server_list (&state).await; let x = v1_server_list (&state).await;
Ok (error_reply (StatusCode::OK, &serde_json::to_string (&x).unwrap ())?) Ok (error_reply (StatusCode::OK, &serde_json::to_string (&x).unwrap ())?)
} }
else if let Some (rest) = prefix_match ("server/", path_rest) { else if let Some (rest) = path_rest.strip_prefix ("server/") {
// DRY T4H76LB3 // DRY T4H76LB3
if let Some (idx) = rest.find ('/') { if let Some (idx) = rest.find ('/') {
@ -196,10 +195,10 @@ pub async fn handle (
} }
} }
if let Some (rest) = prefix_match ("v1/", path_rest) { if let Some (rest) = path_rest.strip_prefix ("v1/") {
api_v1 (req, state, rest).await api_v1 (req, state, rest).await
} }
else if let Some (rest) = prefix_match ("api/", path_rest) { else if let Some (rest) = path_rest.strip_prefix ("api/") {
api_v1 (req, state, rest).await api_v1 (req, state, rest).await
} }
else { else {

View File

@ -43,7 +43,7 @@ toml = "0.5.7"
uom = "0.30.0" uom = "0.30.0"
always_equal = { path = "../always_equal", version = "1.0.0" } always_equal = { path = "../always_equal", version = "1.0.0" }
ptth_core = { path = "../ptth_core", version = "1.4.0" } ptth_core = { path = "../ptth_core", version = "2.0.0" }
[dev-dependencies] [dev-dependencies]