2023-07-01 20:19:14 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2023-07-02 01:01:19 +00:00
|
|
|
use http::StatusCode;
|
2023-07-01 20:19:14 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2023-08-12 20:26:47 +00:00
|
|
|
use crate::{http_response::HttpResponse, utils::content_type_for_exension_osstr};
|
2023-07-01 20:19:14 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Deserialize, Serialize, Debug)]
|
|
|
|
pub struct StaticHttpServerRoute {
|
|
|
|
pub domain: String,
|
2023-07-02 01:01:19 +00:00
|
|
|
#[serde(default = "String::default")]
|
2023-07-01 20:19:14 +00:00
|
|
|
pub prefix: String,
|
2023-07-02 01:01:19 +00:00
|
|
|
#[serde(default = "PathBuf::default")]
|
2023-07-01 20:19:14 +00:00
|
|
|
pub path: PathBuf,
|
2023-07-02 01:01:19 +00:00
|
|
|
#[serde(default = "PathBuf::default")]
|
2023-07-01 20:19:14 +00:00
|
|
|
pub on_404: PathBuf,
|
2023-07-02 01:01:19 +00:00
|
|
|
#[serde(default = "ServingMode::default")]
|
|
|
|
pub mode: ServingMode,
|
|
|
|
#[serde(default = "String::default")]
|
|
|
|
pub redirect: String,
|
2023-07-02 05:39:35 +00:00
|
|
|
#[serde(default = "PathBuf::default")]
|
|
|
|
pub template_path: PathBuf,
|
2023-08-09 13:03:07 +00:00
|
|
|
#[serde(default = "bool::default")]
|
|
|
|
pub only_on_dev: bool,
|
2023-08-09 22:51:49 +00:00
|
|
|
#[serde(default = "bool::default")]
|
|
|
|
pub hsts: bool,
|
2023-07-02 01:01:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl StaticHttpServerRoute {
|
|
|
|
pub fn generate_404(&self) -> Option<HttpResponse> {
|
2023-07-02 13:11:38 +00:00
|
|
|
(!self.on_404.clone().into_os_string().is_empty())
|
|
|
|
.then(|| std::fs::read(&self.on_404).ok())
|
|
|
|
.flatten()
|
|
|
|
.map(|contents| {
|
|
|
|
HttpResponse::new(
|
2023-07-02 01:01:19 +00:00
|
|
|
StatusCode::NOT_FOUND,
|
|
|
|
contents.as_slice(),
|
2023-08-12 20:26:47 +00:00
|
|
|
content_type_for_exension_osstr(self.on_404.extension()),
|
2023-07-02 01:01:19 +00:00
|
|
|
None,
|
|
|
|
None,
|
2023-08-09 22:51:49 +00:00
|
|
|
false,
|
2023-07-02 13:11:38 +00:00
|
|
|
)
|
|
|
|
})
|
2023-07-02 01:01:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Deserialize, Serialize, Debug, Default)]
|
|
|
|
pub enum ServingMode {
|
|
|
|
#[default]
|
|
|
|
Serving,
|
|
|
|
RedirectTo,
|
|
|
|
RedirectKeepingRoute,
|
|
|
|
RedirectKeepingRouteAndSubdomain,
|
|
|
|
Templating,
|
2023-07-01 22:10:08 +00:00
|
|
|
}
|