Añade lectura de configuración global y modular

- Soporta jerarquía de ficheros TOML que mapean ajustes a estructuras
  fuertemente tipadas con valores predefinidos.
- Permite definir configuraciones distintas para cada entorno.
- Añade la macro `include_config!` para facilitar la asignación modular
  de ajustes de configuración.
- Añade documentación detallada y tests de verificación.
This commit is contained in:
Manuel Cillero 2025-07-05 22:23:05 +02:00
parent cbee4c2cb8
commit f7dbd90af2
14 changed files with 4938 additions and 4 deletions

View file

@ -1,6 +1,10 @@
//! Prepara y ejecuta una aplicación creada con `Pagetop`.
use crate::service;
mod figfont;
use crate::{global, service};
use substring::Substring;
use std::io::Error;
@ -9,14 +13,61 @@ pub struct Application;
impl Application {
/// Crea una instancia de la aplicación.
pub fn new() -> Self {
// Al arrancar muestra una cabecera para la aplicación.
Self::show_banner();
Self
}
// Muestra una cabecera para la aplicación basada en la configuración.
fn show_banner() {
use colored::Colorize;
use terminal_size::{terminal_size, Width};
if global::SETTINGS.app.startup_banner.to_lowercase() != "off" {
// Nombre de la aplicación, ajustado al ancho del terminal si es necesario.
let mut app_ff = String::new();
let app_name = &global::SETTINGS.app.name;
if let Some((Width(term_width), _)) = terminal_size() {
if term_width >= 80 {
let maxlen: usize = ((term_width / 10) - 2).into();
let mut app = app_name.substring(0, maxlen).to_owned();
if app_name.len() > maxlen {
app = format!("{app}...");
}
if let Some(ff) = figfont::FIGFONT.convert(&app) {
app_ff = ff.to_string();
}
}
}
if app_ff.is_empty() {
println!("\n{app_name}");
} else {
print!("\n{app_ff}");
}
// Descripción de la aplicación.
if !global::SETTINGS.app.description.is_empty() {
println!("{}", global::SETTINGS.app.description.cyan());
};
// Versión de PageTop.
println!(
"{} {}\n",
"Powered by PageTop".yellow(),
env!("CARGO_PKG_VERSION").yellow()
);
}
}
/// Ejecuta el servidor web de la aplicación.
pub fn run(self) -> Result<service::Server, Error> {
// Prepara el servidor web.
Ok(service::HttpServer::new(move || Self::service_app())
.bind("localhost:8080")?
.bind(format!(
"{}:{}",
&global::SETTINGS.server.bind_address,
&global::SETTINGS.server.bind_port
))?
.run())
}