💄 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> {
config: Config,
handlebars: handlebars::Handlebars <'a>,
server_info: file_server::ServerInfo,
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 (
&state.handlebars,
&state.server_info,
file_server_root,
ptth_req.method,
&ptth_req.uri,
@ -93,6 +95,7 @@ async fn handle_all (req: Request <Body>, state: Arc <ServerState <'static>>)
#[derive (Deserialize)]
pub struct ConfigFile {
pub file_server_root: Option <PathBuf>,
pub name: Option <String>,
}
#[tokio::main]
@ -112,6 +115,9 @@ async fn main () -> Result <(), Box <dyn Error>> {
file_server_root: config_file.file_server_root,
},
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),
});

View File

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

View File

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

View File

@ -27,6 +27,7 @@ pub mod file_server;
struct ServerState {
config: Config,
handlebars: Handlebars <'static>,
server_info: file_server::ServerInfo,
client: Client,
hidden_path: Option <PathBuf>,
}
@ -64,6 +65,7 @@ async fn handle_req_resp <'a> (
let response = file_server::serve_all (
&state.handlebars,
&state.server_info,
file_server_root,
parts.method,
&parts.uri,
@ -136,9 +138,14 @@ pub async fn run_server (
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 ());
info! ("Our tripcode is {}", tripcode);
info! ("Server name is {}", config_file.name);
info! ("Tripcode is {}", tripcode);
let mut headers = reqwest::header::HeaderMap::new ();
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,
},
handlebars,
server_info,
client,
hidden_path,
});

View File

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