no cache option
This commit is contained in:
parent
8dd8bc8033
commit
50f9852cc9
6 changed files with 32 additions and 13 deletions
|
@ -6,10 +6,11 @@ use crate::proxy::ProxyMirror;
|
|||
pub struct Config {
|
||||
pub mirrors: Vec<String>,
|
||||
pub cache_dir: String,
|
||||
pub no_cache: String,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn to_proxy(&self) -> ProxyMirror {
|
||||
ProxyMirror::new(self.mirrors.clone(), &self.cache_dir)
|
||||
ProxyMirror::new(self.mirrors.clone(), &self.cache_dir, &self.no_cache)
|
||||
}
|
||||
}
|
||||
|
|
28
src/proxy.rs
28
src/proxy.rs
|
@ -5,32 +5,40 @@ use actix_web::{HttpRequest, HttpResponse};
|
|||
pub struct ProxyMirror {
|
||||
mirrors: Vec<String>,
|
||||
data_dir: String,
|
||||
no_cache: regex::Regex,
|
||||
}
|
||||
|
||||
impl ProxyMirror {
|
||||
pub fn new(mirrors: Vec<String>, data_dir: &str) -> Self {
|
||||
pub fn new(mirrors: Vec<String>, data_dir: &str, no_cache: &str) -> Self {
|
||||
Self {
|
||||
mirrors,
|
||||
data_dir: data_dir.to_string(),
|
||||
no_cache: regex::Regex::new(no_cache).unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get(&self, path: &str, req: &HttpRequest) -> Option<HttpResponse> {
|
||||
let p = std::path::Path::new(&path[1..]);
|
||||
let p = std::path::Path::new(&self.data_dir).join(p);
|
||||
std::fs::create_dir_all(p.parent().unwrap()).unwrap();
|
||||
|
||||
if p.exists() {
|
||||
// todo : refresh caches
|
||||
return Some(
|
||||
actix_files::NamedFile::open_async(&p)
|
||||
.await
|
||||
.ok()?
|
||||
.into_response(req),
|
||||
);
|
||||
// todo : fix file dir problem
|
||||
if !self.no_cache.is_match(path) {
|
||||
std::fs::create_dir_all(p.parent().unwrap()).unwrap();
|
||||
|
||||
if p.exists() {
|
||||
// todo : refresh caches
|
||||
log::info!("Returning {path} from cache");
|
||||
return Some(
|
||||
actix_files::NamedFile::open_async(&p)
|
||||
.await
|
||||
.ok()?
|
||||
.into_response(req),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
for mirror in &self.mirrors {
|
||||
log::info!("Fetching {path} from mirrors");
|
||||
let url = format!("{mirror}{path}");
|
||||
let res = self.get_url(&url, &p).await;
|
||||
if let Some(res) = res {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue