From fb9c58f4aec3f943d35658d5a1b9b0df04353cd7 Mon Sep 17 00:00:00 2001 From: guus <_@guusw.nl> Date: Sat, 25 Oct 2025 21:34:13 +0800 Subject: [PATCH] Add update hook --- src/main.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++ src/post_manager.rs | 2 +- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index a8a7122..e8414fa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,7 @@ use axum::{ Router, }; use std::sync::Arc; +use tokio::process::Command; use tokio::sync::RwLock; use tower_http::services::ServeDir; @@ -34,6 +35,7 @@ async fn main() { let app = Router::new() .route("/", get(index_handler)) + .route("/update", get(update_handler)) .route("/:page", get(all_handler)) .nest("/p", p_router) .with_state(app_state); @@ -87,6 +89,58 @@ async fn post_handler( } } +async fn update_handler(State(state): State) -> impl IntoResponse { + // Run git pull --autostash + let output = Command::new("git") + .arg("pull") + .arg("--autostash") + .output() + .await; + + match output { + Ok(output) => { + if !output.status.success() { + let stderr = String::from_utf8_lossy(&output.stderr); + eprintln!("Git pull failed: {}", stderr); + return ( + StatusCode::INTERNAL_SERVER_ERROR, + format!("Git pull failed: {}", stderr), + ) + .into_response(); + } + + let stdout = String::from_utf8_lossy(&output.stdout); + println!("Git pull output: {}", stdout); + + // Refresh posts + let mut manager = state.post_manager.write().await; + match manager.refresh_posts() { + Ok(_) => { + println!("Successfully refreshed log pages"); + (StatusCode::OK, "Update successful: pulled changes and refreshed log pages") + .into_response() + } + Err(e) => { + eprintln!("Failed to refresh posts: {}", e); + ( + StatusCode::INTERNAL_SERVER_ERROR, + format!("Failed to refresh posts: {}", e), + ) + .into_response() + } + } + } + Err(e) => { + eprintln!("Failed to execute git pull: {}", e); + ( + StatusCode::INTERNAL_SERVER_ERROR, + format!("Failed to execute git pull: {}", e), + ) + .into_response() + } + } +} + async fn render_template(template_name: &str, state: &AppState) -> Result { let template_path = format!("templates/{}", template_name); diff --git a/src/post_manager.rs b/src/post_manager.rs index 7dd6b72..cd88250 100644 --- a/src/post_manager.rs +++ b/src/post_manager.rs @@ -132,7 +132,7 @@ impl PostManager { Ok(result) } - fn refresh_posts(&mut self) -> Result<(), String> { + pub fn refresh_posts(&mut self) -> Result<(), String> { self.posts.clear(); // Get timestamps from git history