♻️ Inicializa configuración global en módulo app

This commit is contained in:
Manuel Cillero 2022-11-04 19:26:31 +01:00
parent 483774e6c1
commit 0e3c7dd6c6
15 changed files with 162 additions and 152 deletions

View file

@ -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;

View file

@ -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;

View file

@ -1,7 +1,7 @@
mod figfont;
use figfont::FIGFONT;
use crate::config::SETTINGS;
use super::SETTINGS;
use substring::Substring;

View file

@ -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<FIGfont> = 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

133
pagetop/src/app/config.rs Normal file
View file

@ -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<Settings> = LazyStatic::new(|| {
config::try_into::<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" => ""
))
});

View file

@ -1,4 +1,4 @@
use crate::config::SETTINGS;
use super::SETTINGS;
use crate::db::*;
use crate::{run_now, trace, LazyStatic};

View file

@ -1,4 +1,4 @@
use crate::config::SETTINGS;
use super::SETTINGS;
use crate::{trace, LazyStatic};
use unic_langid::LanguageIdentifier;

View file

@ -1,4 +1,4 @@
use crate::config::SETTINGS;
use super::SETTINGS;
use crate::LazyStatic;
use tracing_appender::non_blocking::WorkerGuard;

View file

@ -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<MySettings> = LazyStatic::new(|| {
//! init_settings::<MySettings>(predefined_settings!(
//! config::try_into::<MySettings>(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<ConfigData> = 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<T>(values: PredefinedSettings) -> T
pub fn try_into<T>(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<Settings> = LazyStatic::new(|| {
init_settings::<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" => ""
))
});

View file

@ -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};

View file

@ -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};

View file

@ -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::*};

View file

@ -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};

View file

@ -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};

View file

@ -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 {