From 2384ffe5c9b886733b4050a678cb3a3a049c8cf9 Mon Sep 17 00:00:00 2001 From: vicanso Date: Sat, 16 Nov 2024 18:25:35 +0800 Subject: [PATCH] refactor: add static serve example --- examples/static-serve.toml | 28 ++++++++++++++++++++++++++++ src/config/file.rs | 15 +++++++++++++++ src/plugin/directory.rs | 9 ++++++++- src/proxy/server.rs | 6 +++++- web/src/i18n/en.ts | 3 ++- web/src/i18n/zh.ts | 2 +- 6 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 examples/static-serve.toml diff --git a/examples/static-serve.toml b/examples/static-serve.toml new file mode 100644 index 00000000..31c91108 --- /dev/null +++ b/examples/static-serve.toml @@ -0,0 +1,28 @@ +[locations.staticServe] +plugins = [ + "staticAcceptEncoding", + "staticCompression", + "staticServe", +] + +[plugins.staticAcceptEncoding] +category = "accept_encoding" +encodings = "zstd, br, gzip" +only_one_encoding = true +step = "request" + +[plugins.staticCompression] +br_level = 9 +category = "compression" +gzip_level = 9 +step = "request" +zstd_level = 9 + +[plugins.staticServe] +category = "directory" +path = "~/github/pingap/dist" +step = "request" + +[servers.staticServe] +addr = "127.0.0.1:3000" +locations = ["staticServe"] diff --git a/src/config/file.rs b/src/config/file.rs index a25da05c..237cd0b1 100644 --- a/src/config/file.rs +++ b/src/config/file.rs @@ -100,6 +100,21 @@ impl ConfigStorage for FileStorage { if Path::new(&filepath).is_file() { let ping_conf = toml::to_string_pretty(&conf) .map_err(|e| Error::Ser { source: e })?; + let mut values: toml::Table = toml::from_str(&ping_conf) + .map_err(|e| Error::De { source: e })?; + let mut omit_keys = vec![]; + for key in values.keys() { + if let Some(value) = values.get(key) { + if value.to_string() == "{}" { + omit_keys.push(key.clone()); + } + } + } + for key in omit_keys { + values.remove(&key); + } + let ping_conf = toml::to_string_pretty(&values) + .map_err(|e| Error::Ser { source: e })?; return fs::write(&filepath, ping_conf).await.map_err(|e| { Error::Io { source: e, diff --git a/src/plugin/directory.rs b/src/plugin/directory.rs index d90d77c5..b5f24f93 100644 --- a/src/plugin/directory.rs +++ b/src/plugin/directory.rs @@ -210,10 +210,17 @@ impl TryFrom<&PluginConf> for Directory { let cache_private = get_bool_conf(value, "private"); let cache_private = if cache_private { Some(true) } else { None }; + let mut index = get_str_conf(value, "index"); + if index.is_empty() { + index = "index.html".to_string(); + } + if !index.starts_with("/") { + index = format!("/{index}"); + } let params = Self { hash_value, autoindex: get_bool_conf(value, "autoindex"), - index: get_str_conf(value, "index"), + index, path: Path::new(&util::resolve_path(&get_str_conf(value, "path"))) .to_path_buf(), chunk_size, diff --git a/src/proxy/server.rs b/src/proxy/server.rs index 165ed762..827767d1 100644 --- a/src/proxy/server.rs +++ b/src/proxy/server.rs @@ -57,7 +57,9 @@ use pingora::cache::{ }; use pingora::http::{RequestHeader, ResponseHeader}; use pingora::listeners::TcpSocketOptions; -use pingora::modules::http::compression::ResponseCompression; +use pingora::modules::http::compression::{ + ResponseCompression, ResponseCompressionBuilder, +}; use pingora::modules::http::grpc_web::{GrpcWeb, GrpcWebBridge}; use pingora::modules::http::HttpModules; use pingora::protocols::http::error_resp; @@ -425,6 +427,8 @@ impl ProxyHttp for Server { State::new() } fn init_downstream_modules(&self, modules: &mut HttpModules) { + // Add disabled downstream compression module by default + modules.add_module(ResponseCompressionBuilder::enable(0)); let Some(value) = &self.modules else { return; }; diff --git a/web/src/i18n/en.ts b/web/src/i18n/en.ts index eea123fb..8d67aaed 100644 --- a/web/src/i18n/en.ts +++ b/web/src/i18n/en.ts @@ -316,7 +316,8 @@ export default { compressionZstdLevelPlaceholder: "Input the zstd level(1-22)", compressionDecompression: "Support Decompression", acceptEncodingList: "Accept Encoding", - acceptEncodingListPlaceholder: "Input the request accept encoding", + acceptEncodingListPlaceholder: + "Input the request accept encoding(e.g. zstd, br)", acceptEncodingOnlyOne: "Supports One Encoding", keyAuthQuery: "Query", keyAuthQueryPlaceholder: "Input the name of query", diff --git a/web/src/i18n/zh.ts b/web/src/i18n/zh.ts index cf2a5ad4..cb62c6a4 100644 --- a/web/src/i18n/zh.ts +++ b/web/src/i18n/zh.ts @@ -297,7 +297,7 @@ export default { compressionZstdLevelPlaceholder: "输入zstd压缩级别(1-22)", compressionDecompression: "是否支持解压缩", acceptEncodingList: "支持的压缩编码", - acceptEncodingListPlaceholder: "输入请求支持的压缩编码", + acceptEncodingListPlaceholder: "输入请求支持的压缩编码(如 zstd, br)", acceptEncodingOnlyOne: "仅支持单个压缩编码", keyAuthQuery: "Query", keyAuthQueryPlaceholder: "输入query的名称",