Refactor util to be just about serving files
This commit is contained in:
parent
fb4a33685b
commit
4c63370a26
|
@ -3,17 +3,13 @@
|
||||||
//! ⚠️ Docs still under construction & API not yet stable ⚠️
|
//! ⚠️ Docs still under construction & API not yet stable ⚠️
|
||||||
#![allow(missing_docs)]
|
#![allow(missing_docs)]
|
||||||
|
|
||||||
#[cfg(feature="serve_dir")]
|
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
#[cfg(feature="serve_dir")]
|
|
||||||
use tokio::{
|
use tokio::{
|
||||||
fs::{self, File},
|
fs::{self, File},
|
||||||
io,
|
io,
|
||||||
};
|
};
|
||||||
#[cfg(feature="serve_dir")]
|
|
||||||
use crate::types::Response;
|
use crate::types::Response;
|
||||||
|
|
||||||
#[cfg(feature="serve_dir")]
|
|
||||||
pub async fn serve_file<P: AsRef<Path>>(path: P, mime: &str) -> Response {
|
pub async fn serve_file<P: AsRef<Path>>(path: P, mime: &str) -> Response {
|
||||||
let path = path.as_ref();
|
let path = path.as_ref();
|
||||||
|
|
||||||
|
@ -31,7 +27,6 @@ pub async fn serve_file<P: AsRef<Path>>(path: P, mime: &str) -> Response {
|
||||||
Response::success(mime, file)
|
Response::success(mime, file)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature="serve_dir")]
|
|
||||||
pub async fn serve_dir<D: AsRef<Path>, P: AsRef<Path>>(dir: D, virtual_path: &[P]) -> Response {
|
pub async fn serve_dir<D: AsRef<Path>, P: AsRef<Path>>(dir: D, virtual_path: &[P]) -> Response {
|
||||||
debug!("Dir: {}", dir.as_ref().display());
|
debug!("Dir: {}", dir.as_ref().display());
|
||||||
let dir = dir.as_ref();
|
let dir = dir.as_ref();
|
||||||
|
@ -85,7 +80,6 @@ pub async fn serve_dir<D: AsRef<Path>, P: AsRef<Path>>(dir: D, virtual_path: &[P
|
||||||
serve_dir_listing(path, virtual_path).await
|
serve_dir_listing(path, virtual_path).await
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature="serve_dir")]
|
|
||||||
async fn serve_dir_listing<P: AsRef<Path>, B: AsRef<Path>>(path: P, virtual_path: &[B]) -> Response {
|
async fn serve_dir_listing<P: AsRef<Path>, B: AsRef<Path>>(path: P, virtual_path: &[B]) -> Response {
|
||||||
let mut dir = match fs::read_dir(path.as_ref()).await {
|
let mut dir = match fs::read_dir(path.as_ref()).await {
|
||||||
Ok(dir) => dir,
|
Ok(dir) => dir,
|
||||||
|
@ -126,7 +120,6 @@ async fn serve_dir_listing<P: AsRef<Path>, B: AsRef<Path>>(path: P, virtual_path
|
||||||
document.into()
|
document.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature="serve_dir")]
|
|
||||||
pub fn guess_mime_from_path<P: AsRef<Path>>(path: P) -> &'static str {
|
pub fn guess_mime_from_path<P: AsRef<Path>>(path: P) -> &'static str {
|
||||||
let path = path.as_ref();
|
let path = path.as_ref();
|
||||||
let extension = path.extension().and_then(|s| s.to_str());
|
let extension = path.extension().and_then(|s| s.to_str());
|
||||||
|
@ -142,7 +135,6 @@ pub fn guess_mime_from_path<P: AsRef<Path>>(path: P) -> &'static str {
|
||||||
mime_guess::from_ext(extension).first_raw().unwrap_or("application/octet-stream")
|
mime_guess::from_ext(extension).first_raw().unwrap_or("application/octet-stream")
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature="serve_dir")]
|
|
||||||
/// Print a warning to the log asking to file an issue and respond with "Unexpected Error"
|
/// Print a warning to the log asking to file an issue and respond with "Unexpected Error"
|
||||||
pub (crate) fn warn_unexpected(err: impl std::fmt::Debug, path: &Path, line: u32) -> Response {
|
pub (crate) fn warn_unexpected(err: impl std::fmt::Debug, path: &Path, line: u32) -> Response {
|
||||||
warn!(
|
warn!(
|
||||||
|
@ -157,19 +149,3 @@ pub (crate) fn warn_unexpected(err: impl std::fmt::Debug, path: &Path, line: u32
|
||||||
);
|
);
|
||||||
Response::temporary_failure("Unexpected error")
|
Response::temporary_failure("Unexpected error")
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A convenience trait alias for `AsRef<T> + Into<T::Owned>`,
|
|
||||||
/// most commonly used to accept `&str` or `String`:
|
|
||||||
///
|
|
||||||
/// `Cowy<str>` ⇔ `AsRef<str> + Into<String>`
|
|
||||||
pub trait Cowy<T>
|
|
||||||
where
|
|
||||||
Self: AsRef<T> + Into<T::Owned>,
|
|
||||||
T: ToOwned + ?Sized,
|
|
||||||
{}
|
|
||||||
|
|
||||||
impl<C, T> Cowy<T> for C
|
|
||||||
where
|
|
||||||
C: AsRef<T> + Into<T::Owned>,
|
|
||||||
T: ToOwned + ?Sized,
|
|
||||||
{}
|
|
|
@ -235,7 +235,8 @@ use rustls::Session;
|
||||||
|
|
||||||
mod types;
|
mod types;
|
||||||
mod handling;
|
mod handling;
|
||||||
pub mod util;
|
#[cfg(feature = "serve_dir")]
|
||||||
|
pub mod files;
|
||||||
pub mod routing;
|
pub mod routing;
|
||||||
#[cfg(feature = "ratelimiting")]
|
#[cfg(feature = "ratelimiting")]
|
||||||
mod ratelimiting;
|
mod ratelimiting;
|
||||||
|
|
Loading…
Reference in a new issue