diff --git a/pagetop-admin/src/summary.rs b/pagetop-admin/src/summary.rs index e3de92be..9b763b50 100644 --- a/pagetop-admin/src/summary.rs +++ b/pagetop-admin/src/summary.rs @@ -1,7 +1,7 @@ use super::l; use pagetop::prelude::*; -pub async fn summary() -> ResultPage { +pub async fn summary(request: server::HttpRequest) -> ResultPage { let top_menu = Menu::new() .with_item(MenuItem::label(l("module_name").as_str())) .with_item(MenuItem::link("Opción 2", "https://www.google.es")) @@ -40,8 +40,8 @@ pub async fn summary() -> ResultPage { )) .with_item(MenuItem::label("Opción 4")); - Page::new() - .with_context(ContextOp::SetTheme("Bootsier")) + Page::new(request) + .with_context(ContextOp::Theme("Bootsier")) .with_title("Admin") .with_this_in("top-menu", top_menu) .with_this_in( diff --git a/pagetop-node/src/lib.rs b/pagetop-node/src/lib.rs index aef94a93..f6a09d16 100644 --- a/pagetop-node/src/lib.rs +++ b/pagetop-node/src/lib.rs @@ -40,8 +40,8 @@ impl ModuleTrait for Node { } } -async fn node() -> ResultPage { - Page::new().with_title("Nodo").render() +async fn node(request: server::HttpRequest) -> ResultPage { + Page::new(request).with_title("Nodo").render() } fn before_render_page(page: &mut Page) { diff --git a/pagetop-user/src/lib.rs b/pagetop-user/src/lib.rs index 0d01c771..845928fd 100644 --- a/pagetop-user/src/lib.rs +++ b/pagetop-user/src/lib.rs @@ -35,8 +35,8 @@ impl ModuleTrait for User { } } -async fn login() -> ResultPage { - Page::new() +async fn login(request: server::HttpRequest) -> ResultPage { + Page::new(request) .with_title("Identificación del usuario") .with_this_in( "region-content", diff --git a/pagetop/src/app.rs b/pagetop/src/app.rs index 39f6f3dc..ba4bbd89 100644 --- a/pagetop/src/app.rs +++ b/pagetop/src/app.rs @@ -70,6 +70,6 @@ impl Application { } } -async fn service_not_found() -> ResultPage { - Err(FatalError::NotFound) +async fn service_not_found(request: server::HttpRequest) -> ResultPage { + Err(FatalError::NotFound(request)) } diff --git a/pagetop/src/base/module/homepage.rs b/pagetop/src/base/module/homepage.rs index 7ccebc69..7b395f78 100644 --- a/pagetop/src/base/module/homepage.rs +++ b/pagetop/src/base/module/homepage.rs @@ -24,8 +24,8 @@ impl ModuleTrait for DefaultHomePage { } } -async fn demo() -> ResultPage { - Page::new() +async fn demo(request: server::HttpRequest) -> ResultPage { + Page::new(request) .with_title(l("page_title").as_str()) .with_context(ContextOp::AddStyleSheet(StyleSheet::located( "/theme/module/homepage/styles.css", diff --git a/pagetop/src/core/component/context.rs b/pagetop/src/core/component/context.rs index 88a3368e..4fecb209 100644 --- a/pagetop/src/core/component/context.rs +++ b/pagetop/src/core/component/context.rs @@ -1,5 +1,6 @@ use crate::core::theme::{all::theme_by_single_name, ThemeStaticRef}; use crate::html::{html, Assets, IdentifierValue, JavaScript, Markup, ModeJS, StyleSheet}; +use crate::server::HttpRequest; use crate::{base, concat_string, config, util, LazyStatic}; static DEFAULT_THEME: LazyStatic = @@ -9,7 +10,8 @@ static DEFAULT_THEME: LazyStatic = }); pub enum ContextOp { - SetTheme(&'static str), + Theme(&'static str), + Request(Option), AddStyleSheet(StyleSheet), RemoveStyleSheet(&'static str), AddJavaScript(JavaScript), @@ -20,6 +22,7 @@ pub enum ContextOp { #[rustfmt::skip] pub struct RenderContext { theme : ThemeStaticRef, + request : Option, stylesheets: Assets, javascripts: Assets, with_jquery: bool, @@ -31,6 +34,7 @@ impl Default for RenderContext { fn default() -> Self { RenderContext { theme : *DEFAULT_THEME, + request : None, stylesheets: Assets::::new(), javascripts: Assets::::new(), with_jquery: false, @@ -40,15 +44,18 @@ impl Default for RenderContext { } impl RenderContext { - pub fn new() -> Self { + pub(crate) fn new() -> Self { RenderContext::default() } pub fn alter(&mut self, op: ContextOp) -> &mut Self { match op { - ContextOp::SetTheme(theme_name) => { + ContextOp::Theme(theme_name) => { self.theme = theme_by_single_name(theme_name).unwrap_or(*DEFAULT_THEME); } + ContextOp::Request(request) => { + self.request = request; + } ContextOp::AddStyleSheet(css) => { self.stylesheets.add(css); } @@ -78,10 +85,14 @@ impl RenderContext { /// Context GETTERS. - pub(crate) fn theme(&mut self) -> ThemeStaticRef { + pub(crate) fn theme(&self) -> ThemeStaticRef { self.theme } + pub fn request(&self) -> &Option { + &self.request + } + /// Context RENDER. pub fn render(&mut self) -> Markup { diff --git a/pagetop/src/response/fatal_error.rs b/pagetop/src/response/fatal_error.rs index 591b0472..0c17dd8e 100644 --- a/pagetop/src/response/fatal_error.rs +++ b/pagetop/src/response/fatal_error.rs @@ -1,30 +1,30 @@ use crate::response::{page::Page, ResponseError}; use crate::server::http::{header::ContentType, StatusCode}; -use crate::server::HttpResponse; +use crate::server::{HttpRequest, HttpResponse}; use std::fmt; #[derive(Debug)] pub enum FatalError { - NotModified, - BadRequest, - AccessDenied, - NotFound, - PreconditionFailed, - InternalError, - Timeout, + NotModified(HttpRequest), + BadRequest(HttpRequest), + AccessDenied(HttpRequest), + NotFound(HttpRequest), + PreconditionFailed(HttpRequest), + InternalError(HttpRequest), + Timeout(HttpRequest), } impl fmt::Display for FatalError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match *self { + match self { // Error 304. - FatalError::NotModified => write!(f, "Not Modified"), + FatalError::NotModified(_) => write!(f, "Not Modified"), // Error 400. - FatalError::BadRequest => write!(f, "Bad Client Data"), + FatalError::BadRequest(_) => write!(f, "Bad Client Data"), // Error 403. - FatalError::AccessDenied => { - let mut error_page = Page::new(); + FatalError::AccessDenied(request) => { + let mut error_page = Page::new(request.clone()); let error_content = error_page.context().theme().error_403_access_denied(); if let Ok(page) = error_page .with_title("Error FORBIDDEN") @@ -38,8 +38,8 @@ impl fmt::Display for FatalError { } } // Error 404. - FatalError::NotFound => { - let mut error_page = Page::new(); + FatalError::NotFound(request) => { + let mut error_page = Page::new(request.clone()); let error_content = error_page.context().theme().error_404_not_found(); if let Ok(page) = error_page .with_title("Error RESOURCE NOT FOUND") @@ -53,11 +53,11 @@ impl fmt::Display for FatalError { } } // Error 412. - FatalError::PreconditionFailed => write!(f, "Precondition Failed"), + FatalError::PreconditionFailed(_) => write!(f, "Precondition Failed"), // Error 500. - FatalError::InternalError => write!(f, "Internal Error"), + FatalError::InternalError(_) => write!(f, "Internal Error"), // Error 504. - FatalError::Timeout => write!(f, "Timeout"), + FatalError::Timeout(_) => write!(f, "Timeout"), } } } @@ -71,14 +71,14 @@ impl ResponseError for FatalError { #[rustfmt::skip] fn status_code(&self) -> StatusCode { - match *self { - FatalError::NotModified => StatusCode::NOT_MODIFIED, - FatalError::BadRequest => StatusCode::BAD_REQUEST, - FatalError::AccessDenied => StatusCode::FORBIDDEN, - FatalError::NotFound => StatusCode::NOT_FOUND, - FatalError::PreconditionFailed => StatusCode::PRECONDITION_FAILED, - FatalError::InternalError => StatusCode::INTERNAL_SERVER_ERROR, - FatalError::Timeout => StatusCode::GATEWAY_TIMEOUT, + match self { + FatalError::NotModified(_) => StatusCode::NOT_MODIFIED, + FatalError::BadRequest(_) => StatusCode::BAD_REQUEST, + FatalError::AccessDenied(_) => StatusCode::FORBIDDEN, + FatalError::NotFound(_) => StatusCode::NOT_FOUND, + FatalError::PreconditionFailed(_) => StatusCode::PRECONDITION_FAILED, + FatalError::InternalError(_) => StatusCode::INTERNAL_SERVER_ERROR, + FatalError::Timeout(_) => StatusCode::GATEWAY_TIMEOUT, } } } diff --git a/pagetop/src/response/page/definition.rs b/pagetop/src/response/page/definition.rs index 5b6360e2..c772cbfd 100644 --- a/pagetop/src/response/page/definition.rs +++ b/pagetop/src/response/page/definition.rs @@ -4,7 +4,7 @@ use crate::core::component::*; use crate::core::hook::{action_ref, run_actions}; use crate::html::{html, AttributeValue, Classes, ClassesOp, Favicon, Markup, DOCTYPE}; use crate::response::FatalError; -use crate::{config, fn_builder, trace, LazyStatic}; +use crate::{config, fn_builder, server, trace, LazyStatic}; use std::collections::HashMap; @@ -82,8 +82,10 @@ impl Default for Page { } impl Page { - pub fn new() -> Self { - Page::default() + pub fn new(request: server::HttpRequest) -> Self { + let mut page = Page::default(); + page.context.alter(ContextOp::Request(Some(request))); + page } // Page BUILDER.