no cache option

This commit is contained in:
JMARyA 2024-04-10 10:24:56 +02:00
parent 8dd8bc8033
commit 50f9852cc9
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
6 changed files with 32 additions and 13 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
/target
/data

2
Cargo.lock generated
View file

@ -1001,6 +1001,8 @@ dependencies = [
"actix-files",
"actix-web",
"env_logger",
"log",
"regex",
"reqwest",
"serde",
"toml",

View file

@ -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"

View file

@ -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"
#ttl = "3h"
# Regex for paths which will never be served from cache
no_cache = '.*(?:db|db\.sig)$'

View file

@ -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)
}
}

View file

@ -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 {