Skip to content

Commit

Permalink
refactor: adjust embedded file for serve
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Mar 31, 2024
1 parent a3c1f69 commit eb9a7e7
Show file tree
Hide file tree
Showing 12 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pingap"
version = "0.1.4"
version = "0.1.5"
authors = ["Tree Xie <tree.xie@outlook.com>"]
edition = "2021"
categories = ["network-programming", "web-programming::http-server"]
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod cache;
pub mod config;
pub mod http_extra;
pub mod proxy;
pub mod serve;
pub mod state;
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/proxy/location.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
6 changes: 3 additions & 3 deletions src/proxy/server.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions src/serve/admin.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 5 additions & 6 deletions src/serve/static_file.rs → src/serve/embedded_file.rs
Original file line number Diff line number Diff line change
@@ -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<EmbeddedFile>, pub u32);

pub struct StaticFile(pub Option<EmbeddedFile>);

impl From<StaticFile> for HttpResponse {
fn from(value: StaticFile) -> Self {
impl From<EmbeddedStaticFile> for HttpResponse {
fn from(value: EmbeddedStaticFile) -> Self {
if value.0.is_none() {
return HttpResponse::not_found();
}
Expand All @@ -25,7 +24,7 @@ impl From<StaticFile> for HttpResponse {
let max_age = if mime_type.contains("text/html") {
0
} else {
24 * 3600
value.1
};

let mut headers = vec![];
Expand Down
2 changes: 1 addition & 1 deletion src/serve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit eb9a7e7

Please sign in to comment.