use actix_web::{web, Result, Responder}; use tera::Tera; use crate::utils::render_template; use actix_session::Session; use std::env; /// Controller for handling home-related routes pub struct HomeController; impl HomeController { /// Renders the home page pub async fn index(tmpl: web::Data, session: Session) -> Result { let mut ctx = tera::Context::new(); ctx.insert("active_page", "home"); let is_gitea_flow_active = env::var("GITEA_CLIENT_ID") .ok() .filter(|s| !s.is_empty()) .is_some(); ctx.insert("gitea_enabled", &is_gitea_flow_active); // Add user to context if available if let Ok(Some(user_json)) = session.get::("user") { // Keep the raw JSON for backward compatibility ctx.insert("user_json", &user_json); // Parse the JSON into a User object match serde_json::from_str::(&user_json) { Ok(user) => { log::info!("Successfully parsed user: {:?}", user); ctx.insert("user", &user); }, Err(e) => { log::error!("Failed to parse user JSON: {}", e); log::error!("User JSON: {}", user_json); } } } render_template(&tmpl, "home/index.html", &ctx) } /// Renders the about page pub async fn about(tmpl: web::Data, session: Session) -> Result { let mut ctx = tera::Context::new(); let is_gitea_flow_active = env::var("GITEA_CLIENT_ID") .ok() .filter(|s| !s.is_empty()) .is_some(); ctx.insert("gitea_enabled", &is_gitea_flow_active); ctx.insert("active_page", "about"); // Add user to context if available if let Ok(Some(user_json)) = session.get::("user") { // Keep the raw JSON for backward compatibility ctx.insert("user_json", &user_json); // Parse the JSON into a User object match serde_json::from_str::(&user_json) { Ok(user) => { log::info!("Successfully parsed user: {:?}", user); ctx.insert("user", &user); }, Err(e) => { log::error!("Failed to parse user JSON: {}", e); log::error!("User JSON: {}", user_json); } } } render_template(&tmpl, "home/about.html", &ctx) } }