Skip to content

Commit

Permalink
refactor: add static serve example
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Nov 16, 2024
1 parent 20acd3e commit 2384ffe
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 4 deletions.
28 changes: 28 additions & 0 deletions examples/static-serve.toml
Original file line number Diff line number Diff line change
@@ -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"]
15 changes: 15 additions & 0 deletions src/config/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion src/plugin/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion src/proxy/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
};
Expand Down
3 changes: 2 additions & 1 deletion web/src/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion web/src/i18n/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ export default {
compressionZstdLevelPlaceholder: "输入zstd压缩级别(1-22)",
compressionDecompression: "是否支持解压缩",
acceptEncodingList: "支持的压缩编码",
acceptEncodingListPlaceholder: "输入请求支持的压缩编码",
acceptEncodingListPlaceholder: "输入请求支持的压缩编码(如 zstd, br)",
acceptEncodingOnlyOne: "仅支持单个压缩编码",
keyAuthQuery: "Query",
keyAuthQueryPlaceholder: "输入query的名称",
Expand Down

0 comments on commit 2384ffe

Please sign in to comment.