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
|
/target
|
||||||
|
/data
|
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -1001,6 +1001,8 @@ dependencies = [
|
||||||
"actix-files",
|
"actix-files",
|
||||||
"actix-web",
|
"actix-web",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
|
"log",
|
||||||
|
"regex",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"serde",
|
"serde",
|
||||||
"toml",
|
"toml",
|
||||||
|
|
|
@ -7,6 +7,8 @@ edition = "2021"
|
||||||
actix-files = "0.6.5"
|
actix-files = "0.6.5"
|
||||||
actix-web = "4.5.1"
|
actix-web = "4.5.1"
|
||||||
env_logger = "0.11.3"
|
env_logger = "0.11.3"
|
||||||
|
log = "0.4.21"
|
||||||
|
regex = "1.10.4"
|
||||||
reqwest = "0.12.3"
|
reqwest = "0.12.3"
|
||||||
serde = { version = "1.0.197", features = ["derive"] }
|
serde = { version = "1.0.197", features = ["derive"] }
|
||||||
toml = "0.8.12"
|
toml = "0.8.12"
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
|
|
||||||
|
# Mirrors to fetch from
|
||||||
mirrors = [
|
mirrors = [
|
||||||
"https://mirror.leaseweb.net",
|
"https://mirror.leaseweb.net",
|
||||||
"https://geo.mirror.pkgbuild.com",
|
"https://geo.mirror.pkgbuild.com",
|
||||||
|
@ -6,5 +7,9 @@ mirrors = [
|
||||||
"https://archlinux.thaller.ws"
|
"https://archlinux.thaller.ws"
|
||||||
]
|
]
|
||||||
|
|
||||||
|
# Local cache directory
|
||||||
cache_dir = "./data"
|
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 struct Config {
|
||||||
pub mirrors: Vec<String>,
|
pub mirrors: Vec<String>,
|
||||||
pub cache_dir: String,
|
pub cache_dir: String,
|
||||||
|
pub no_cache: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
pub fn to_proxy(&self) -> ProxyMirror {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
10
src/proxy.rs
10
src/proxy.rs
|
@ -5,23 +5,29 @@ use actix_web::{HttpRequest, HttpResponse};
|
||||||
pub struct ProxyMirror {
|
pub struct ProxyMirror {
|
||||||
mirrors: Vec<String>,
|
mirrors: Vec<String>,
|
||||||
data_dir: String,
|
data_dir: String,
|
||||||
|
no_cache: regex::Regex,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ProxyMirror {
|
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 {
|
Self {
|
||||||
mirrors,
|
mirrors,
|
||||||
data_dir: data_dir.to_string(),
|
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> {
|
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(&path[1..]);
|
||||||
let p = std::path::Path::new(&self.data_dir).join(p);
|
let p = std::path::Path::new(&self.data_dir).join(p);
|
||||||
|
|
||||||
|
// todo : fix file dir problem
|
||||||
|
if !self.no_cache.is_match(path) {
|
||||||
std::fs::create_dir_all(p.parent().unwrap()).unwrap();
|
std::fs::create_dir_all(p.parent().unwrap()).unwrap();
|
||||||
|
|
||||||
if p.exists() {
|
if p.exists() {
|
||||||
// todo : refresh caches
|
// todo : refresh caches
|
||||||
|
log::info!("Returning {path} from cache");
|
||||||
return Some(
|
return Some(
|
||||||
actix_files::NamedFile::open_async(&p)
|
actix_files::NamedFile::open_async(&p)
|
||||||
.await
|
.await
|
||||||
|
@ -29,8 +35,10 @@ impl ProxyMirror {
|
||||||
.into_response(req),
|
.into_response(req),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for mirror in &self.mirrors {
|
for mirror in &self.mirrors {
|
||||||
|
log::info!("Fetching {path} from mirrors");
|
||||||
let url = format!("{mirror}{path}");
|
let url = format!("{mirror}{path}");
|
||||||
let res = self.get_url(&url, &p).await;
|
let res = self.get_url(&url, &p).await;
|
||||||
if let Some(res) = res {
|
if let Some(res) = res {
|
||||||
|
|
Loading…
Add table
Reference in a new issue