Skip to content

Commit

Permalink
refactor: adjust index file for static directory
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Apr 2, 2024
1 parent d17136e commit 9e26cd6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
- [x] static file serve
- [x] set priority for location
- [x] mock response for upstream
- [ ] add remark for config
- [x] add remark for config
- [ ] support multi host for location?
8 changes: 4 additions & 4 deletions conf/pingap.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ idle_timeout = "120s"
# Anther upstream using all default config.
[upstreams.diving]
# static file
# addrs = ["file://~/Downloads"]
addrs = [
'mock://{"status":500,"headers":["Content-Type: application/json"],"data":"{\"message\":\"Mock Service Unavailable\"}"}',
]
addrs = ["file://~/Downloads"]
# addrs = [
# 'mock://{"status":500,"headers":["Content-Type: application/json"],"data":"{\"message\":\"Mock Service Unavailable\"}"}',
# ]

# Location config list, it will defined as [locations.name]
[locations.lo]
Expand Down
16 changes: 9 additions & 7 deletions src/serve/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::http_extra::{HttpChunkResponse, HttpHeader, HttpResponse};
#[derive(Default)]
pub struct Directory {
path: PathBuf,
index: String,
chunk_size: Option<usize>,
// max age of http response
max_age: Option<u32>,
Expand Down Expand Up @@ -60,8 +61,9 @@ impl Directory {
let mut chunk_size = None;
let mut max_age = None;
let mut cache_private = None;
let mut index_file = "index.html".to_string();
if let Ok(url_info) = Url::parse(path) {
let query = url_info.query().unwrap();
let query = url_info.query().unwrap_or_default();
if !query.is_empty() {
new_path = new_path.substring(0, new_path.len() - query.len() - 1);
}
Expand All @@ -78,24 +80,24 @@ impl Directory {
}
}
"private" => cache_private = Some(false),
"index" => index_file = value.to_string(),
_ => {}
}
}
};
Directory {
index: format!("/{index_file}"),
path: Path::new(&utils::resolve_path(new_path)).to_path_buf(),
chunk_size,
max_age,
cache_private,
}
}
pub async fn handle(&self, session: &mut Session, _ctx: &mut State) -> pingora::Result<bool> {
let mut filename = session
.req_header()
.uri
.path()
.to_string()
.max("/".to_string());
let mut filename = session.req_header().uri.path().to_string();
if filename.len() <= 1 {
filename = self.index.clone();
}
if let Ok(value) = decode(&filename) {
filename = value.into_owned().clone();
}
Expand Down

0 comments on commit 9e26cd6

Please sign in to comment.