|
| 1 | +use axum::{ |
| 2 | + extract::State, |
| 3 | + response::{Html, IntoResponse}, |
| 4 | +}; |
| 5 | +use reqwest::Client; |
| 6 | +use std::env; |
| 7 | +use crate::{db, models::SiteContent}; |
| 8 | + |
| 9 | +// Default frontend URL (internal Docker network) |
| 10 | +const DEFAULT_FRONTEND_URL: &str = "http://frontend"; |
| 11 | + |
| 12 | +pub async fn serve_index(State(pool): State<db::DbPool>) -> impl IntoResponse { |
| 13 | + let frontend_url = env::var("FRONTEND_URL").unwrap_or_else(|_| DEFAULT_FRONTEND_URL.to_string()); |
| 14 | + let index_url = format!("{}/index.html", frontend_url); |
| 15 | + |
| 16 | + // Fetch index.html from frontend container |
| 17 | + let client = Client::new(); |
| 18 | + let html_content = match client.get(&index_url).send().await { |
| 19 | + Ok(resp) => match resp.text().await { |
| 20 | + Ok(text) => text, |
| 21 | + Err(e) => { |
| 22 | + tracing::error!("Failed to read index.html body: {}", e); |
| 23 | + return Html("<h1>Internal Server Error</h1><p>Failed to load application.</p>".to_string()).into_response(); |
| 24 | + } |
| 25 | + }, |
| 26 | + Err(e) => { |
| 27 | + tracing::error!("Failed to fetch index.html from {}: {}", index_url, e); |
| 28 | + return Html("<h1>Internal Server Error</h1><p>Failed to connect to frontend service.</p>".to_string()).into_response(); |
| 29 | + } |
| 30 | + }; |
| 31 | + |
| 32 | + // Fetch site meta from DB |
| 33 | + let site_meta = match db::fetch_site_content_by_section(&pool, "site_meta").await { |
| 34 | + Ok(Some(record)) => { |
| 35 | + match serde_json::from_str::<serde_json::Value>(&record.content_json) { |
| 36 | + Ok(json) => json, |
| 37 | + Err(_) => serde_json::json!({}), |
| 38 | + } |
| 39 | + }, |
| 40 | + _ => serde_json::json!({}), |
| 41 | + }; |
| 42 | + |
| 43 | + let title = site_meta.get("title") |
| 44 | + .and_then(|v| v.as_str()) |
| 45 | + .unwrap_or("Linux Tutorial - Lerne Linux Schritt für Schritt"); |
| 46 | + |
| 47 | + let description = site_meta.get("description") |
| 48 | + .and_then(|v| v.as_str()) |
| 49 | + .unwrap_or("Lerne Linux von Grund auf - Interaktiv, modern und praxisnah."); |
| 50 | + |
| 51 | + // Inject meta tags using simple string replacement |
| 52 | + // We target the specific default tags to replace them |
| 53 | + let mut injected_html = html_content; |
| 54 | + |
| 55 | + // Replace Title |
| 56 | + injected_html = injected_html.replace( |
| 57 | + "<title>Linux Tutorial - Lerne Linux Schritt für Schritt</title>", |
| 58 | + &format!("<title>{}</title>", title) |
| 59 | + ); |
| 60 | + |
| 61 | + // Replace Meta Description |
| 62 | + // Note: This regex-like replacement is brittle if the HTML formatting changes. |
| 63 | + // For now, we assume the exact string from index.html or use a more robust regex if needed. |
| 64 | + // Since we don't have regex crate here yet, we'll try to replace the known default description. |
| 65 | + // If it's dynamic, we might need a more robust approach, but for now let's try replacing the known default. |
| 66 | + let default_desc = "Lerne Linux von Grund auf - Interaktiv, modern und praxisnah. Umfassende Tutorials für Einsteiger und Fortgeschrittene."; |
| 67 | + injected_html = injected_html.replace( |
| 68 | + &format!("content=\"{}\"", default_desc), |
| 69 | + &format!("content=\"{}\"", description) |
| 70 | + ); |
| 71 | + |
| 72 | + // Also replace OG tags if possible. |
| 73 | + // A better approach for robust replacement without full HTML parsing: |
| 74 | + // We can replace the whole <head> block or specific known lines if we are sure about the structure. |
| 75 | + // Given the index.html structure, we can try to replace specific lines. |
| 76 | + |
| 77 | + // Replace OG Title |
| 78 | + injected_html = injected_html.replace( |
| 79 | + "content=\"Linux Tutorial - Lerne Linux Schritt für Schritt\"", |
| 80 | + &format!("content=\"{}\"", title) |
| 81 | + ); |
| 82 | + |
| 83 | + // Replace OG Description (reusing the description replacement above might handle this if content matches) |
| 84 | + // The default OG description in index.html is shorter: "Lerne Linux von Grund auf - Interaktiv, modern und praxisnah." |
| 85 | + let default_og_desc = "Lerne Linux von Grund auf - Interaktiv, modern und praxisnah."; |
| 86 | + injected_html = injected_html.replace( |
| 87 | + &format!("content=\"{}\"", default_og_desc), |
| 88 | + &format!("content=\"{}\"", description) |
| 89 | + ); |
| 90 | + |
| 91 | + Html(injected_html).into_response() |
| 92 | +} |
0 commit comments