diff --git a/Cargo.lock b/Cargo.lock index 72af77cf..dc28a2cd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1550,7 +1550,7 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "pingap" -version = "0.1.4" +version = "0.1.5" dependencies = [ "async-trait", "base64 0.22.0", diff --git a/Cargo.toml b/Cargo.toml index 18c730aa..75f3cd11 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pingap" -version = "0.1.4" +version = "0.1.5" authors = ["Tree Xie "] edition = "2021" categories = ["network-programming", "web-programming::http-server"] diff --git a/src/cache/http_header.rs b/src/http_extra/http_header.rs similarity index 100% rename from src/cache/http_header.rs rename to src/http_extra/http_header.rs diff --git a/src/cache/http_response.rs b/src/http_extra/http_response.rs similarity index 99% rename from src/cache/http_response.rs rename to src/http_extra/http_response.rs index 3a2368b4..70b932a5 100644 --- a/src/cache/http_response.rs +++ b/src/http_extra/http_response.rs @@ -118,7 +118,7 @@ impl HttpResponse { #[cfg(test)] mod tests { use super::HttpResponse; - use crate::cache::convert_headers; + use crate::http_extra::convert_headers; use bytes::Bytes; use http::StatusCode; use pretty_assertions::assert_eq; diff --git a/src/cache/mod.rs b/src/http_extra/mod.rs similarity index 100% rename from src/cache/mod.rs rename to src/http_extra/mod.rs diff --git a/src/lib.rs b/src/lib.rs index b3eb9e9b..af8e31cd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ -pub mod cache; pub mod config; +pub mod http_extra; pub mod proxy; pub mod serve; pub mod state; diff --git a/src/main.rs b/src/main.rs index 3335cc22..65f51d3d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,8 +9,8 @@ use std::error::Error; use std::io::Write; use std::sync::Arc; -mod cache; mod config; +mod http_extra; mod proxy; mod serve; mod state; diff --git a/src/proxy/location.rs b/src/proxy/location.rs index 0231d21c..8cb48177 100644 --- a/src/proxy/location.rs +++ b/src/proxy/location.rs @@ -1,6 +1,6 @@ use super::Upstream; -use crate::cache::{convert_headers, HttpHeader}; use crate::config::LocationConf; +use crate::http_extra::{convert_headers, HttpHeader}; use pingora::http::{RequestHeader, ResponseHeader}; use regex::Regex; use snafu::{ResultExt, Snafu}; diff --git a/src/proxy/server.rs b/src/proxy/server.rs index cbfc09ba..800f410f 100644 --- a/src/proxy/server.rs +++ b/src/proxy/server.rs @@ -1,7 +1,7 @@ use super::logger::Parser; use super::{Location, Upstream}; -use crate::cache; use crate::config::{LocationConf, PingapConf, UpstreamConf}; +use crate::http_extra::{HttpResponse, HTTP_HEADER_CONTENT_JSON}; use crate::serve::Serve; use crate::serve::ADMIN_SERVE; use crate::state::State; @@ -276,10 +276,10 @@ impl Server { }) .unwrap_or_default(); - let size = cache::HttpResponse { + let size = HttpResponse { status: StatusCode::OK, body: Bytes::from(buf), - headers: Some(vec![cache::HTTP_HEADER_CONTENT_JSON.clone()]), + headers: Some(vec![HTTP_HEADER_CONTENT_JSON.clone()]), ..Default::default() } .send(session) diff --git a/src/serve/admin.rs b/src/serve/admin.rs index 1e76de89..332e43ac 100644 --- a/src/serve/admin.rs +++ b/src/serve/admin.rs @@ -1,9 +1,10 @@ -use super::static_file::StaticFile; +use super::embedded_file::EmbeddedStaticFile; use super::Serve; +use crate::config::PingapConf; use crate::config::{self, get_start_time, save_config, LocationConf, ServerConf, UpstreamConf}; +use crate::http_extra::HttpResponse; use crate::state::State; use crate::utils::get_pkg_version; -use crate::{cache::HttpResponse, config::PingapConf}; use async_trait::async_trait; use http::{Method, StatusCode}; use log::error; @@ -200,7 +201,7 @@ impl Serve for AdminServe { if file.is_empty() { file = "index.html"; } - StaticFile(AdminAsset::get(file)).into() + EmbeddedStaticFile(AdminAsset::get(file), 365 * 24 * 3600).into() }; ctx.response_body_size = resp.send(session).await?; Ok(true) diff --git a/src/serve/static_file.rs b/src/serve/embedded_file.rs similarity index 85% rename from src/serve/static_file.rs rename to src/serve/embedded_file.rs index e675da08..94d7ceee 100644 --- a/src/serve/static_file.rs +++ b/src/serve/embedded_file.rs @@ -1,14 +1,13 @@ +use crate::http_extra::HttpResponse; use bytes::Bytes; use hex::encode; use http::{header, HeaderValue, StatusCode}; use rust_embed::EmbeddedFile; -use crate::cache::HttpResponse; +pub struct EmbeddedStaticFile(pub Option, pub u32); -pub struct StaticFile(pub Option); - -impl From for HttpResponse { - fn from(value: StaticFile) -> Self { +impl From for HttpResponse { + fn from(value: EmbeddedStaticFile) -> Self { if value.0.is_none() { return HttpResponse::not_found(); } @@ -25,7 +24,7 @@ impl From for HttpResponse { let max_age = if mime_type.contains("text/html") { 0 } else { - 24 * 3600 + value.1 }; let mut headers = vec![]; diff --git a/src/serve/mod.rs b/src/serve/mod.rs index 610b82c6..b9f6a63c 100644 --- a/src/serve/mod.rs +++ b/src/serve/mod.rs @@ -3,7 +3,7 @@ use async_trait::async_trait; use pingora::proxy::Session; mod admin; -mod static_file; +mod embedded_file; #[async_trait] pub trait Serve {