Modifica y depura el uso de la api interna

This commit is contained in:
Manuel Cillero 2022-05-08 12:53:16 +02:00
parent 897ce6bb64
commit 6b38e4b8ae
11 changed files with 39 additions and 54 deletions

View file

@ -1,13 +1,14 @@
use pagetop::{prelude::*, core::app::AppTrait}; use pagetop::prelude::*;
struct Drust; struct Drust;
impl AppTrait for Drust { impl AppTrait for Drust {
fn enabled_modules(&self) -> Vec<&'static dyn ModuleTrait> { fn enable_modules(&self) -> Vec<&'static dyn ModuleTrait> {
vec![ vec![
&pagetop_admin::Admin, &pagetop_admin::Admin,
&pagetop_user::User, &pagetop_user::User,
&pagetop_node::Node, &pagetop_node::Node,
&demopage::Demopage,
] ]
} }
} }

View file

@ -5,13 +5,14 @@ use super::AppTrait;
use std::io::Error; use std::io::Error;
use actix_web::middleware::normalize::{NormalizePath, TrailingSlash}; use actix_web::middleware::normalize::{NormalizePath, TrailingSlash};
use actix_web::dev::Server;
pub struct Application { pub struct Application {
server: super::Server, server: Server,
} }
impl Application { impl Application {
pub async fn prepare(brrrz: impl AppTrait) -> Result<Self, Error> { pub async fn prepare(app: impl AppTrait) -> Result<Self, Error> {
// Rótulo de presentación. // Rótulo de presentación.
super::banner::print_on_startup(); super::banner::print_on_startup();
@ -25,36 +26,30 @@ impl Application {
#[cfg(any(feature = "mysql", feature = "postgres", feature = "sqlite"))] #[cfg(any(feature = "mysql", feature = "postgres", feature = "sqlite"))]
Lazy::force(&super::db::DBCONN); Lazy::force(&super::db::DBCONN);
// Habilita los módulos de la aplicación.
module::all::enable_modules(app.enable_modules());
// Registra los temas predeterminados. // Registra los temas predeterminados.
theme::register_themes(vec![ theme::all::register_themes(vec![
&base::theme::aliner::Aliner, &base::theme::aliner::Aliner,
&base::theme::minimal::Minimal, &base::theme::minimal::Minimal,
&base::theme::bootsier::Bootsier, &base::theme::bootsier::Bootsier,
&base::theme::bulmix::Bulmix, &base::theme::bulmix::Bulmix,
]); ]);
theme::register_themes(brrrz.register_themes()); // Registra los temas de la aplicación.
theme::all::register_themes(app.themes());
// Habilita los módulos predeterminados.
module::enable_modules(brrrz.enabled_modules());
// Habilita el módulo de presentación de PageTop.
// Normalmente se sobrecargará en la función de inicio.
module::enable_module(&base::module::demopage::Demopage);
// Registra las acciones de todos los módulos. // Registra las acciones de todos los módulos.
module::all::register_hooks(); module::all::register_hooks();
// Ejecuta la función de inicio de la aplicación. // Ejecuta actualizaciones pendientes de la base de datos (opcional).
trace::info!("Calling application bootstrap");
brrrz.bootstrap();
/*
if let UsingBootstrap::Fn(bootstrap) = bootstrap {
let _ = &bootstrap();
}*/
// Actualizaciones pendientes de la base de datos (opcional).
#[cfg(any(feature = "mysql", feature = "postgres", feature = "sqlite"))] #[cfg(any(feature = "mysql", feature = "postgres", feature = "sqlite"))]
module::all::run_migrations(); module::all::run_migrations();
// Ejecuta la función de inicio de la aplicación.
trace::info!("Calling application bootstrap");
app.bootstrap();
// Prepara el servidor web. // Prepara el servidor web.
let server = super::HttpServer::new(move || { let server = super::HttpServer::new(move || {
super::App::new() super::App::new()
@ -72,7 +67,7 @@ impl Application {
Ok(Self { server }) Ok(Self { server })
} }
pub fn run(self) -> Result<super::Server, Error> { pub fn run(self) -> Result<Server, Error> {
Ok(self.server) Ok(self.server)
} }
} }

View file

@ -1,3 +1,4 @@
use crate::base::module::demopage;
use crate::core::module::ModuleTrait; use crate::core::module::ModuleTrait;
use crate::core::theme::ThemeTrait; use crate::core::theme::ThemeTrait;
@ -5,15 +6,17 @@ pub trait AppTrait: Send + Sync {
fn bootstrap(&self) { fn bootstrap(&self) {
} }
fn enabled_modules(&self) -> Vec<&'static dyn ModuleTrait> { fn enable_modules(&self) -> Vec<&'static dyn ModuleTrait> {
vec![
&demopage::Demopage,
]
}
fn disable_modules(&self) -> Vec<&'static dyn ModuleTrait> {
vec![] vec![]
} }
fn disabled_modules(&self) -> Vec<&'static dyn ModuleTrait> { fn themes(&self) -> Vec<&'static dyn ThemeTrait> {
vec![]
}
fn register_themes(&self) -> Vec<&'static dyn ThemeTrait> {
vec![] vec![]
} }
} }

View file

@ -1,7 +1,6 @@
pub use actix_web::{ pub use actix_web::{
App, HttpRequest, HttpResponse, HttpServer, Responder, Result, http, web App, HttpRequest, HttpResponse, HttpServer, Responder, Result, http, web
}; };
use actix_web::dev::Server;
mod banner; mod banner;

View file

@ -4,7 +4,7 @@ use crate::html::{Markup, PreEscaped, html};
use crate::core::theme::*; use crate::core::theme::*;
static DEFAULT_THEME: Lazy<&dyn ThemeTrait> = Lazy::new(|| { static DEFAULT_THEME: Lazy<&dyn ThemeTrait> = Lazy::new(|| {
match theme_by_single_name(&SETTINGS.app.theme) { match all::theme_by_single_name(&SETTINGS.app.theme) {
Some(theme) => theme, Some(theme) => theme,
None => &base::theme::bootsier::Bootsier, None => &base::theme::bootsier::Bootsier,
} }
@ -197,7 +197,7 @@ impl Assets {
} }
pub fn using_theme(&mut self, theme_name: &str) -> &mut Self { pub fn using_theme(&mut self, theme_name: &str) -> &mut Self {
self.theme = theme_by_single_name(theme_name).unwrap_or(*DEFAULT_THEME); self.theme = all::theme_by_single_name(theme_name).unwrap_or(*DEFAULT_THEME);
self self
} }

View file

@ -18,11 +18,11 @@ static DISABLED_MODULES: Lazy<RwLock<Vec<&dyn ModuleTrait>>> = Lazy::new(|| {
pub fn enable_modules(modules: Vec<&'static dyn ModuleTrait>) { pub fn enable_modules(modules: Vec<&'static dyn ModuleTrait>) {
for m in modules { for m in modules {
enable_module(m) enable(m)
} }
} }
pub fn enable_module(module: &'static dyn ModuleTrait) { fn enable(module: &'static dyn ModuleTrait) {
let mut list: Vec<&dyn ModuleTrait> = Vec::new(); let mut list: Vec<&dyn ModuleTrait> = Vec::new();
add_to(&mut list, module); add_to(&mut list, module);
list.reverse(); list.reverse();
@ -43,11 +43,11 @@ fn add_to(list: &mut Vec<&dyn ModuleTrait>, module: &'static dyn ModuleTrait) {
} }
} }
} }
/*
#[allow(unused_variables)] #[allow(unused_variables)]
pub fn disable_module(module: &'static dyn ModuleTrait) { pub fn disable_module(module: &'static dyn ModuleTrait) {
} }
*/
pub fn modules(cfg: &mut app::web::ServiceConfig) { pub fn modules(cfg: &mut app::web::ServiceConfig) {
for m in ENABLED_MODULES.read().unwrap().iter() { for m in ENABLED_MODULES.read().unwrap().iter() {
m.configure_service(cfg); m.configure_service(cfg);

View file

@ -5,8 +5,3 @@ pub use definition::{
}; };
pub(crate) mod all; pub(crate) mod all;
pub use all::{
disable_module,
enable_module,
enable_modules,
};

View file

@ -13,11 +13,11 @@ static THEMES: Lazy<RwLock<Vec<&dyn ThemeTrait>>> = Lazy::new(|| {
pub fn register_themes(themes: Vec<&'static dyn ThemeTrait>) { pub fn register_themes(themes: Vec<&'static dyn ThemeTrait>) {
for t in themes { for t in themes {
register_theme(t) register(t)
} }
} }
pub fn register_theme(theme: &'static dyn ThemeTrait) { fn register(theme: &'static dyn ThemeTrait) {
let mut themes = THEMES.write().unwrap(); let mut themes = THEMES.write().unwrap();
if !themes.iter().any(|t| t.handler() == theme.handler()) { if !themes.iter().any(|t| t.handler() == theme.handler()) {
trace::debug!("Registering theme \"{}\"", theme.single_name()); trace::debug!("Registering theme \"{}\"", theme.single_name());

View file

@ -5,8 +5,3 @@ pub use definition::{
}; };
pub(crate) mod all; pub(crate) mod all;
pub use all::{
register_theme,
register_themes,
theme_by_single_name,
};

View file

@ -18,10 +18,9 @@ pub mod html; // HTML en código.
#[cfg(any(feature = "mysql", feature = "postgres", feature = "sqlite"))] #[cfg(any(feature = "mysql", feature = "postgres", feature = "sqlite"))]
pub mod db; // Acceso a base de datos. pub mod db; // Acceso a base de datos.
pub mod core; // Main APIs for actions, components, modules and themes. pub mod core; // Main APIs for app, components, hooks, modules and themes.
pub mod response; // Tipos de respuestas web. pub mod response; // Tipos de respuestas web.
//pub mod app; // Aplicación y servidor web.
pub mod base; // Base de componentes, módulos y temas. pub mod base; // Base de componentes, módulos y temas.
pub mod util; // Macros y funciones útiles. pub mod util; // Macros y funciones útiles.

View file

@ -21,20 +21,18 @@ pub use crate::{
}; };
pub use crate::{hook_item, core::{ pub use crate::{hook_item, core::{
// app::*, app,
component::*, component::*,
hook::*, hook::*,
module::*, module::*,
theme::*, theme::*,
}}; }};
pub use crate::core::app; pub use crate::core::app::AppTrait;
pub use crate::core::app::application::Application; pub use crate::core::app::application::Application;
pub use crate::response::page::*; pub use crate::response::page::*;
//pub use crate::app;
//pub use crate::app::application::{Application, UsingBootstrap};
pub use crate::base::component::*; pub use crate::base::component::*;
pub use crate::base::module::demopage;
pub use crate::util; pub use crate::util;