From 50f9852cc9a94b45499f5a02764a2fe6b0d2636f Mon Sep 17 00:00:00 2001 From: JMARyA Date: Wed, 10 Apr 2024 10:24:56 +0200 Subject: [PATCH] no cache option --- .gitignore | 1 + Cargo.lock | 2 ++ Cargo.toml | 2 ++ sample.conf | 9 +++++++-- src/config.rs | 3 ++- src/proxy.rs | 28 ++++++++++++++++++---------- 6 files changed, 32 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index ea8c4bf..f6c313c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +/data \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 603886e..85069f8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1001,6 +1001,8 @@ dependencies = [ "actix-files", "actix-web", "env_logger", + "log", + "regex", "reqwest", "serde", "toml", diff --git a/Cargo.toml b/Cargo.toml index d00fa72..2f59be9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,8 @@ edition = "2021" actix-files = "0.6.5" actix-web = "4.5.1" env_logger = "0.11.3" +log = "0.4.21" +regex = "1.10.4" reqwest = "0.12.3" serde = { version = "1.0.197", features = ["derive"] } toml = "0.8.12" diff --git a/sample.conf b/sample.conf index 687ac0f..96d11dd 100644 --- a/sample.conf +++ b/sample.conf @@ -1,4 +1,5 @@ - + +# Mirrors to fetch from mirrors = [ "https://mirror.leaseweb.net", "https://geo.mirror.pkgbuild.com", @@ -6,5 +7,9 @@ mirrors = [ "https://archlinux.thaller.ws" ] +# Local cache directory cache_dir = "./data" -#ttl = "3h" \ No newline at end of file +#ttl = "3h" + +# Regex for paths which will never be served from cache +no_cache = '.*(?:db|db\.sig)$' \ No newline at end of file diff --git a/src/config.rs b/src/config.rs index 70468e2..e5b7119 100644 --- a/src/config.rs +++ b/src/config.rs @@ -6,10 +6,11 @@ use crate::proxy::ProxyMirror; pub struct Config { pub mirrors: Vec, 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) } } diff --git a/src/proxy.rs b/src/proxy.rs index c5233cb..b4dc64e 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -5,32 +5,40 @@ use actix_web::{HttpRequest, HttpResponse}; pub struct ProxyMirror { mirrors: Vec, data_dir: String, + no_cache: regex::Regex, } impl ProxyMirror { - pub fn new(mirrors: Vec, data_dir: &str) -> Self { + pub fn new(mirrors: Vec, 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 { 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 {