- Add a comprehensive .gitignore to manage project files. - Create the basic project structure including Cargo.toml, LICENSE, and README.md. - Add basic project documentation.
74 lines
2.6 KiB
Rust
74 lines
2.6 KiB
Rust
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<Tera>, session: Session) -> Result<impl Responder> {
|
|
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::<String>("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::<crate::models::user::User>(&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<Tera>, session: Session) -> Result<impl Responder> {
|
|
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::<String>("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::<crate::models::user::User>(&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)
|
|
}
|
|
}
|