mirror of
https://codeberg.org/icewind/log-archiver.git
synced 2026-06-03 09:34:09 +02:00
also download original logs files
This commit is contained in:
parent
29ea9f2721
commit
5868f998bd
3 changed files with 310 additions and 202 deletions
485
Cargo.lock
generated
485
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -13,3 +13,4 @@ reqwest = { version = "0.11", default-features = false , features = ["rustls-tls
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
chrono = { version = "0.4", features = ["serde"] }
|
chrono = { version = "0.4", features = ["serde"] }
|
||||||
|
zip = "0.5"
|
||||||
28
src/main.rs
28
src/main.rs
|
|
@ -4,17 +4,20 @@ use reqwest::{Client, Response};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use sqlx::postgres::PgPool;
|
use sqlx::postgres::PgPool;
|
||||||
|
use std::io::Cursor;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tokio::time::sleep;
|
use tokio::time::sleep;
|
||||||
|
use zip::ZipArchive;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), MainError> {
|
async fn main() -> Result<(), MainError> {
|
||||||
let database_url = dotenv::var("DATABASE_URL")?;
|
let database_url = dotenv::var("DATABASE_URL")?;
|
||||||
let api_base =
|
let api_host = dotenv::var("API_HOST").unwrap_or_else(|_| "https://logs.tf".to_string());
|
||||||
dotenv::var("API_BASE").unwrap_or_else(|_| "http://logs.tf/api/v1/log".to_string());
|
let log_target = PathBuf::from(dotenv::var("LOG_TARGET")?);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
if let Err(e) = archive(&database_url, &api_base).await {
|
if let Err(e) = archive(&database_url, &api_host, &log_target).await {
|
||||||
eprintln!("{:?}", e);
|
eprintln!("{:?}", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -22,9 +25,9 @@ async fn main() -> Result<(), MainError> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_last_demo(client: &Client, api_base: &str) -> Result<i32, MainError> {
|
async fn get_last_demo(client: &Client, api_host: &str) -> Result<i32, MainError> {
|
||||||
let response: Response = client
|
let response: Response = client
|
||||||
.get(&format!("{}?limit=100", api_base))
|
.get(&format!("{}/api/v1/log?limit=100", api_host))
|
||||||
.send()
|
.send()
|
||||||
.await?;
|
.await?;
|
||||||
let listing: LogListing = serde_json::from_str(&response.text().await?)?;
|
let listing: LogListing = serde_json::from_str(&response.text().await?)?;
|
||||||
|
|
@ -43,7 +46,7 @@ async fn get_last_demo(client: &Client, api_base: &str) -> Result<i32, MainError
|
||||||
Ok(last_log.ok_or("Failed to find last log")?.id)
|
Ok(last_log.ok_or("Failed to find last log")?.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn archive(database_url: &str, api_base: &str) -> Result<(), MainError> {
|
async fn archive(database_url: &str, api_host: &str, log_target: &Path) -> Result<(), MainError> {
|
||||||
let pool = PgPool::connect(database_url).await?;
|
let pool = PgPool::connect(database_url).await?;
|
||||||
|
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
|
|
@ -52,7 +55,7 @@ async fn archive(database_url: &str, api_base: &str) -> Result<(), MainError> {
|
||||||
.fetch_one(&pool)
|
.fetch_one(&pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let last_demo = get_last_demo(&client, api_base).await?;
|
let last_demo = get_last_demo(&client, api_host).await?;
|
||||||
println!("Archiving up to log {}", last_demo);
|
println!("Archiving up to log {}", last_demo);
|
||||||
|
|
||||||
let mut last_archived = row.last_archived.unwrap_or_default();
|
let mut last_archived = row.last_archived.unwrap_or_default();
|
||||||
|
|
@ -65,7 +68,7 @@ async fn archive(database_url: &str, api_base: &str) -> Result<(), MainError> {
|
||||||
sleep(Duration::from_millis(200)).await;
|
sleep(Duration::from_millis(200)).await;
|
||||||
|
|
||||||
let response: Response = client
|
let response: Response = client
|
||||||
.get(&format!("{}/{}", api_base, last_archived))
|
.get(&format!("{}/api/v1/log/{}", api_host, last_archived))
|
||||||
.send()
|
.send()
|
||||||
.await?;
|
.await?;
|
||||||
let body: Value = serde_json::from_str(&response.text().await?)?;
|
let body: Value = serde_json::from_str(&response.text().await?)?;
|
||||||
|
|
@ -77,6 +80,15 @@ async fn archive(database_url: &str, api_base: &str) -> Result<(), MainError> {
|
||||||
)
|
)
|
||||||
.execute(&pool)
|
.execute(&pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
let log_zip = client
|
||||||
|
.get(&format!("{}/logs/log_{}.log.zip", api_host, last_archived))
|
||||||
|
.send()
|
||||||
|
.await?
|
||||||
|
.bytes()
|
||||||
|
.await?;
|
||||||
|
let mut archive = ZipArchive::new(Cursor::new(&log_zip))?;
|
||||||
|
archive.extract(log_target)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue