This commit is contained in:
JMARyA 2024-04-12 08:39:51 +02:00
parent aeb41e5d44
commit 9dda6fcf7a
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
3 changed files with 54 additions and 10 deletions

View file

@ -1,6 +1,6 @@
use serde::Deserialize;
use crate::proxy::ProxyMirror;
use crate::proxy::Mirror;
#[derive(Debug, Deserialize)]
pub struct Config {
@ -10,7 +10,7 @@ pub struct Config {
}
impl Config {
pub fn to_proxy(&self) -> ProxyMirror {
ProxyMirror::new(self.mirrors.clone(), &self.cache_dir, &self.no_cache)
pub fn to_proxy(&self) -> Mirror {
Mirror::new(self.mirrors.clone(), &self.cache_dir, &self.no_cache)
}
}

View file

@ -1,11 +1,11 @@
use actix_web::{web, App, HttpRequest, HttpServer, Responder};
mod config;
mod proxy;
use proxy::ProxyMirror;
use proxy::Mirror;
async fn index(req: HttpRequest) -> impl Responder {
let path = req.path();
let p: &actix_web::web::Data<ProxyMirror> = req.app_data().unwrap();
let p: &actix_web::web::Data<Mirror> = req.app_data().unwrap();
let data = p.get(path, &req).await;
data.unwrap()

View file

@ -5,21 +5,35 @@ use std::{
sync::Arc,
};
pub struct ProxyMirror {
pub struct Mirror {
mirrors: Vec<Arc<String>>,
data_dir: String,
no_cache: regex::Regex,
}
impl ProxyMirror {
impl Mirror {
pub fn new(mirrors: Vec<String>, data_dir: &str, no_cache: &str) -> Self {
Self {
mirrors: mirrors.into_iter().map(|x| Arc::new(x)).collect(),
mirrors: mirrors.into_iter().map(Arc::new).collect(),
data_dir: data_dir.to_string(),
no_cache: regex::Regex::new(no_cache).unwrap(),
}
}
/// Creates a cache directory at the specified path.
///
/// If the specified path points to a file, it renames the file by appending ".tmp" to its name,
/// creates the cache directory, and moves the file into the cache directory with the name "index".
///
/// If the specified path does not exist or points to a directory, it creates the directory.
///
/// # Arguments
///
/// * `dir` - A reference to the path where the cache directory should be created.
///
/// # Panics
///
/// This function panics if any of the file system operations fail, such as renaming files or creating directories.
pub fn create_cache_dir(dir: &Path) {
if dir.is_file() {
let tmp_file_path = dir.with_extension("tmp");
@ -35,6 +49,21 @@ impl ProxyMirror {
}
}
/// Asynchronously retrieves content from the specified path, either from cache or mirrors.
///
/// This function attempts to retrieve content from the specified `path`. If caching is enabled
/// and the content exists in the cache, it is returned directly from the cache. If not found in
/// the cache or caching is disabled, the function attempts to fetch the content from a list of mirrors.
///
/// # Arguments
///
/// * `path` - The path from which to retrieve content.
/// * `req` - The HTTP request associated with the retrieval operation.
///
/// # Returns
///
/// An `Option` containing an `HttpResponse` if content is successfully retrieved, or `None` if the
/// content could not be found or fetched from any source.
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);
@ -68,8 +97,8 @@ impl ProxyMirror {
log::info!("Fetching {path} from mirrors");
for mirror in mirrors {
let url = format!("{mirror}{path}");
let res = self.get_url(&url, &p).await;
if let Some(res) = res {
let response = self.get_url(&url, &p).await;
if let Some(res) = response {
if res.status().is_success() {
return Some(res);
}
@ -78,6 +107,21 @@ impl ProxyMirror {
None
}
/// Asynchronously fetches content from the specified URL and saves it to the provided file path.
///
/// This function sends an HTTP GET request to the URL specified by `path`, retrieves the response,
/// and saves the response body to the file specified by `save`. If the HTTP request is successful
/// (status code 2xx), the response body is saved to the file.
///
/// # Arguments
///
/// * `path` - The URL from which to fetch content.
/// * `save` - The file path where the fetched content should be saved.
///
/// # Returns
///
/// An `Option` containing an `HttpResponse` if the request was successful and the response was received,
/// or `None` if there was an error during the request or response retrieval.
pub async fn get_url(&self, path: &str, save: &PathBuf) -> Option<HttpResponse> {
log::info!("Fetching {path}");
let response = reqwest::get(path).await.unwrap();