From aa9f392ba92f15201186bd0f7b79842027bff656 Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Thu, 10 Nov 2022 21:43:38 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20[global]=20Para=20acceder?= =?UTF-8?q?=20a=20elementos=20globales?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pagetop-user/src/lib.rs | 2 +- pagetop/src/app.rs | 3 --- pagetop/src/app/application.rs | 9 +++----- pagetop/src/app/banner.rs | 14 ++++++------ pagetop/src/app/banner/figfont.rs | 7 +++--- pagetop/src/app/db.rs | 29 ++++++++++++------------ pagetop/src/app/locale.rs | 7 +++--- pagetop/src/app/tracing.rs | 17 +++++++------- pagetop/src/base/module/homepage.rs | 7 ++++-- pagetop/src/config.rs | 6 ++--- pagetop/src/core/theme/definition.rs | 8 +++---- pagetop/src/{app/config.rs => global.rs} | 2 -- pagetop/src/lib.rs | 3 +++ pagetop/src/prelude.rs | 3 ++- pagetop/src/response/page/context.rs | 6 ++--- pagetop/src/response/page/definition.rs | 10 ++++---- pagetop/src/util.rs | 2 +- 17 files changed, 64 insertions(+), 71 deletions(-) rename pagetop/src/{app/config.rs => global.rs} (98%) diff --git a/pagetop-user/src/lib.rs b/pagetop-user/src/lib.rs index 6b192af8..b527b7c6 100644 --- a/pagetop-user/src/lib.rs +++ b/pagetop-user/src/lib.rs @@ -58,7 +58,7 @@ fn form_login() -> Form { t( "username_help", &args![ - "app" => SETTINGS.app.name.to_owned() + "app" => global::SETTINGS.app.name.to_owned() ], ) .as_str(), diff --git a/pagetop/src/app.rs b/pagetop/src/app.rs index cb5a3f79..843a32b5 100644 --- a/pagetop/src/app.rs +++ b/pagetop/src/app.rs @@ -4,9 +4,6 @@ pub use actix_web::{ pub use actix_web_files::Files as ActixFiles; pub use actix_web_static_files::ResourceFiles; -pub mod config; -pub use config::SETTINGS; - mod banner; mod tracing; diff --git a/pagetop/src/app/application.rs b/pagetop/src/app/application.rs index 5482992c..4b0e0ff9 100644 --- a/pagetop/src/app/application.rs +++ b/pagetop/src/app/application.rs @@ -1,10 +1,10 @@ use super::fatal_error::FatalError; -use super::SETTINGS; + use crate::core::module::ModuleStaticRef; use crate::core::{module, theme}; use crate::html::Markup; use crate::response::page::ResultPage; -use crate::LazyStatic; +use crate::{global, LazyStatic}; use actix_web::dev::Server; @@ -19,9 +19,6 @@ impl Application { // Rótulo de presentación. super::banner::print_on_startup(); - // Inicializa la configuración global. - LazyStatic::force(&super::config::SETTINGS); - // Inicia registro de trazas y eventos. LazyStatic::force(&super::tracing::TRACING); @@ -58,7 +55,7 @@ impl Application { }) .bind(format!( "{}:{}", - &SETTINGS.server.bind_address, &SETTINGS.server.bind_port + &global::SETTINGS.server.bind_address, &global::SETTINGS.server.bind_port ))? .run(); diff --git a/pagetop/src/app/banner.rs b/pagetop/src/app/banner.rs index 43222d2b..40697c71 100644 --- a/pagetop/src/app/banner.rs +++ b/pagetop/src/app/banner.rs @@ -1,23 +1,23 @@ mod figfont; use figfont::FIGFONT; -use super::SETTINGS; +use crate::global; use substring::Substring; pub fn print_on_startup() { - if SETTINGS.app.startup_banner.to_lowercase() != "off" { + if global::SETTINGS.app.startup_banner.to_lowercase() != "off" { if let Some((term_width, _)) = term_size::dimensions() { if term_width >= 80 { let maxlen = (term_width / 10) - 2; - let mut app = SETTINGS.app.name.substring(0, maxlen).to_owned(); - if SETTINGS.app.name.len() > maxlen { + let mut app = global::SETTINGS.app.name.substring(0, maxlen).to_owned(); + if global::SETTINGS.app.name.len() > maxlen { app = format!("{}...", app); } println!( "\n{} {}\n\n Powered by PageTop {}\n", FIGFONT.convert(&app).unwrap(), - &SETTINGS.app.description, + &global::SETTINGS.app.description, env!("CARGO_PKG_VERSION") ); return; @@ -25,8 +25,8 @@ pub fn print_on_startup() { } println!( "\n{}\n{}\n\nPowered by PageTop {}\n", - &SETTINGS.app.name, - &SETTINGS.app.description, + &global::SETTINGS.app.name, + &global::SETTINGS.app.description, env!("CARGO_PKG_VERSION") ); } diff --git a/pagetop/src/app/banner/figfont.rs b/pagetop/src/app/banner/figfont.rs index 31c6c8e2..880a3342 100644 --- a/pagetop/src/app/banner/figfont.rs +++ b/pagetop/src/app/banner/figfont.rs @@ -1,5 +1,4 @@ -use crate::app::SETTINGS; -use crate::LazyStatic; +use crate::{global, LazyStatic}; use figlet_rs::FIGfont; @@ -9,7 +8,7 @@ pub static FIGFONT: LazyStatic = LazyStatic::new(|| { let speed = include_str!("speed.flf"); let starwars = include_str!("starwars.flf"); - FIGfont::from_content(match SETTINGS.app.startup_banner.to_lowercase().as_str() { + FIGfont::from_content(match global::SETTINGS.app.startup_banner.to_lowercase().as_str() { "off" => slant, "slant" => slant, "small" => small, @@ -18,7 +17,7 @@ pub static FIGFONT: LazyStatic = LazyStatic::new(|| { _ => { println!( "\n FIGfont \"{}\" not found for banner. Using \"Slant\". Check settings files.", - SETTINGS.app.startup_banner, + global::SETTINGS.app.startup_banner, ); slant } diff --git a/pagetop/src/app/db.rs b/pagetop/src/app/db.rs index 1073e609..44491b67 100644 --- a/pagetop/src/app/db.rs +++ b/pagetop/src/app/db.rs @@ -1,6 +1,5 @@ -use super::SETTINGS; use crate::db::*; -use crate::{run_now, trace, LazyStatic}; +use crate::{global, run_now, trace, LazyStatic}; use sea_orm::{ConnectOptions, ConnectionTrait, Database, DatabaseBackend, Statement}; use tracing_unwrap::ResultExt; @@ -8,38 +7,38 @@ use tracing_unwrap::ResultExt; pub static DBCONN: LazyStatic = LazyStatic::new(|| { trace::info!( "Connecting to database \"{}\" using a pool of {} connections", - &SETTINGS.database.db_name, - &SETTINGS.database.max_pool_size + &global::SETTINGS.database.db_name, + &global::SETTINGS.database.max_pool_size ); - let db_uri = match SETTINGS.database.db_type.as_str() { + let db_uri = match global::SETTINGS.database.db_type.as_str() { "mysql" | "postgres" => { let mut tmp_uri = DbUri::parse( format!( "{}://{}/{}", - &SETTINGS.database.db_type, - &SETTINGS.database.db_host, - &SETTINGS.database.db_name + &global::SETTINGS.database.db_type, + &global::SETTINGS.database.db_host, + &global::SETTINGS.database.db_name ) .as_str(), ) .unwrap(); tmp_uri - .set_username(SETTINGS.database.db_user.as_str()) + .set_username(global::SETTINGS.database.db_user.as_str()) .unwrap(); // https://github.com/launchbadge/sqlx/issues/1624 tmp_uri - .set_password(Some(SETTINGS.database.db_pass.as_str())) + .set_password(Some(global::SETTINGS.database.db_pass.as_str())) .unwrap(); - if SETTINGS.database.db_port != 0 { - tmp_uri.set_port(Some(SETTINGS.database.db_port)).unwrap(); + if global::SETTINGS.database.db_port != 0 { + tmp_uri.set_port(Some(global::SETTINGS.database.db_port)).unwrap(); } tmp_uri } "sqlite" => DbUri::parse( format!( "{}://{}", - &SETTINGS.database.db_type, &SETTINGS.database.db_name + &global::SETTINGS.database.db_type, &global::SETTINGS.database.db_name ) .as_str(), ) @@ -47,7 +46,7 @@ pub static DBCONN: LazyStatic = LazyStatic::new(|| { _ => { trace::error!( "Unrecognized database type \"{}\"", - &SETTINGS.database.db_type + &global::SETTINGS.database.db_type ); DbUri::parse("").unwrap() } @@ -55,7 +54,7 @@ pub static DBCONN: LazyStatic = LazyStatic::new(|| { run_now(Database::connect::({ let mut db_opt = ConnectOptions::new(db_uri.to_string()); - db_opt.max_connections(SETTINGS.database.max_pool_size); + db_opt.max_connections(global::SETTINGS.database.max_pool_size); db_opt })) .expect_or_log("Failed to connect to database") diff --git a/pagetop/src/app/locale.rs b/pagetop/src/app/locale.rs index 06172362..e6e1c4ef 100644 --- a/pagetop/src/app/locale.rs +++ b/pagetop/src/app/locale.rs @@ -1,5 +1,4 @@ -use super::SETTINGS; -use crate::{trace, LazyStatic}; +use crate::{global, trace, LazyStatic}; use unic_langid::LanguageIdentifier; @@ -7,14 +6,14 @@ use unic_langid::LanguageIdentifier; /// ([Unicode Language Identifier](https://unicode.org/reports/tr35/tr35.html#Unicode_language_identifier)) /// de la aplicación, obtenido de `SETTINGS.app.language`. pub static LANGID: LazyStatic = - LazyStatic::new(|| match SETTINGS.app.language.parse() { + LazyStatic::new(|| match global::SETTINGS.app.language.parse() { Ok(language) => language, Err(_) => { trace::warn!( "{}, {} \"{}\"! {}, {}", "Failed to parse language", "unrecognized Unicode Language Identifier", - SETTINGS.app.language, + global::SETTINGS.app.language, "Using \"en-US\"", "check the settings file", ); diff --git a/pagetop/src/app/tracing.rs b/pagetop/src/app/tracing.rs index 86f1f682..3c7b58a8 100644 --- a/pagetop/src/app/tracing.rs +++ b/pagetop/src/app/tracing.rs @@ -1,5 +1,4 @@ -use super::SETTINGS; -use crate::LazyStatic; +use crate::{global, LazyStatic}; use tracing_appender::non_blocking::WorkerGuard; use tracing_subscriber::EnvFilter; @@ -20,14 +19,14 @@ use tracing_subscriber::EnvFilter; #[rustfmt::skip] pub static TRACING: LazyStatic = LazyStatic::new(|| { let env_filter = - EnvFilter::try_new(&SETTINGS.log.tracing).unwrap_or_else(|_| EnvFilter::new("Info")); + EnvFilter::try_new(&global::SETTINGS.log.tracing).unwrap_or_else(|_| EnvFilter::new("Info")); - let rolling = SETTINGS.log.rolling.to_lowercase(); + let rolling = global::SETTINGS.log.rolling.to_lowercase(); let (non_blocking, guard) = match rolling.as_str() { "stdout" => tracing_appender::non_blocking(std::io::stdout()), _ => tracing_appender::non_blocking({ - let path = &SETTINGS.log.path; - let prefix = &SETTINGS.log.prefix; + let path = &global::SETTINGS.log.path; + let prefix = &global::SETTINGS.log.prefix; match rolling.as_str() { "daily" => tracing_appender::rolling::daily(path, prefix), "hourly" => tracing_appender::rolling::hourly(path, prefix), @@ -36,7 +35,7 @@ pub static TRACING: LazyStatic = LazyStatic::new(|| { _ => { println!( "Rolling value \"{}\" not valid. Using \"daily\". Check the settings file.", - SETTINGS.log.rolling, + global::SETTINGS.log.rolling, ); tracing_appender::rolling::daily(path, prefix) } @@ -47,7 +46,7 @@ pub static TRACING: LazyStatic = LazyStatic::new(|| { .with_env_filter(env_filter) .with_writer(non_blocking) .with_ansi(rolling.as_str() == "stdout"); - match SETTINGS.log.format.to_lowercase().as_str() { + match global::SETTINGS.log.format.to_lowercase().as_str() { "json" => subscriber.json().init(), "full" => subscriber.init(), "compact" => subscriber.compact().init(), @@ -55,7 +54,7 @@ pub static TRACING: LazyStatic = LazyStatic::new(|| { _ => { println!( "Tracing format \"{}\" not valid. Using \"Full\". Check the settings file.", - SETTINGS.log.format, + global::SETTINGS.log.format, ); subscriber.init(); } diff --git a/pagetop/src/base/module/homepage.rs b/pagetop/src/base/module/homepage.rs index dee2e7b3..4152acb9 100644 --- a/pagetop/src/base/module/homepage.rs +++ b/pagetop/src/base/module/homepage.rs @@ -57,7 +57,7 @@ fn hello_world() -> Container { (e("hello_intro", &args![ "app" => format!( "{}", - &SETTINGS.app.name, + &global::SETTINGS.app.name, ) ])) }) @@ -108,7 +108,10 @@ fn welcome() -> Container { .with_component( Heading::h3(html! { (e("welcome_subtitle", &args![ - "app" => format!("{}", &SETTINGS.app.name) + "app" => format!( + "{}", + &global::SETTINGS.app.name + ) ])) }) .with_display(HeadingDisplay::Subtitle), diff --git a/pagetop/src/config.rs b/pagetop/src/config.rs index 318e47e5..a49b0a71 100644 --- a/pagetop/src/config.rs +++ b/pagetop/src/config.rs @@ -100,9 +100,9 @@ //! use pagetop::prelude::*; //! //! fn global_settings() { -//! println!("App name: {}", &SETTINGS.app.name); -//! println!("App description: {}", &SETTINGS.app.description); -//! println!("Value of PAGETOP_RUN_MODE: {}", &SETTINGS.app.run_mode); +//! println!("App name: {}", &global::SETTINGS.app.name); +//! println!("App description: {}", &global::SETTINGS.app.description); +//! println!("Value of PAGETOP_RUN_MODE: {}", &global::SETTINGS.app.run_mode); //! } //! //! fn module_settings() { diff --git a/pagetop/src/core/theme/definition.rs b/pagetop/src/core/theme/definition.rs index 112bab37..744006d8 100644 --- a/pagetop/src/core/theme/definition.rs +++ b/pagetop/src/core/theme/definition.rs @@ -1,7 +1,5 @@ -use crate::app; -use crate::app::SETTINGS; +use crate::{app, concat_string, global}; use crate::base::component::{Container, Html}; -use crate::concat_string; use crate::core::component::ComponentTrait; use crate::html::{html, Favicon, Markup}; use crate::response::page::{Page, PageContext, PageOp}; @@ -43,9 +41,9 @@ pub trait ThemeTrait: BaseTheme + Send + Sync { @match page.title().get() { Some(t) => title { - (concat_string!(SETTINGS.app.name, " | ", t)) + (concat_string!(global::SETTINGS.app.name, " | ", t)) }, - None => title { (SETTINGS.app.name) } + None => title { (global::SETTINGS.app.name) } } @if let Some(d) = page.description().get() { diff --git a/pagetop/src/app/config.rs b/pagetop/src/global.rs similarity index 98% rename from pagetop/src/app/config.rs rename to pagetop/src/global.rs index 4bdac5a2..041216db 100644 --- a/pagetop/src/app/config.rs +++ b/pagetop/src/global.rs @@ -1,5 +1,3 @@ -//! Ajustes globales de la configuración. - use crate::pub_config; use serde::Deserialize; diff --git a/pagetop/src/lib.rs b/pagetop/src/lib.rs index ff543aed..d1a06327 100644 --- a/pagetop/src/lib.rs +++ b/pagetop/src/lib.rs @@ -62,6 +62,9 @@ pub mod html; #[cfg(feature = "database")] pub mod db; +// Acceso a declaraciones globales. +pub mod global; + // Prepara y ejecuta la aplicación. pub mod app; diff --git a/pagetop/src/prelude.rs b/pagetop/src/prelude.rs index de5b762f..852b070f 100644 --- a/pagetop/src/prelude.rs +++ b/pagetop/src/prelude.rs @@ -19,11 +19,12 @@ pub use crate::html::*; #[cfg(feature = "database")] pub use crate::{db, db::*, migration_item, pub_migration}; +pub use crate::global; + pub use crate::app; pub use crate::app::application::Application; pub use crate::app::fatal_error::FatalError; pub use crate::app::HttpMessage; -pub use crate::app::SETTINGS; pub use crate::core::{component::*, hook::*, module::*, theme::*}; diff --git a/pagetop/src/response/page/context.rs b/pagetop/src/response/page/context.rs index cc7d5f36..76a39450 100644 --- a/pagetop/src/response/page/context.rs +++ b/pagetop/src/response/page/context.rs @@ -1,11 +1,11 @@ use super::PageOp; -use crate::app::SETTINGS; + use crate::core::theme::{all::theme_by_single_name, ThemeStaticRef}; use crate::html::{html, Assets, Favicon, IdentifierValue, JavaScript, Markup, ModeJS, StyleSheet}; -use crate::{base, concat_string, util, LazyStatic}; +use crate::{base, concat_string, global, util, LazyStatic}; static DEFAULT_THEME: LazyStatic = - LazyStatic::new(|| match theme_by_single_name(&SETTINGS.app.theme) { + LazyStatic::new(|| match theme_by_single_name(&global::SETTINGS.app.theme) { Some(theme) => theme, None => &base::theme::bootsier::Bootsier, }); diff --git a/pagetop/src/response/page/definition.rs b/pagetop/src/response/page/definition.rs index 118dbfed..97e58645 100644 --- a/pagetop/src/response/page/definition.rs +++ b/pagetop/src/response/page/definition.rs @@ -1,15 +1,15 @@ use super::{BeforeRenderPageHook, PageContext, PageOp, ResultPage, HOOK_BEFORE_RENDER_PAGE}; + use crate::app::fatal_error::FatalError; -use crate::app::SETTINGS; use crate::core::component::*; use crate::core::hook::{action_ref, run_actions}; use crate::html::{html, AttributeValue, Classes, ClassesOp, Markup, DOCTYPE}; -use crate::{trace, LazyStatic}; +use crate::{global, trace, LazyStatic}; use std::collections::HashMap; static DEFAULT_LANGUAGE: LazyStatic> = LazyStatic::new(|| { - let language = SETTINGS.app.language[..2].to_lowercase(); + let language = global::SETTINGS.app.language[..2].to_lowercase(); if !language.is_empty() { Some(language) } else { @@ -18,7 +18,7 @@ static DEFAULT_LANGUAGE: LazyStatic> = LazyStatic::new(|| { }); static DEFAULT_DIRECTION: LazyStatic> = LazyStatic::new(|| { - let direction = SETTINGS.app.direction.to_lowercase(); + let direction = global::SETTINGS.app.direction.to_lowercase(); match direction.as_str() { "auto" => Some("auto".to_owned()), "ltr" => Some("ltr".to_owned()), @@ -27,7 +27,7 @@ static DEFAULT_DIRECTION: LazyStatic> = LazyStatic::new(|| { _ => { trace::warn!( "Text direction \"{}\" not valid, {}", - SETTINGS.app.direction, + global::SETTINGS.app.direction, "check the settings file" ); None diff --git a/pagetop/src/util.rs b/pagetop/src/util.rs index 91d9c07d..e926966e 100644 --- a/pagetop/src/util.rs +++ b/pagetop/src/util.rs @@ -169,7 +169,7 @@ macro_rules! args { #[macro_export] macro_rules! configure_service_for_static_files { ( $cfg:ident, $dir:expr, $embed:ident ) => {{ - let static_files = &$crate::app::SETTINGS.dev.static_files; + let static_files = &$crate::global::SETTINGS.dev.static_files; if static_files.is_empty() { $cfg.service($crate::app::ResourceFiles::new($dir, $embed())); } else {