From d72e1ccad56221bdafec9568dfb2e443272c7347 Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Wed, 23 Mar 2022 22:29:07 +0100 Subject: [PATCH] =?UTF-8?q?Actualiza=20la=20presentaci=C3=B3n=20del=20r?= =?UTF-8?q?=C3=B3tulo=20de=20arranque?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pagetop/STARTER.bin.Cargo.toml | 10 +- pagetop/STARTER.lib.Cargo.toml | 6 +- pagetop/config/settings.default.toml | 4 +- pagetop/src/app/application.rs | 40 ++------ pagetop/src/app/banner/mod.rs | 40 ++++++++ .../src/app/{figfonts => banner}/slant.flf | 0 .../src/app/{figfonts => banner}/small.flf | 0 .../src/app/{figfonts => banner}/speed.flf | 0 .../src/app/{figfonts => banner}/starwars.flf | 0 pagetop/src/app/mod.rs | 2 + pagetop/src/config.rs | 2 +- pagetop/src/global.rs | 93 ------------------- pagetop/src/lib.rs | 2 - pagetop/src/module/all.rs | 44 +++++++++ pagetop/src/module/mod.rs | 3 +- pagetop/src/response/page/assets.rs | 10 +- pagetop/src/theme/all.rs | 43 +++++++++ pagetop/src/theme/mod.rs | 7 +- 18 files changed, 164 insertions(+), 142 deletions(-) create mode 100644 pagetop/src/app/banner/mod.rs rename pagetop/src/app/{figfonts => banner}/slant.flf (100%) rename pagetop/src/app/{figfonts => banner}/small.flf (100%) rename pagetop/src/app/{figfonts => banner}/speed.flf (100%) rename pagetop/src/app/{figfonts => banner}/starwars.flf (100%) delete mode 100644 pagetop/src/global.rs create mode 100644 pagetop/src/module/all.rs create mode 100644 pagetop/src/theme/all.rs diff --git a/pagetop/STARTER.bin.Cargo.toml b/pagetop/STARTER.bin.Cargo.toml index 2ee2fd9d..9c6c8979 100644 --- a/pagetop/STARTER.bin.Cargo.toml +++ b/pagetop/STARTER.bin.Cargo.toml @@ -8,20 +8,20 @@ edition = "2021" [dependencies.pagetop] path = "../pagetop" -# PageTop por defecto no requiere base de datos. Hay que habilitarla: +# Si se usa base de datos: features = ["mysql"] # features = ["postgres"] # features = ["sqlite"] -# PageTop puede dar soporte a todas las bases de datos: +# Soporte alternativo a todas las bases de datos: # features = ["mysql", "postgres", "sqlite"] # En estos casos hay que deshabilitar las características predeterminadas: default-features = false [dependencies] actix-web = "3.3.3" -# Opcional. Sólo si se usa la macro html!: +# Si se usa la macro html!: maud = { version = "0.23.0" } -# Opcional. Sólo si se usa base de datos: +# Si se usa base de datos: sea-orm = { version = "0.6.0" } -# Opcional. Para la serialización de estructuras de datos: +# Si se requiere serialización de estructuras de datos: serde = { version = "1.0", features = ["derive"] } diff --git a/pagetop/STARTER.lib.Cargo.toml b/pagetop/STARTER.lib.Cargo.toml index 74e74a8a..e98fed13 100644 --- a/pagetop/STARTER.lib.Cargo.toml +++ b/pagetop/STARTER.lib.Cargo.toml @@ -8,9 +8,9 @@ edition = "2021" [dependencies] pagetop = { path = "../pagetop" } -# Opcional. Sólo si se usa la macro html!: +# Si se usa la macro html!: maud = { version = "0.23.0" } -# Opcional. Sólo si se usa base de datos: +# Si se usa base de datos: sea-orm = { version = "0.6.0" } -# Opcional. Para la serialización de estructuras de datos: +# Si se requiere serialización de estructuras de datos: serde = { version = "1.0", features = ["derive"] } diff --git a/pagetop/config/settings.default.toml b/pagetop/config/settings.default.toml index 8ac7ab2d..115b4cb7 100644 --- a/pagetop/config/settings.default.toml +++ b/pagetop/config/settings.default.toml @@ -8,7 +8,7 @@ language = "en-US" # Dirección predeterminada para el texto: "ltr", "rtl" o "auto". direction = "ltr" # Rótulo al inicio: "Off", "Slant", "Small", "Speed" o "Starwars". -startup_banner = "Small" +startup_banner = "Slant" [log] # Traza de ejecución: "Error", "Warn", "Info", "Debug" o "Trace". @@ -25,7 +25,7 @@ format = "Full" [database] # Conecta con una base de datos (opcional). -# Tipo de la base de datos (mysql, postgres ó sqlite). +# Tipo de base de datos (mysql, postgres ó sqlite). db_type = "" # Nombre (para mysql/postgres) o referencia (para sqlite) de la base de datos. db_name = "" diff --git a/pagetop/src/app/application.rs b/pagetop/src/app/application.rs index 274203e6..4ed2c1da 100644 --- a/pagetop/src/app/application.rs +++ b/pagetop/src/app/application.rs @@ -1,6 +1,5 @@ -use crate::{Lazy, app, base, global, trace}; +use crate::{Lazy, app, base, module, theme, trace}; use crate::config::SETTINGS; -use crate::module::register_module; use std::io::Error; use actix_web::middleware::normalize::{NormalizePath, TrailingSlash}; @@ -15,31 +14,8 @@ pub fn essence() { impl Application { pub async fn prepare(bootstrap: fn()) -> Result { - // Imprime un rótulo de presentación (opcional). - if SETTINGS.app.startup_banner.to_lowercase() != "off" { - let figfont = figlet_rs::FIGfont::from_content( - match SETTINGS.app.startup_banner.to_lowercase().as_str() { - "slant" => include_str!("figfonts/slant.flf"), - "small" => include_str!("figfonts/small.flf"), - "speed" => include_str!("figfonts/speed.flf"), - "starwars" => include_str!("figfonts/starwars.flf"), - _ => { - println!( - "FIGfont \"{}\" not found for banner. {}. {}.", - SETTINGS.app.startup_banner, - "Using \"Small\"", - "Check the settings file", - ); - include_str!("figfonts/small.flf") - } - } - ).unwrap(); - println!("\n{} {}\n\n Powered by PageTop {}\n", - figfont.convert(&SETTINGS.app.name).unwrap(), - &SETTINGS.app.description, - env!("CARGO_PKG_VERSION") - ); - } + // Rótulo de presentación. + app::banner::print_on_startup(); // Inicia registro de trazas y eventos. Lazy::force(&app::tracing::TRACING); @@ -57,19 +33,19 @@ impl Application { // Registra el módulo para una página de presentación de PageTop. // Normalmente se sobrecargará en la función de inicio. - register_module(&base::module::homepage::HomepageModule); + module::register_module(&base::module::homepage::HomepageModule); - // Comprueba actualizaciones pendientes de la base de datos (opcional). + // Actualizaciones pendientes de la base de datos (opcional). #[cfg(any(feature = "mysql", feature = "postgres", feature = "sqlite"))] - global::run_migrations(); + module::all::migrations(); // Prepara el servidor web. let server = app::HttpServer::new(move || { app::App::new() .wrap(tracing_actix_web::TracingLogger) .wrap(NormalizePath::new(TrailingSlash::Trim)) - .configure(&global::themes) - .configure(&global::modules) + .configure(&module::all::modules) + .configure(&theme::all::themes) }) .bind(format!("{}:{}", &SETTINGS.webserver.bind_address, diff --git a/pagetop/src/app/banner/mod.rs b/pagetop/src/app/banner/mod.rs new file mode 100644 index 00000000..f6d63841 --- /dev/null +++ b/pagetop/src/app/banner/mod.rs @@ -0,0 +1,40 @@ +use crate::Lazy; +use crate::config::SETTINGS; + +use figlet_rs::FIGfont; + +static FIGFONT: Lazy = Lazy::new(|| { + let slant = include_str!("slant.flf"); + let small = include_str!("small.flf"); + let speed = include_str!("speed.flf"); + let starwars = include_str!("starwars.flf"); + + FIGfont::from_content( + match SETTINGS.app.startup_banner.to_lowercase().as_str() { + "off" => slant, + "slant" => slant, + "small" => small, + "speed" => speed, + "starwars" => starwars, + _ => { + println!( + "\n FIGfont \"{}\" not found for banner. {}. {}.", + SETTINGS.app.startup_banner, + "Using \"Slant\"", + "Check the settings file", + ); + slant + } + } + ).unwrap() +}); + +pub fn print_on_startup() { + if SETTINGS.app.startup_banner.to_lowercase() != "off" { + println!("\n{} {}\n\n Powered by PageTop {}\n", + FIGFONT.convert(&SETTINGS.app.name).unwrap(), + &SETTINGS.app.description, + env!("CARGO_PKG_VERSION") + ); + } +} diff --git a/pagetop/src/app/figfonts/slant.flf b/pagetop/src/app/banner/slant.flf similarity index 100% rename from pagetop/src/app/figfonts/slant.flf rename to pagetop/src/app/banner/slant.flf diff --git a/pagetop/src/app/figfonts/small.flf b/pagetop/src/app/banner/small.flf similarity index 100% rename from pagetop/src/app/figfonts/small.flf rename to pagetop/src/app/banner/small.flf diff --git a/pagetop/src/app/figfonts/speed.flf b/pagetop/src/app/banner/speed.flf similarity index 100% rename from pagetop/src/app/figfonts/speed.flf rename to pagetop/src/app/banner/speed.flf diff --git a/pagetop/src/app/figfonts/starwars.flf b/pagetop/src/app/banner/starwars.flf similarity index 100% rename from pagetop/src/app/figfonts/starwars.flf rename to pagetop/src/app/banner/starwars.flf diff --git a/pagetop/src/app/mod.rs b/pagetop/src/app/mod.rs index b3f7cd6a..07404f64 100644 --- a/pagetop/src/app/mod.rs +++ b/pagetop/src/app/mod.rs @@ -3,6 +3,8 @@ pub use actix_web::{ }; use actix_web::dev::Server; +mod banner; + mod tracing; pub mod locale; diff --git a/pagetop/src/config.rs b/pagetop/src/config.rs index 0a5e322d..775cc4a6 100644 --- a/pagetop/src/config.rs +++ b/pagetop/src/config.rs @@ -130,7 +130,7 @@ Ajustes globales y valores predeterminados para las secciones *\[app\]*, "app.theme" => "Bootsier", "app.language" => "en-US", "app.direction" => "ltr", - "app.startup_banner" => "Small", + "app.startup_banner" => "Slant", // [log] "log.tracing" => "Info", diff --git a/pagetop/src/global.rs b/pagetop/src/global.rs deleted file mode 100644 index 1b4c169f..00000000 --- a/pagetop/src/global.rs +++ /dev/null @@ -1,93 +0,0 @@ -use crate::{Lazy, app, base, run_now, theme_static_files, trace}; -use crate::config::SETTINGS; -use crate::db::migration::*; -use crate::module::*; -use crate::theme::*; - -use std::sync::RwLock; - -// ----------------------------------------------------------------------------- -// Módulos registrados. -// ----------------------------------------------------------------------------- - -pub static MODULES: Lazy>> = Lazy::new( - || { RwLock::new(Vec::new()) } -); - -pub fn register_module(m: &'static dyn ModuleTrait) { - let mut modules = MODULES.write().unwrap(); - match modules.iter().find(|t| t.name() == m.name()) { - None => { - trace::info!("{}", m.name()); - modules.push(m); - }, - Some(_) => {}, - } -} - -pub fn modules(cfg: &mut app::web::ServiceConfig) { - for m in MODULES.read().unwrap().iter() { - m.configure_module(cfg); - } -} - -#[cfg(any(feature = "mysql", feature = "postgres", feature = "sqlite"))] -pub fn run_migrations() { - run_now({ - struct Migrator; - impl MigratorTrait for Migrator { - fn migrations() -> Vec> { - let mut migrations = vec![]; - for m in MODULES.read().unwrap().iter() { - migrations.append(&mut m.migrations()); - } - migrations - } - } - Migrator::up(&app::db::DBCONN, None) - }).unwrap(); -} - -// ----------------------------------------------------------------------------- -// Temas registrados y tema predeterminado. -// ----------------------------------------------------------------------------- - -include!(concat!(env!("OUT_DIR"), "/theme.rs")); - -pub static THEMES: Lazy>> = Lazy::new(|| { - RwLock::new(vec![ - &base::theme::aliner::AlinerTheme, - &base::theme::minimal::MinimalTheme, - &base::theme::bootsier::BootsierTheme, - ]) -}); - -pub static DEFAULT_THEME: Lazy<&dyn ThemeTrait> = Lazy::new(|| { - for t in THEMES.read().unwrap().iter() { - if t.name().to_lowercase() == SETTINGS.app.theme.to_lowercase() { - return *t; - } - } - &base::theme::bootsier::BootsierTheme -}); - -pub fn register_theme(t: &'static dyn ThemeTrait) { - THEMES.write().unwrap().push(t); -} - -pub fn theme_by_name(name: &str) -> Option<&'static dyn ThemeTrait> { - let themes = crate::global::THEMES.write().unwrap(); - match themes.iter().find(|t| t.name() == name) { - Some(theme) => Some(*theme), - _ => None, - } -} - -pub fn themes(cfg: &mut app::web::ServiceConfig) { - theme_static_files!(cfg, "/theme"); - - for t in THEMES.read().unwrap().iter() { - t.configure_theme(cfg); - } -} - diff --git a/pagetop/src/lib.rs b/pagetop/src/lib.rs index 3efe21aa..73e17c8d 100644 --- a/pagetop/src/lib.rs +++ b/pagetop/src/lib.rs @@ -8,8 +8,6 @@ pub use futures::executor::block_on as run_now; // APIs públicas. // ----------------------------------------------------------------------------- -mod global; // Ref. privadas globales a todos los temas y módulos. - pub mod config; // Gestión de la configuración. pub mod trace; // Registro de trazas y eventos de la aplicación. pub mod locale; // Localización. diff --git a/pagetop/src/module/all.rs b/pagetop/src/module/all.rs new file mode 100644 index 00000000..120c59b6 --- /dev/null +++ b/pagetop/src/module/all.rs @@ -0,0 +1,44 @@ +use crate::{Lazy, app, run_now, trace}; +use crate::db::migration::*; +use super::ModuleTrait; + +use std::sync::RwLock; + +// Módulos registrados. +static MODULES: Lazy>> = Lazy::new(|| { + RwLock::new(Vec::new()) +}); + +pub fn register_module(module: &'static dyn ModuleTrait) { + let mut modules = MODULES.write().unwrap(); + match modules.iter().find(|m| m.name() == module.name()) { + None => { + trace::info!("{}", module.name()); + modules.push(module); + }, + Some(_) => {}, + } +} + +pub fn modules(cfg: &mut app::web::ServiceConfig) { + for m in MODULES.read().unwrap().iter() { + m.configure_module(cfg); + } +} + +#[cfg(any(feature = "mysql", feature = "postgres", feature = "sqlite"))] +pub fn migrations() { + run_now({ + struct Migrator; + impl MigratorTrait for Migrator { + fn migrations() -> Vec> { + let mut migrations = vec![]; + for m in MODULES.read().unwrap().iter() { + migrations.append(&mut m.migrations()); + } + migrations + } + } + Migrator::up(&app::db::DBCONN, None) + }).unwrap(); +} diff --git a/pagetop/src/module/mod.rs b/pagetop/src/module/mod.rs index faf51e8b..a767683b 100644 --- a/pagetop/src/module/mod.rs +++ b/pagetop/src/module/mod.rs @@ -1,4 +1,5 @@ mod definition; pub use definition::ModuleTrait; -pub use crate::global::register_module; +pub(crate) mod all; +pub use all::register_module; \ No newline at end of file diff --git a/pagetop/src/response/page/assets.rs b/pagetop/src/response/page/assets.rs index 99e42b06..ffc566ea 100644 --- a/pagetop/src/response/page/assets.rs +++ b/pagetop/src/response/page/assets.rs @@ -1,7 +1,15 @@ -use crate::global::DEFAULT_THEME; +use crate::{Lazy, base}; +use crate::config::SETTINGS; use crate::html::{Markup, PreEscaped, html}; use crate::theme::*; +static DEFAULT_THEME: Lazy<&dyn ThemeTrait> = Lazy::new(|| { + match theme_by_name(&SETTINGS.app.theme) { + Some(theme) => theme, + None => &base::theme::bootsier::BootsierTheme, + } +}); + // ----------------------------------------------------------------------------- // Favicon. // ----------------------------------------------------------------------------- diff --git a/pagetop/src/theme/all.rs b/pagetop/src/theme/all.rs new file mode 100644 index 00000000..cc25d9d8 --- /dev/null +++ b/pagetop/src/theme/all.rs @@ -0,0 +1,43 @@ +use crate::{Lazy, app, base, theme_static_files, trace}; +use super::ThemeTrait; + +use std::sync::RwLock; + +include!(concat!(env!("OUT_DIR"), "/theme.rs")); + +// Temas registrados. +static THEMES: Lazy>> = Lazy::new(|| { + RwLock::new(vec![ + &base::theme::aliner::AlinerTheme, + &base::theme::minimal::MinimalTheme, + &base::theme::bootsier::BootsierTheme, + ]) +}); + +pub fn register_theme(theme: &'static dyn ThemeTrait) { + let mut themes = THEMES.write().unwrap(); + match themes.iter().find(|t| t.name() == theme.name()) { + None => { + trace::info!("{}", theme.name()); + themes.push(theme); + }, + Some(_) => {}, + } +} + +pub fn theme_by_name(name: &str) -> Option<&'static dyn ThemeTrait> { + match THEMES.write().unwrap().iter().find( + |t| t.name().to_lowercase() == name.to_lowercase() + ) { + Some(theme) => Some(*theme), + _ => None, + } +} + +pub fn themes(cfg: &mut app::web::ServiceConfig) { + theme_static_files!(cfg, "/theme"); + + for t in THEMES.read().unwrap().iter() { + t.configure_theme(cfg); + } +} diff --git a/pagetop/src/theme/mod.rs b/pagetop/src/theme/mod.rs index a1d0e4ca..4456189e 100644 --- a/pagetop/src/theme/mod.rs +++ b/pagetop/src/theme/mod.rs @@ -1,5 +1,8 @@ mod definition; pub use definition::ThemeTrait; -pub use crate::global::register_theme; -pub use crate::global::theme_by_name; +pub(crate) mod all; +pub use all::{ + register_theme, + theme_by_name, +};