refactor
This commit is contained in:
parent
ff9a0b8691
commit
b0ce294ec8
1 changed files with 31 additions and 12 deletions
23
src/proxy.rs
23
src/proxy.rs
|
@ -68,12 +68,29 @@ impl Mirror {
|
||||||
let p = std::path::Path::new(&path[1..]);
|
let p = std::path::Path::new(&path[1..]);
|
||||||
let p = std::path::Path::new(&self.data_dir).join(p);
|
let p = std::path::Path::new(&self.data_dir).join(p);
|
||||||
|
|
||||||
|
// check if cache should be used
|
||||||
if !self.no_cache.is_match(path) {
|
if !self.no_cache.is_match(path) {
|
||||||
Self::create_cache_dir(p.parent().unwrap());
|
Self::create_cache_dir(p.parent().unwrap());
|
||||||
|
|
||||||
|
// use cache if present
|
||||||
|
if let Some(resp) = self.fetch_cache(&p, req).await {
|
||||||
|
log::info!("Returning {path} from cache");
|
||||||
|
return Some(resp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// fetch from network, if no response (offline) then use cache
|
||||||
|
if let Some(resp) = self.fetch_network(path, &p).await {
|
||||||
|
Some(resp)
|
||||||
|
} else {
|
||||||
|
log::info!("Returning {path} from cache");
|
||||||
|
self.fetch_cache(&p, req).await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn fetch_cache(&self, p: &PathBuf, req: &HttpRequest) -> Option<HttpResponse> {
|
||||||
if p.exists() {
|
if p.exists() {
|
||||||
// todo : refresh caches
|
// todo : refresh caches
|
||||||
log::info!("Returning {path} from cache");
|
|
||||||
if p.is_dir() {
|
if p.is_dir() {
|
||||||
return Some(
|
return Some(
|
||||||
actix_files::NamedFile::open_async(p.join("index"))
|
actix_files::NamedFile::open_async(p.join("index"))
|
||||||
|
@ -89,15 +106,17 @@ impl Mirror {
|
||||||
.into_response(req),
|
.into_response(req),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn fetch_network(&self, path: &str, local: &PathBuf) -> Option<HttpResponse> {
|
||||||
let mut mirrors = self.mirrors.clone();
|
let mut mirrors = self.mirrors.clone();
|
||||||
mirrors.shuffle(&mut rand::thread_rng());
|
mirrors.shuffle(&mut rand::thread_rng());
|
||||||
|
|
||||||
log::info!("Fetching {path} from mirrors");
|
log::info!("Fetching {path} from mirrors");
|
||||||
for mirror in mirrors {
|
for mirror in mirrors {
|
||||||
let url = format!("{mirror}{path}");
|
let url = format!("{mirror}{path}");
|
||||||
let response = self.get_url(&url, &p).await;
|
let response = self.get_url(&url, local).await;
|
||||||
if let Some(res) = response {
|
if let Some(res) = response {
|
||||||
if res.status().is_success() {
|
if res.status().is_success() {
|
||||||
return Some(res);
|
return Some(res);
|
||||||
|
|
Loading…
Add table
Reference in a new issue