no cache option
This commit is contained in:
parent
8dd8bc8033
commit
50f9852cc9
6 changed files with 32 additions and 13 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
|||
/target
|
||||
/data
|
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -1001,6 +1001,8 @@ dependencies = [
|
|||
"actix-files",
|
||||
"actix-web",
|
||||
"env_logger",
|
||||
"log",
|
||||
"regex",
|
||||
"reqwest",
|
||||
"serde",
|
||||
"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"
|
||||
|
|
|
@ -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)$'
|
|
@ -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
Reference in a new issue