56 lines
1.6 KiB
Rust
56 lines
1.6 KiB
Rust
use std::path::PathBuf;
|
|
|
|
use http::StatusCode;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use crate::{http_response::HttpResponse, utils::content_type_for_exension_osstr};
|
|
|
|
#[derive(Clone, Deserialize, Serialize, Debug)]
|
|
pub struct StaticHttpServerRoute {
|
|
pub domain: String,
|
|
#[serde(default = "String::default")]
|
|
pub prefix: String,
|
|
#[serde(default = "PathBuf::default")]
|
|
pub path: PathBuf,
|
|
#[serde(default = "PathBuf::default")]
|
|
pub on_404: PathBuf,
|
|
#[serde(default = "ServingMode::default")]
|
|
pub mode: ServingMode,
|
|
#[serde(default = "String::default")]
|
|
pub redirect: String,
|
|
#[serde(default = "PathBuf::default")]
|
|
pub template_path: PathBuf,
|
|
#[serde(default = "bool::default")]
|
|
pub only_on_dev: bool,
|
|
#[serde(default = "bool::default")]
|
|
pub hsts: bool,
|
|
}
|
|
|
|
impl StaticHttpServerRoute {
|
|
pub fn generate_404(&self) -> Option<HttpResponse> {
|
|
(!self.on_404.clone().into_os_string().is_empty())
|
|
.then(|| std::fs::read(&self.on_404).ok())
|
|
.flatten()
|
|
.map(|contents| {
|
|
HttpResponse::new(
|
|
StatusCode::NOT_FOUND,
|
|
contents.as_slice(),
|
|
content_type_for_exension_osstr(self.on_404.extension()),
|
|
None,
|
|
None,
|
|
false,
|
|
)
|
|
})
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Deserialize, Serialize, Debug, Default)]
|
|
pub enum ServingMode {
|
|
#[default]
|
|
Serving,
|
|
RedirectTo,
|
|
RedirectKeepingRoute,
|
|
RedirectKeepingRouteAndSubdomain,
|
|
Templating,
|
|
}
|