add custom index

This commit is contained in:
JMARyA 2024-09-17 15:34:15 +02:00
parent e4005b38ce
commit 0f35d34bcb
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
7 changed files with 291 additions and 268 deletions

507
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,15 +1,15 @@
[package]
name = "mirrord"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
[dependencies]
actix-files = "0.6.5"
actix-web = "4.5.1"
env_logger = "0.11.3"
log = "0.4.21"
rand = "0.8.5"
regex = "1.10.4"
reqwest = "0.12.3"
serde = { version = "1.0.197", features = ["derive"] }
toml = "0.8.12"
actix-files = "0.6"
actix-web = "4.5"
env_logger = "0.11"
log = "0.4"
rand = "0.8"
regex = "1.10"
reqwest = "0.12"
serde = { version = "1.0", features = ["derive"] }
toml = "0.8"

View file

@ -7,3 +7,4 @@ services:
volumes:
- ./data:/data
- ./sample.conf:/mirrord.conf
# - ./static/:/static # Custom Index

View file

@ -17,4 +17,10 @@ ttl = "180"
no_cache = '.*(?:db|db\.sig)$'
# Redirect only paths matching this regex to the mirrors, return 404 otherwise
only_allow = '^\/archlinux'
only_allow = '^\/archlinux'
# Show an index page on root path /
# Options:
# - `no` // disable index page
# - `<path>` // custom index page html file
index = "no"

View file

@ -15,6 +15,8 @@ pub struct Config {
pub ttl: Option<usize>,
/// Regex for allowing only specific path requests
pub only_allow: Option<String>,
/// Index Page
pub index: Option<String>,
}
impl Config {

View file

@ -7,6 +7,12 @@ async fn index(req: HttpRequest) -> impl Responder {
let path = req.path();
let p: &actix_web::web::Data<Mirror> = req.app_data().unwrap();
if path == "/" {
if let Some(ret) = p.index_page() {
return ret;
}
}
let data = p.get(path, &req).await;
data.unwrap_or_else(|| HttpResponse::NotFound().finish())
}

View file

@ -203,6 +203,21 @@ impl Mirror {
None
}
pub fn index_page(&self) -> Option<HttpResponse> {
if let Some(index) = &self.config.index {
return match index.as_str() {
"no" => None,
file => Some(
HttpResponse::Ok()
.content_type("text/html")
.body(std::fs::read_to_string(file).unwrap()),
),
};
}
return 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,