✨ Añade la petición de entrada al contexto
Si la respuesta (Response) va a ser una página (Page) entonces hay que añadir la petición de entrada (HttpRequest) al contexto de renderizado (RenderContext) para que los componentes puedan consultarla durante la preparación de la página. Por ejemplo para consultar la URL de entrada y decidir si se renderiza o no un componente dado.
This commit is contained in:
parent
f081a00bd4
commit
c5de6f4b6d
8 changed files with 57 additions and 44 deletions
|
|
@ -1,7 +1,7 @@
|
|||
use super::l;
|
||||
use pagetop::prelude::*;
|
||||
|
||||
pub async fn summary() -> ResultPage<Markup, FatalError> {
|
||||
pub async fn summary(request: server::HttpRequest) -> ResultPage<Markup, FatalError> {
|
||||
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<Markup, FatalError> {
|
|||
))
|
||||
.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(
|
||||
|
|
|
|||
|
|
@ -40,8 +40,8 @@ impl ModuleTrait for Node {
|
|||
}
|
||||
}
|
||||
|
||||
async fn node() -> ResultPage<Markup, FatalError> {
|
||||
Page::new().with_title("Nodo").render()
|
||||
async fn node(request: server::HttpRequest) -> ResultPage<Markup, FatalError> {
|
||||
Page::new(request).with_title("Nodo").render()
|
||||
}
|
||||
|
||||
fn before_render_page(page: &mut Page) {
|
||||
|
|
|
|||
|
|
@ -35,8 +35,8 @@ impl ModuleTrait for User {
|
|||
}
|
||||
}
|
||||
|
||||
async fn login() -> ResultPage<Markup, FatalError> {
|
||||
Page::new()
|
||||
async fn login(request: server::HttpRequest) -> ResultPage<Markup, FatalError> {
|
||||
Page::new(request)
|
||||
.with_title("Identificación del usuario")
|
||||
.with_this_in(
|
||||
"region-content",
|
||||
|
|
|
|||
|
|
@ -70,6 +70,6 @@ impl Application {
|
|||
}
|
||||
}
|
||||
|
||||
async fn service_not_found() -> ResultPage<Markup, FatalError> {
|
||||
Err(FatalError::NotFound)
|
||||
async fn service_not_found(request: server::HttpRequest) -> ResultPage<Markup, FatalError> {
|
||||
Err(FatalError::NotFound(request))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,8 +24,8 @@ impl ModuleTrait for DefaultHomePage {
|
|||
}
|
||||
}
|
||||
|
||||
async fn demo() -> ResultPage<Markup, FatalError> {
|
||||
Page::new()
|
||||
async fn demo(request: server::HttpRequest) -> ResultPage<Markup, FatalError> {
|
||||
Page::new(request)
|
||||
.with_title(l("page_title").as_str())
|
||||
.with_context(ContextOp::AddStyleSheet(StyleSheet::located(
|
||||
"/theme/module/homepage/styles.css",
|
||||
|
|
|
|||
|
|
@ -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<ThemeStaticRef> =
|
||||
|
|
@ -9,7 +10,8 @@ static DEFAULT_THEME: LazyStatic<ThemeStaticRef> =
|
|||
});
|
||||
|
||||
pub enum ContextOp {
|
||||
SetTheme(&'static str),
|
||||
Theme(&'static str),
|
||||
Request(Option<HttpRequest>),
|
||||
AddStyleSheet(StyleSheet),
|
||||
RemoveStyleSheet(&'static str),
|
||||
AddJavaScript(JavaScript),
|
||||
|
|
@ -20,6 +22,7 @@ pub enum ContextOp {
|
|||
#[rustfmt::skip]
|
||||
pub struct RenderContext {
|
||||
theme : ThemeStaticRef,
|
||||
request : Option<HttpRequest>,
|
||||
stylesheets: Assets<StyleSheet>,
|
||||
javascripts: Assets<JavaScript>,
|
||||
with_jquery: bool,
|
||||
|
|
@ -31,6 +34,7 @@ impl Default for RenderContext {
|
|||
fn default() -> Self {
|
||||
RenderContext {
|
||||
theme : *DEFAULT_THEME,
|
||||
request : None,
|
||||
stylesheets: Assets::<StyleSheet>::new(),
|
||||
javascripts: Assets::<JavaScript>::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<HttpRequest> {
|
||||
&self.request
|
||||
}
|
||||
|
||||
/// Context RENDER.
|
||||
|
||||
pub fn render(&mut self) -> Markup {
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue