docs
This commit is contained in:
parent
ef14646507
commit
193163bca6
2 changed files with 39 additions and 0 deletions
|
@ -2,12 +2,18 @@ use serde::Deserialize;
|
||||||
|
|
||||||
use crate::proxy::Mirror;
|
use crate::proxy::Mirror;
|
||||||
|
|
||||||
|
/// Mirrord configuration
|
||||||
#[derive(Debug, Deserialize, Clone)]
|
#[derive(Debug, Deserialize, Clone)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
|
/// List of mirror hosts to fetch data from.
|
||||||
pub mirrors: Vec<String>,
|
pub mirrors: Vec<String>,
|
||||||
|
/// Directory path where cached data will be stored.
|
||||||
pub cache_dir: String,
|
pub cache_dir: String,
|
||||||
|
/// Regex for which paths should never use cache
|
||||||
pub no_cache: String,
|
pub no_cache: String,
|
||||||
|
/// Time-to-live (TTL) duration for cached data in minutes
|
||||||
pub ttl: usize,
|
pub ttl: usize,
|
||||||
|
/// Regex for allowing only specific path requests
|
||||||
pub only_allow: Option<String>,
|
pub only_allow: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
33
src/proxy.rs
33
src/proxy.rs
|
@ -7,6 +7,10 @@ use std::{
|
||||||
|
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
|
|
||||||
|
// todo : download from many mirrors and compare results if one acts badly
|
||||||
|
// todo : better logging
|
||||||
|
// todo : better cache management
|
||||||
|
|
||||||
pub struct Mirror {
|
pub struct Mirror {
|
||||||
mirrors: Vec<Arc<String>>,
|
mirrors: Vec<Arc<String>>,
|
||||||
data_dir: String,
|
data_dir: String,
|
||||||
|
@ -59,6 +63,15 @@ impl Mirror {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Checks if the cached data at the specified path is invalid based on the TTL (time-to-live) setting.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `p` - A reference to the path of the cached data file.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// * `true` if the cached data is invalid (older than TTL), `false` otherwise.
|
||||||
pub fn is_cache_invalid(&self, p: &Path) -> bool {
|
pub fn is_cache_invalid(&self, p: &Path) -> bool {
|
||||||
let try_is_cache_invalid = || {
|
let try_is_cache_invalid = || {
|
||||||
let modified = p.metadata().ok()?.modified().ok()?;
|
let modified = p.metadata().ok()?.modified().ok()?;
|
||||||
|
@ -128,6 +141,16 @@ impl Mirror {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Asynchronously fetches cached data from the specified path and returns an HTTP response if the data exists.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `p` - A reference to the path of the cached data file.
|
||||||
|
/// * `req` - A reference to the HTTP request associated with the fetch operation.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// An optional HTTP response containing the fetched data if it exists, or `None` if the data does not exist.
|
||||||
pub async fn fetch_cache(&self, p: &PathBuf, req: &HttpRequest) -> Option<HttpResponse> {
|
pub async fn fetch_cache(&self, p: &PathBuf, req: &HttpRequest) -> Option<HttpResponse> {
|
||||||
if p.exists() {
|
if p.exists() {
|
||||||
if p.is_dir() {
|
if p.is_dir() {
|
||||||
|
@ -148,6 +171,16 @@ impl Mirror {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Asynchronously fetches data from network mirrors and returns an HTTP response if successful.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `path` - The path of the resource to fetch from network mirrors.
|
||||||
|
/// * `local` - A reference to the local path where the fetched data will be stored.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// An optional HTTP response containing the fetched data if successful, or `None` if fetching fails.
|
||||||
pub async fn fetch_network(&self, path: &str, local: &PathBuf) -> Option<HttpResponse> {
|
pub async fn fetch_network(&self, path: &str, local: &PathBuf) -> Option<HttpResponse> {
|
||||||
let mut mirrors = self.mirrors.clone();
|
let mut mirrors = self.mirrors.clone();
|
||||||
mirrors.shuffle(&mut rand::thread_rng());
|
mirrors.shuffle(&mut rand::thread_rng());
|
||||||
|
|
Loading…
Add table
Reference in a new issue