♻️ 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]]
name = "ptth_core"
version = "1.4.0"
version = "2.0.0"
dependencies = [
"base64",
"ctrlc",
@ -1529,7 +1529,7 @@ dependencies = [
[[package]]
name = "ptth_relay"
version = "1.3.0"
version = "2.0.0"
dependencies = [
"base64",
"blake3",

View File

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

View File

@ -22,19 +22,6 @@ pub mod prelude;
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
pub fn gen_key () -> String {
@ -48,16 +35,17 @@ pub fn gen_key () -> String {
#[cfg (test)]
mod tests {
use super::*;
#[test]
fn prefix () {
for (p, h, expected) in &[
("/files/", "/files/a", Some ("a")),
("/files/", "/files/abc/def", Some ("abc/def")),
("/files/", "/files", None),
("/files/", "/not_files", None),
("/files/", "/files/", Some ("")),
] {
assert_eq! (prefix_match (*p, *h), *expected);
}
fn test_gen_key () {
// Smoke test
let blank = base64::encode (&vec! [0_u8; 64]);
let a = gen_key ();
let b = gen_key ();
assert_ne! (blank, a);
assert_ne! (blank, b);
assert_ne! (a, b);
}
}

View File

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

View File

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

View File

@ -26,7 +26,6 @@ use crate::{
BlakeHashWrapper,
KeyValidity,
},
prefix_match,
relay_state::RelayState,
};
@ -160,7 +159,7 @@ async fn api_v1 (
let x = v1_server_list (&state).await;
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
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
}
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
}
else {

View File

@ -43,7 +43,7 @@ toml = "0.5.7"
uom = "0.30.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]