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