💄 Show file server name in directory pages

main
_ 2020-11-10 00:44:21 +00:00
parent 1d469c8dff
commit 63abdc3a16
5 changed files with 36 additions and 26 deletions

View File

@ -32,6 +32,7 @@ pub struct Config {
struct ServerState <'a> { struct ServerState <'a> {
config: Config, config: Config,
handlebars: handlebars::Handlebars <'a>, handlebars: handlebars::Handlebars <'a>,
server_info: file_server::ServerInfo,
hidden_path: Option <PathBuf>, hidden_path: Option <PathBuf>,
} }
@ -64,6 +65,7 @@ async fn handle_all (req: Request <Body>, state: Arc <ServerState <'static>>)
let ptth_resp = file_server::serve_all ( let ptth_resp = file_server::serve_all (
&state.handlebars, &state.handlebars,
&state.server_info,
file_server_root, file_server_root,
ptth_req.method, ptth_req.method,
&ptth_req.uri, &ptth_req.uri,
@ -93,6 +95,7 @@ async fn handle_all (req: Request <Body>, state: Arc <ServerState <'static>>)
#[derive (Deserialize)] #[derive (Deserialize)]
pub struct ConfigFile { pub struct ConfigFile {
pub file_server_root: Option <PathBuf>, pub file_server_root: Option <PathBuf>,
pub name: Option <String>,
} }
#[tokio::main] #[tokio::main]
@ -112,6 +115,9 @@ async fn main () -> Result <(), Box <dyn Error>> {
file_server_root: config_file.file_server_root, file_server_root: config_file.file_server_root,
}, },
handlebars, handlebars,
server_info: crate::file_server::ServerInfo {
server_name: config_file.name.unwrap_or_else (|| "PTTH File Server".to_string ()).clone (),
},
hidden_path: Some (path), hidden_path: Some (path),
}); });

View File

@ -66,6 +66,9 @@ mod tests {
use crate::password_is_bad; use crate::password_is_bad;
for pw in &[ for pw in &[
"",
" ",
"user",
"password", "password",
"pAsSwOrD", "pAsSwOrD",
"secret", "secret",

View File

@ -46,9 +46,9 @@ use crate::{
prefix_match, prefix_match,
}; };
#[derive (Serialize)] #[derive (Debug, Serialize)]
struct ServerInfo <'a> { pub struct ServerInfo {
server_name: &'a str, pub server_name: String,
} }
#[derive (Serialize)] #[derive (Serialize)]
@ -75,7 +75,7 @@ struct TemplateDirEntry {
#[derive (Serialize)] #[derive (Serialize)]
struct TemplateDirPage <'a> { struct TemplateDirPage <'a> {
#[serde (flatten)] #[serde (flatten)]
server_info: ServerInfo <'a>, server_info: &'a ServerInfo,
path: Cow <'a, str>, path: Cow <'a, str>,
entries: Vec <TemplateDirEntry>, entries: Vec <TemplateDirEntry>,
@ -224,12 +224,9 @@ async fn read_dir_entry (entry: DirEntry) -> TemplateDirEntry
async fn serve_root ( async fn serve_root (
handlebars: &Handlebars <'static>, handlebars: &Handlebars <'static>,
server_info: &ServerInfo
) -> Response ) -> Response
{ {
let server_info = ServerInfo {
server_name: "PTTH file server",
};
let s = handlebars.render ("file_server_root", &server_info).unwrap (); let s = handlebars.render ("file_server_root", &server_info).unwrap ();
let body = s.into_bytes (); let body = s.into_bytes ();
@ -244,14 +241,11 @@ async fn serve_root (
#[instrument (level = "debug", skip (handlebars, dir))] #[instrument (level = "debug", skip (handlebars, dir))]
async fn serve_dir ( async fn serve_dir (
handlebars: &Handlebars <'static>, handlebars: &Handlebars <'static>,
server_info: &ServerInfo,
path: Cow <'_, str>, path: Cow <'_, str>,
mut dir: ReadDir mut dir: ReadDir
) -> Response ) -> Response
{ {
let server_info = ServerInfo {
server_name: "PTTH file server",
};
let mut entries = vec! []; let mut entries = vec! [];
while let Ok (Some (entry)) = dir.next_entry ().await { while let Ok (Some (entry)) = dir.next_entry ().await {
@ -521,6 +515,7 @@ async fn internal_serve_all (
#[instrument (level = "debug", skip (handlebars, headers))] #[instrument (level = "debug", skip (handlebars, headers))]
pub async fn serve_all ( pub async fn serve_all (
handlebars: &Handlebars <'static>, handlebars: &Handlebars <'static>,
server_info: &ServerInfo,
root: &Path, root: &Path,
method: Method, method: Method,
uri: &str, uri: &str,
@ -544,11 +539,11 @@ pub async fn serve_all (
}, },
Redirect (location) => serve_307 (location), Redirect (location) => serve_307 (location),
Root => serve_root (handlebars).await, Root => serve_root (handlebars, server_info).await,
ServeDir (ServeDirParams { ServeDir (ServeDirParams {
path, path,
dir, dir,
}) => serve_dir (handlebars, path.to_string_lossy (), dir.into_inner ()).await, }) => serve_dir (handlebars, server_info, path.to_string_lossy (), dir.into_inner ()).await,
ServeFile (ServeFileParams { ServeFile (ServeFileParams {
file, file,
send_body, send_body,
@ -694,20 +689,17 @@ mod tests {
http_serde::Method, http_serde::Method,
//prelude::*, //prelude::*,
}; };
use super::{ use super::*;
InternalResponse,
internal_serve_all,
load_templates,
serve_all,
ServeDirParams,
ServeFileParams,
};
tracing_subscriber::fmt ().try_init ().ok (); tracing_subscriber::fmt ().try_init ().ok ();
let mut rt = Runtime::new ().unwrap (); let mut rt = Runtime::new ().unwrap ();
rt.block_on (async { rt.block_on (async {
let handlebars = load_templates ().unwrap (); let handlebars = load_templates ().unwrap ();
let server_info = ServerInfo {
server_name: "PTTH File Server".to_string (),
};
let file_server_root = PathBuf::from ("./"); let file_server_root = PathBuf::from ("./");
let headers = Default::default (); let headers = Default::default ();
@ -719,6 +711,7 @@ mod tests {
].into_iter () { ].into_iter () {
let resp = serve_all ( let resp = serve_all (
&handlebars, &handlebars,
&server_info,
&file_server_root, &file_server_root,
Method::Get, Method::Get,
uri_path, uri_path,

View File

@ -27,6 +27,7 @@ pub mod file_server;
struct ServerState { struct ServerState {
config: Config, config: Config,
handlebars: Handlebars <'static>, handlebars: Handlebars <'static>,
server_info: file_server::ServerInfo,
client: Client, client: Client,
hidden_path: Option <PathBuf>, hidden_path: Option <PathBuf>,
} }
@ -64,6 +65,7 @@ async fn handle_req_resp <'a> (
let response = file_server::serve_all ( let response = file_server::serve_all (
&state.handlebars, &state.handlebars,
&state.server_info,
file_server_root, file_server_root,
parts.method, parts.method,
&parts.uri, &parts.uri,
@ -136,9 +138,14 @@ pub async fn run_server (
panic! ("API key is too weak, server can't use it"); panic! ("API key is too weak, server can't use it");
} }
let server_info = file_server::ServerInfo {
server_name: config_file.name.clone (),
};
let tripcode = base64::encode (blake3::hash (config_file.api_key.as_bytes ()).as_bytes ()); let tripcode = base64::encode (blake3::hash (config_file.api_key.as_bytes ()).as_bytes ());
info! ("Our tripcode is {}", tripcode); info! ("Server name is {}", config_file.name);
info! ("Tripcode is {}", tripcode);
let mut headers = reqwest::header::HeaderMap::new (); let mut headers = reqwest::header::HeaderMap::new ();
headers.insert ("X-ApiKey", config_file.api_key.try_into ().unwrap ()); headers.insert ("X-ApiKey", config_file.api_key.try_into ().unwrap ());
@ -155,6 +162,7 @@ pub async fn run_server (
file_server_root: config_file.file_server_root, file_server_root: config_file.file_server_root,
}, },
handlebars, handlebars,
server_info,
client, client,
hidden_path, hidden_path,
}); });

View File

@ -1,7 +1,7 @@
- "Preview as" feature for Markdown / pretty-printed logs - "Preview as" feature for Markdown / pretty-printed logs
- Make a debug client to replicate the issue Firefox is having with turtling
- Add Prometheus metrics - Add Prometheus metrics
- Not working great behind reverse proxies - Not working great behind reverse proxies
- Show file server names in HTML
- Impl multi-range / multi-part byte serving - Impl multi-range / multi-part byte serving
- Deny unused HTTP methods for endpoints - Deny unused HTTP methods for endpoints