From 0e3c7dd6c652ba3ded40be2d233f87473248fc9a Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Fri, 4 Nov 2022 19:26:31 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Inicializa=20configuraci?= =?UTF-8?q?=C3=B3n=20global=20en=20m=C3=B3dulo=20app?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pagetop/src/app.rs | 3 + pagetop/src/app/application.rs | 2 +- pagetop/src/app/banner.rs | 2 +- pagetop/src/app/banner/figfont.rs | 4 +- pagetop/src/app/config.rs | 133 +++++++++++++++++++++++ pagetop/src/app/db.rs | 2 +- pagetop/src/app/locale.rs | 2 +- pagetop/src/app/tracing.rs | 2 +- pagetop/src/config.rs | 138 +----------------------- pagetop/src/core/module/definition.rs | 2 - pagetop/src/core/theme/definition.rs | 2 +- pagetop/src/prelude.rs | 16 ++- pagetop/src/response/page/context.rs | 2 +- pagetop/src/response/page/definition.rs | 2 +- pagetop/src/util.rs | 2 +- 15 files changed, 162 insertions(+), 152 deletions(-) create mode 100644 pagetop/src/app/config.rs diff --git a/pagetop/src/app.rs b/pagetop/src/app.rs index 843a32b5..931a71e9 100644 --- a/pagetop/src/app.rs +++ b/pagetop/src/app.rs @@ -4,6 +4,9 @@ pub use actix_web::{ pub use actix_web_files::Files as ActixFiles; pub use actix_web_static_files::ResourceFiles; +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 a5c18c4c..5c20228d 100644 --- a/pagetop/src/app/application.rs +++ b/pagetop/src/app/application.rs @@ -1,5 +1,5 @@ use super::fatal_error::FatalError; -use crate::config::SETTINGS; +use super::SETTINGS; use crate::core::module::ModuleStaticRef; use crate::core::{module, theme}; use crate::html::Markup; diff --git a/pagetop/src/app/banner.rs b/pagetop/src/app/banner.rs index 1e50e54a..43222d2b 100644 --- a/pagetop/src/app/banner.rs +++ b/pagetop/src/app/banner.rs @@ -1,7 +1,7 @@ mod figfont; use figfont::FIGFONT; -use crate::config::SETTINGS; +use super::SETTINGS; use substring::Substring; diff --git a/pagetop/src/app/banner/figfont.rs b/pagetop/src/app/banner/figfont.rs index d92fe35a..31c6c8e2 100644 --- a/pagetop/src/app/banner/figfont.rs +++ b/pagetop/src/app/banner/figfont.rs @@ -1,4 +1,4 @@ -use crate::config::SETTINGS; +use crate::app::SETTINGS; use crate::LazyStatic; use figlet_rs::FIGfont; @@ -17,7 +17,7 @@ pub static FIGFONT: LazyStatic = LazyStatic::new(|| { "starwars" => starwars, _ => { println!( - "\n FIGfont \"{}\" not found for banner. Using \"Slant\". Check the settings file.", + "\n FIGfont \"{}\" not found for banner. Using \"Slant\". Check settings files.", SETTINGS.app.startup_banner, ); slant diff --git a/pagetop/src/app/config.rs b/pagetop/src/app/config.rs new file mode 100644 index 00000000..f7ba57b7 --- /dev/null +++ b/pagetop/src/app/config.rs @@ -0,0 +1,133 @@ +use crate::config; +use crate::predefined_settings; +use crate::LazyStatic; + +use serde::Deserialize; + +#[derive(Debug, Deserialize)] +/// Ajustes globales para las secciones [`[app]`](App), [`[log]`](Log), [`[database]`](Database), +/// [`[webserver]`](Webserver) y [`[dev]`](Dev) reservadas para PageTop ([`SETTINGS`]). +pub struct Settings { + pub app: App, + pub log: Log, + pub database: Database, + pub webserver: Webserver, + pub dev: Dev, +} + +#[derive(Debug, Deserialize)] +/// Sección `[app]` de los ajustes globales. +/// +/// Ver [`Settings`]. +pub struct App { + /// Valor predefinido: *"PageTop Application"* + pub name: String, + /// Valor predefinido: *"Developed with the amazing PageTop framework."* + pub description: String, + /// Valor predefinido: *"Bootsier"* + pub theme: String, + /// Valor predefinido: *"en-US"* + pub language: String, + /// Valor predefinido: *"ltr"* + pub direction: String, + /// Valor predefinido: *"Slant"* + pub startup_banner: String, + /// Valor predefinido: según variable de entorno PAGETOP_RUN_MODE, o *"default"* si no lo está + pub run_mode: String, +} + +#[derive(Debug, Deserialize)] +/// Sección `[log]` de los ajustes globales. +/// +/// Ver [`Settings`]. +pub struct Log { + /// Valor predefinido: *"Info"* + pub tracing: String, + /// Valor predefinido: *"Stdout"* + pub rolling: String, + /// Valor predefinido: *"log"* + pub path: String, + /// Valor predefinido: *"tracing.log"* + pub prefix: String, + /// Valor predefinido: *"Full"* + pub format: String, +} + +#[derive(Debug, Deserialize)] +/// Sección `[database]` de los ajustes globales. +/// +/// Ver [`Settings`]. +pub struct Database { + /// Valor predefinido: *""* + pub db_type: String, + /// Valor predefinido: *""* + pub db_name: String, + /// Valor predefinido: *""* + pub db_user: String, + /// Valor predefinido: *""* + pub db_pass: String, + /// Valor predefinido: *"localhost"* + pub db_host: String, + /// Valor predefinido: *"0"* + pub db_port: u16, + /// Valor predefinido: *"5"* + pub max_pool_size: u32, +} + +#[derive(Debug, Deserialize)] +/// Sección `[webserver]` de los ajustes globales. +/// +/// Ver [`Settings`]. +pub struct Webserver { + /// Valor predefinido: *"localhost"* + pub bind_address: String, + /// Valor predefinido: *"8088"* + pub bind_port: u16, +} + +#[derive(Debug, Deserialize)] +/// Sección `[dev]` de los ajustes globales. +/// +/// Ver [`Settings`]. +pub struct Dev { + /// Valor predefinido: *""* + pub static_files: String, +} + +/// Declara los ajustes globales para la estructura [`Settings`]. +/// +/// Ver [`Cómo usar los ajustes globales de la configuración`](index.html#cómo-usar-los-ajustes-globales-de-la-configuración). +pub static SETTINGS: LazyStatic = LazyStatic::new(|| { + config::try_into::(predefined_settings!( + // [app] + "app.name" => "PageTop Application", + "app.description" => "Developed with the amazing PageTop framework.", + "app.theme" => "Bootsier", + "app.language" => "en-US", + "app.direction" => "ltr", + "app.startup_banner" => "Slant", + + // [log] + "log.tracing" => "Info", + "log.rolling" => "Stdout", + "log.path" => "log", + "log.prefix" => "tracing.log", + "log.format" => "Full", + + // [database] + "database.db_type" => "", + "database.db_name" => "", + "database.db_user" => "", + "database.db_pass" => "", + "database.db_host" => "localhost", + "database.db_port" => "0", + "database.max_pool_size" => "5", + + // [webserver] + "webserver.bind_address" => "localhost", + "webserver.bind_port" => "8088", + + // [dev] + "dev.static_files" => "" + )) +}); diff --git a/pagetop/src/app/db.rs b/pagetop/src/app/db.rs index 98f96e47..1073e609 100644 --- a/pagetop/src/app/db.rs +++ b/pagetop/src/app/db.rs @@ -1,4 +1,4 @@ -use crate::config::SETTINGS; +use super::SETTINGS; use crate::db::*; use crate::{run_now, trace, LazyStatic}; diff --git a/pagetop/src/app/locale.rs b/pagetop/src/app/locale.rs index 9775867f..8517f86b 100644 --- a/pagetop/src/app/locale.rs +++ b/pagetop/src/app/locale.rs @@ -1,4 +1,4 @@ -use crate::config::SETTINGS; +use super::SETTINGS; use crate::{trace, LazyStatic}; use unic_langid::LanguageIdentifier; diff --git a/pagetop/src/app/tracing.rs b/pagetop/src/app/tracing.rs index bc6bf515..264623ef 100644 --- a/pagetop/src/app/tracing.rs +++ b/pagetop/src/app/tracing.rs @@ -1,4 +1,4 @@ -use crate::config::SETTINGS; +use super::SETTINGS; use crate::LazyStatic; use tracing_appender::non_blocking::WorkerGuard; diff --git a/pagetop/src/config.rs b/pagetop/src/config.rs index 8b3fe1d1..e75ee7d7 100644 --- a/pagetop/src/config.rs +++ b/pagetop/src/config.rs @@ -69,13 +69,12 @@ //! ``` //! //! Incluye en tu código una asignación similar a la que usa [`SETTINGS`] para declarar -//! ([`LazyStatic`]) e inicializar tus nuevos ajustes ([`init_settings()`]) con tipos seguros y +//! ([`LazyStatic`]) e inicializar tus nuevos ajustes ([`try_into()`]) con tipos seguros y //! valores predefinidos ([`predefined_settings!`](crate::predefined_settings)): //! //! ``` //! use pagetop::prelude::*; //! use serde::Deserialize; -//! use std::fmt::Debug; //! //! #[derive(Debug, Deserialize)] //! pub struct MySettings { @@ -91,7 +90,7 @@ //! } //! //! pub static MY_SETTINGS: LazyStatic = LazyStatic::new(|| { -//! init_settings::(predefined_settings!( +//! config::try_into::(predefined_settings!( //! // [myapp] //! "myapp.name" => "Value Name", //! "myapp.width" => "900", @@ -131,7 +130,6 @@ use crate::config::file::File; use std::collections::HashMap; use std::env; -use std::fmt::Debug; use serde::Deserialize; @@ -149,7 +147,7 @@ pub type PredefinedSettings = HashMap<&'static str, &'static str>; macro_rules! predefined_settings { ( $($key:literal => $value:literal),* ) => {{ #[allow(unused_mut)] - let mut a = PredefinedSettings::new(); + let mut a = $crate::config::PredefinedSettings::new(); $( a.insert($key, $value); )* @@ -191,7 +189,7 @@ static CONFIG_DATA: LazyStatic = LazyStatic::new(|| { /// estructura similiar a [`SETTINGS`]. /// /// Ver [`Cómo añadir ajustes de configuración`](index.html#cómo-añadir-ajustes-de-configuración). -pub fn init_settings(values: PredefinedSettings) -> T +pub fn try_into(values: PredefinedSettings) -> T where T: Deserialize<'static>, { @@ -204,131 +202,3 @@ where Err(e) => panic!("Error parsing settings: {}", e), } } - -#[derive(Debug, Deserialize)] -/// Ajustes globales para las secciones [`[app]`](App), [`[log]`](Log), [`[database]`](Database), -/// [`[webserver]`](Webserver) y [`[dev]`](Dev) reservadas para PageTop ([`SETTINGS`]). -pub struct Settings { - pub app: App, - pub log: Log, - pub database: Database, - pub webserver: Webserver, - pub dev: Dev, -} - -#[derive(Debug, Deserialize)] -/// Sección `[app]` de los ajustes globales. -/// -/// Ver [`Settings`]. -pub struct App { - /// Valor predefinido: *"PageTop Application"* - pub name: String, - /// Valor predefinido: *"Developed with the amazing PageTop framework."* - pub description: String, - /// Valor predefinido: *"Bootsier"* - pub theme: String, - /// Valor predefinido: *"en-US"* - pub language: String, - /// Valor predefinido: *"ltr"* - pub direction: String, - /// Valor predefinido: *"Slant"* - pub startup_banner: String, - /// Valor predefinido: según variable de entorno PAGETOP_RUN_MODE, o *"default"* si no lo está - pub run_mode: String, -} - -#[derive(Debug, Deserialize)] -/// Sección `[log]` de los ajustes globales. -/// -/// Ver [`Settings`]. -pub struct Log { - /// Valor predefinido: *"Info"* - pub tracing: String, - /// Valor predefinido: *"Stdout"* - pub rolling: String, - /// Valor predefinido: *"log"* - pub path: String, - /// Valor predefinido: *"tracing.log"* - pub prefix: String, - /// Valor predefinido: *"Full"* - pub format: String, -} - -#[derive(Debug, Deserialize)] -/// Sección `[database]` de los ajustes globales. -/// -/// Ver [`Settings`]. -pub struct Database { - /// Valor predefinido: *""* - pub db_type: String, - /// Valor predefinido: *""* - pub db_name: String, - /// Valor predefinido: *""* - pub db_user: String, - /// Valor predefinido: *""* - pub db_pass: String, - /// Valor predefinido: *"localhost"* - pub db_host: String, - /// Valor predefinido: *"0"* - pub db_port: u16, - /// Valor predefinido: *"5"* - pub max_pool_size: u32, -} - -#[derive(Debug, Deserialize)] -/// Sección `[webserver]` de los ajustes globales. -/// -/// Ver [`Settings`]. -pub struct Webserver { - /// Valor predefinido: *"localhost"* - pub bind_address: String, - /// Valor predefinido: *"8088"* - pub bind_port: u16, -} - -#[derive(Debug, Deserialize)] -/// Sección `[dev]` de los ajustes globales. -/// -/// Ver [`Settings`]. -pub struct Dev { - /// Valor predefinido: *""* - pub static_files: String, -} - -/// Declara los ajustes globales para la estructura [`Settings`]. -/// -/// Ver [`Cómo usar los ajustes globales de la configuración`](index.html#cómo-usar-los-ajustes-globales-de-la-configuración). -pub static SETTINGS: LazyStatic = LazyStatic::new(|| { - init_settings::(predefined_settings!( - // [app] - "app.name" => "PageTop Application", - "app.description" => "Developed with the amazing PageTop framework.", - "app.theme" => "Bootsier", - "app.language" => "en-US", - "app.direction" => "ltr", - "app.startup_banner" => "Slant", - - // [log] - "log.tracing" => "Info", - "log.rolling" => "Stdout", - "log.path" => "log", - "log.prefix" => "tracing.log", - "log.format" => "Full", - - // [database] - "database.db_type" => "", - "database.db_name" => "", - "database.db_user" => "", - "database.db_pass" => "", - "database.db_host" => "localhost", - "database.db_port" => "0", - "database.max_pool_size" => "5", - - // [webserver] - "webserver.bind_address" => "localhost", - "webserver.bind_port" => "8088", - - // [dev] - "dev.static_files" => "" - )) -}); diff --git a/pagetop/src/core/module/definition.rs b/pagetop/src/core/module/definition.rs index 16d9d712..b15d1f26 100644 --- a/pagetop/src/core/module/definition.rs +++ b/pagetop/src/core/module/definition.rs @@ -1,6 +1,4 @@ use crate::app; -//use crate::predefined_settings; -//use crate::settings::PredefinedSettings; use crate::core::hook::HookAction; use crate::core::theme::ThemeStaticRef; use crate::util::{single_type_name, Handler}; diff --git a/pagetop/src/core/theme/definition.rs b/pagetop/src/core/theme/definition.rs index ee3abcbf..6e6301e6 100644 --- a/pagetop/src/core/theme/definition.rs +++ b/pagetop/src/core/theme/definition.rs @@ -1,7 +1,7 @@ use crate::app; +use crate::app::SETTINGS; use crate::base::component::{Container, Html}; use crate::concat_string; -use crate::config::SETTINGS; use crate::core::component::ComponentTrait; use crate::html::{html, Favicon, Markup}; use crate::response::page::{Page, PageContext, PageOp}; diff --git a/pagetop/src/prelude.rs b/pagetop/src/prelude.rs index a8e21580..241f1e82 100644 --- a/pagetop/src/prelude.rs +++ b/pagetop/src/prelude.rs @@ -1,11 +1,16 @@ -// Global macros and helpers. +// Re-exports. +pub use crate::{concat_string, LazyStatic}; + +// Macros. +pub use crate::{args, configure_service_for_static_files, predefined_settings, pub_const_handler}; + +// Helpers. pub use crate::util; pub use crate::util::{Handler, HashMapResources}; -pub use crate::{ - args, concat_string, configure_service_for_static_files, pub_const_handler, LazyStatic, -}; -pub use crate::config::SETTINGS; +// ************************************************************************************************* + +pub use crate::config; pub use crate::trace; @@ -20,6 +25,7 @@ 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 055fa781..cc7d5f36 100644 --- a/pagetop/src/response/page/context.rs +++ b/pagetop/src/response/page/context.rs @@ -1,5 +1,5 @@ use super::PageOp; -use crate::config::SETTINGS; +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}; diff --git a/pagetop/src/response/page/definition.rs b/pagetop/src/response/page/definition.rs index 3617b40d..118dbfed 100644 --- a/pagetop/src/response/page/definition.rs +++ b/pagetop/src/response/page/definition.rs @@ -1,6 +1,6 @@ use super::{BeforeRenderPageHook, PageContext, PageOp, ResultPage, HOOK_BEFORE_RENDER_PAGE}; use crate::app::fatal_error::FatalError; -use crate::config::SETTINGS; +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}; diff --git a/pagetop/src/util.rs b/pagetop/src/util.rs index 68d20d3e..5caa741b 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::config::SETTINGS.dev.static_files; + let static_files = &$crate::app::SETTINGS.dev.static_files; if static_files.is_empty() { $cfg.service($crate::app::ResourceFiles::new($dir, $embed())); } else {