From 8a25459985878c54f4b66c20ca045c51fc2aa0af Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Sun, 27 Nov 2022 21:25:57 +0530 Subject: [PATCH] fix: duplicate home page. Redirect to login page, if user is unauthenticated and redirect to dashboard homepage if user is authenticated --- src/pages/mod.rs | 51 +++----------- src/pages/routes.rs | 2 +- src/serve.rs | 12 ++-- templates/pages/index.html | 138 ------------------------------------- 4 files changed, 16 insertions(+), 187 deletions(-) delete mode 100644 templates/pages/index.html diff --git a/src/pages/mod.rs b/src/pages/mod.rs index eca0d27..b505cc6 100644 --- a/src/pages/mod.rs +++ b/src/pages/mod.rs @@ -77,7 +77,6 @@ lazy_static! { tera.autoescape_on(vec![".html", ".sql"]); auth::register_templates(&mut tera); dash::register_templates(&mut tera); - HOME.register(&mut tera).expect(HOME.name); tera }; } @@ -135,53 +134,20 @@ impl<'a> Footer<'a> { } } -pub const HOME: TemplateFile = TemplateFile::new("home", "pages/index.html"); - -pub struct Home { - ctx: RefCell, -} - -impl CtxError for Home { - fn with_error(&self, e: &ReadableError) -> String { - self.ctx.borrow_mut().insert(ERROR_KEY, e); - self.render() - } -} - -impl Home { - pub fn new(settings: &Settings) -> Self { - let ctx = RefCell::new(context(settings)); - Self { ctx } - } - - pub fn render(&self) -> String { - TEMPLATES.render(HOME.name, &self.ctx.borrow()).unwrap() - } - - pub fn page(s: &Settings) -> String { - let p = Self::new(s); - p.render() - } -} - -#[actix_web_codegen_const_routes::get(path = "PAGES.home")] -#[tracing::instrument(name = "Dashboard homepage", skip(id, ctx))] -pub async fn home(ctx: AppCtx, id: Identity) -> impl Responder { - if id.identity().is_none() { - let home = Home::page(&ctx.settings); - let html = header::ContentType::html(); - HttpResponse::Ok().content_type(html).body(home) +pub async fn home(ctx: AppCtx, id: &Identity) -> HttpResponse { + let location = if id.identity().is_some() { + PAGES.home } else { - HttpResponse::Found() - .append_header((header::LOCATION, PAGES.dash.home)) - .finish() - } + PAGES.dash.home + }; + HttpResponse::Found() + .append_header((header::LOCATION, location)) + .finish() } pub fn services(cfg: &mut web::ServiceConfig) { auth::services(cfg); dash::services(cfg); - cfg.service(home); } #[cfg(test)] @@ -203,7 +169,6 @@ mod tests { auth::login::LOGIN, auth::register::REGISTER, errors::ERROR_TEMPLATE, - HOME, ] .iter() { diff --git a/src/pages/routes.rs b/src/pages/routes.rs index fd3c6b1..6c513a1 100644 --- a/src/pages/routes.rs +++ b/src/pages/routes.rs @@ -35,7 +35,7 @@ impl Pages { const fn new() -> Pages { let auth = Auth::new(); let dash = Dash::new(); - let home = "/"; + let home = auth.login; Pages { auth, home, dash } } } diff --git a/src/serve.rs b/src/serve.rs index 9a19188..7f29dba 100644 --- a/src/serve.rs +++ b/src/serve.rs @@ -1,3 +1,4 @@ +use actix_identity::Identity; /* * Copyright (C) 2022 Aravinth Manivannan * @@ -17,6 +18,7 @@ use actix_web::{http::header::ContentType, web, HttpRequest, HttpResponse, Responder}; use crate::errors::*; +use crate::pages; use crate::AppCtx; pub mod routes { @@ -34,19 +36,19 @@ pub mod routes { } #[actix_web_codegen_const_routes::get(path = "crate::V1_API_ROUTES.serve.catch_all")] -#[tracing::instrument(name = "Serve webpages", skip(req, ctx))] -async fn index(req: HttpRequest, ctx: AppCtx) -> ServiceResult { +#[tracing::instrument(name = "Serve webpages", skip(req, ctx, id))] +async fn index(req: HttpRequest, ctx: AppCtx, id: Identity) -> ServiceResult { let c = req.connection_info(); let mut host = c.host(); if host.contains(':') { host = host.split(':').next().unwrap(); } + tracing::debug!("Current host {host}"); // serve meta page if host == ctx.settings.server.domain || host == "localhost" { - return Ok(HttpResponse::Ok() - .content_type(ContentType::html()) - .body("Welcome to Librepages!")); + tracing::debug!("Into home"); + return Ok(pages::home(ctx.clone(), &id).await); } // serve default hostname content diff --git a/templates/pages/index.html b/templates/pages/index.html deleted file mode 100644 index 827004d..0000000 --- a/templates/pages/index.html +++ /dev/null @@ -1,138 +0,0 @@ - - - - - - - LibrePages - - -
- -
-
-

LibrePages: FOSS static site hosting

-

Welcome to LibrePages. Homepage.

-
- {% include "footer" %} - - - -