Mejora la integración de archivos estáticos #5

Merged
manuelcillero merged 2 commits from about-include-files into main 2025-08-16 12:24:38 +02:00
3 changed files with 95 additions and 45 deletions
Showing only changes of commit d810117fa6 - Show all commits

View file

@ -110,11 +110,13 @@
//! } //! }
//! ``` //! ```
use crate::util;
use config::builder::DefaultState; use config::builder::DefaultState;
use config::{Config, ConfigBuilder, File}; use config::{Config, ConfigBuilder, File};
use std::env; use std::env;
use std::path::{Path, PathBuf}; use std::path::PathBuf;
use std::sync::LazyLock; use std::sync::LazyLock;
// Nombre del directorio de configuración por defecto. // Nombre del directorio de configuración por defecto.
@ -125,25 +127,12 @@ const DEFAULT_RUN_MODE: &str = "default";
/// Valores originales cargados desde los archivos de configuración como pares `clave = valor`. /// Valores originales cargados desde los archivos de configuración como pares `clave = valor`.
pub static CONFIG_VALUES: LazyLock<ConfigBuilder<DefaultState>> = LazyLock::new(|| { pub static CONFIG_VALUES: LazyLock<ConfigBuilder<DefaultState>> = LazyLock::new(|| {
// Determina el directorio de configuración: // CONFIG_DIR (si existe) o DEFAULT_CONFIG_DIR. Si no se puede resolver, se usa tal cual.
// - Usa CONFIG_DIR si está definido en el entorno (p.ej.: CONFIG_DIR=/etc/myapp ./myapp). let dir = env::var_os("CONFIG_DIR").unwrap_or_else(|| DEFAULT_CONFIG_DIR.into());
// - Si no, intenta DEFAULT_CONFIG_DIR dentro del proyecto (en CARGO_MANIFEST_DIR). let config_dir = util::resolve_absolute_dir(&dir).unwrap_or_else(|_| PathBuf::from(&dir));
// - Si nada de esto aplica, entonces usa DEFAULT_CONFIG_DIR relativo al ejecutable.
let config_dir: PathBuf = if let Ok(env_dir) = env::var("CONFIG_DIR") {
env_dir.into()
} else if let Ok(manifest_dir) = env::var("CARGO_MANIFEST_DIR") {
let manifest_config = Path::new(&manifest_dir).join(DEFAULT_CONFIG_DIR);
if manifest_config.exists() {
manifest_config
} else {
DEFAULT_CONFIG_DIR.into()
}
} else {
DEFAULT_CONFIG_DIR.into()
};
// Determina el modo de ejecución según la variable de entorno PAGETOP_RUN_MODE. Por defecto usa // Modo de ejecución según la variable de entorno PAGETOP_RUN_MODE. Si no está definida, se usa
// DEFAULT_RUN_MODE si no está definida (p.ej.: PAGETOP_RUN_MODE=production ./myapp). // por defecto, DEFAULT_RUN_MODE (p.ej.: PAGETOP_RUN_MODE=production).
let rm = env::var("PAGETOP_RUN_MODE").unwrap_or_else(|_| DEFAULT_RUN_MODE.into()); let rm = env::var("PAGETOP_RUN_MODE").unwrap_or_else(|_| DEFAULT_RUN_MODE.into());
Config::builder() Config::builder()

View file

@ -2,35 +2,44 @@
use crate::trace; use crate::trace;
use std::env;
use std::io; use std::io;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
// FUNCIONES ÚTILES ******************************************************************************** // FUNCIONES ÚTILES ********************************************************************************
/// Devuelve la ruta absoluta a un directorio existente. /// Resuelve y valida la ruta de un directorio existente, devolviendo una ruta absoluta.
/// ///
/// * Si `relative_path` es una ruta absoluta, entonces se ignora `root_path`. /// - Si la ruta es relativa, se resuelve respecto al directorio del proyecto según la variable de
/// * Si la ruta final es relativa, se convierte en absoluta respecto al directorio actual. /// entorno `CARGO_MANIFEST_DIR` (si existe) o, en su defecto, respecto al directorio actual de
/// * Devuelve error si la ruta no existe o no es un directorio. /// trabajo.
/// - Normaliza y valida la ruta final (resuelve `.`/`..` y enlaces simbólicos).
/// - Devuelve error si la ruta no existe o no es un directorio.
/// ///
/// # Ejemplo /// # Ejemplos
/// ///
/// ```rust,no_run /// ```rust,no_run
/// use pagetop::prelude::*; /// use pagetop::prelude::*;
/// ///
/// let root = "/home/user"; /// // Ruta relativa, se resuelve respecto a CARGO_MANIFEST_DIR o al directorio actual (`cwd`).
/// let rel = "documents"; /// println!("{:#?}", util::resolve_absolute_dir("documents"));
/// println!("{:#?}", util::absolute_dir(root, rel)); ///
/// // Ruta absoluta, se normaliza y valida tal cual.
/// println!("{:#?}", util::resolve_absolute_dir("/var/www"));
/// ``` /// ```
pub fn absolute_dir<P, Q>(root_path: P, relative_path: Q) -> io::Result<PathBuf> pub fn resolve_absolute_dir<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
where let path = path.as_ref();
P: AsRef<Path>,
Q: AsRef<Path>, let candidate = if path.is_absolute() {
{ path.to_path_buf()
// Une ambas rutas: } else {
// - Si `relative_path` es absoluta, el `join` la devuelve tal cual, descartando `root_path`. // Directorio base CARGO_MANIFEST_DIR si está disponible; o current_dir() en su defecto.
// - Si el resultado es aún relativo, lo será respecto al directorio actual. env::var_os("CARGO_MANIFEST_DIR")
let candidate = root_path.as_ref().join(relative_path.as_ref()); .map(PathBuf::from)
.or_else(|| env::current_dir().ok())
.unwrap_or_else(|| PathBuf::from("."))
.join(path)
};
// Resuelve `.`/`..`, enlaces simbólicos y obtiene la ruta absoluta en un único paso. // Resuelve `.`/`..`, enlaces simbólicos y obtiene la ruta absoluta en un único paso.
let absolute_dir = candidate.canonicalize()?; let absolute_dir = candidate.canonicalize()?;
@ -47,6 +56,16 @@ where
} }
} }
/// Devuelve la ruta absoluta a un directorio existente.
#[deprecated(since = "0.3.0", note = "Use [`resolve_absolute_dir`] instead")]
pub fn absolute_dir<P, Q>(root_path: P, relative_path: Q) -> io::Result<PathBuf>
where
P: AsRef<Path>,
Q: AsRef<Path>,
{
resolve_absolute_dir(root_path.as_ref().join(relative_path.as_ref()))
}
// MACROS ÚTILES *********************************************************************************** // MACROS ÚTILES ***********************************************************************************
#[doc(hidden)] #[doc(hidden)]

View file

@ -1,6 +1,6 @@
use pagetop::prelude::*; use pagetop::prelude::*;
use std::{fs, io}; use std::{env, fs, io};
use tempfile::TempDir; use tempfile::TempDir;
#[cfg(unix)] #[cfg(unix)]
@ -13,15 +13,36 @@ mod unix {
// /tmp/<rand>/sub // /tmp/<rand>/sub
let td = TempDir::new()?; let td = TempDir::new()?;
let root = td.path(); let sub = td.path().join("sub");
let sub = root.join("sub");
fs::create_dir(&sub)?; fs::create_dir(&sub)?;
let abs = util::absolute_dir(root, "sub")?; let abs = util::resolve_absolute_dir(&sub)?;
assert_eq!(abs, std::fs::canonicalize(&sub)?); assert_eq!(abs, std::fs::canonicalize(&sub)?);
Ok(()) Ok(())
} }
#[pagetop::test]
async fn ok_relative_dir_with_manifest() -> io::Result<()> {
let _app = service::test::init_service(Application::new().test()).await;
let td = TempDir::new()?;
let sub = td.path().join("sub");
fs::create_dir(&sub)?;
// Fija CARGO_MANIFEST_DIR para que "sub" se resuelva contra td.path()
let prev_manifest_dir = env::var_os("CARGO_MANIFEST_DIR");
env::set_var("CARGO_MANIFEST_DIR", td.path());
let res = util::resolve_absolute_dir("sub");
// Restaura entorno.
match prev_manifest_dir {
Some(v) => env::set_var("CARGO_MANIFEST_DIR", v),
None => env::remove_var("CARGO_MANIFEST_DIR"),
}
assert_eq!(res?, std::fs::canonicalize(&sub)?);
Ok(())
}
#[pagetop::test] #[pagetop::test]
async fn error_not_a_directory() -> io::Result<()> { async fn error_not_a_directory() -> io::Result<()> {
let _app = service::test::init_service(Application::new().test()).await; let _app = service::test::init_service(Application::new().test()).await;
@ -30,7 +51,7 @@ mod unix {
let file = td.path().join("foo.txt"); let file = td.path().join("foo.txt");
fs::write(&file, b"data")?; fs::write(&file, b"data")?;
let err = util::absolute_dir(td.path(), "foo.txt").unwrap_err(); let err = util::resolve_absolute_dir(&file).unwrap_err();
assert_eq!(err.kind(), io::ErrorKind::InvalidInput); assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
Ok(()) Ok(())
} }
@ -46,15 +67,36 @@ mod windows {
// C:\Users\...\Temp\... // C:\Users\...\Temp\...
let td = TempDir::new()?; let td = TempDir::new()?;
let root = td.path(); let sub = td.path().join("sub");
let sub = root.join("sub");
fs::create_dir(&sub)?; fs::create_dir(&sub)?;
let abs = util::absolute_dir(root, sub.as_path())?; let abs = util::resolve_absolute_dir(&sub)?;
assert_eq!(abs, std::fs::canonicalize(&sub)?); assert_eq!(abs, std::fs::canonicalize(&sub)?);
Ok(()) Ok(())
} }
#[pagetop::test]
async fn ok_relative_dir_with_manifest() -> io::Result<()> {
let _app = service::test::init_service(Application::new().test()).await;
let td = TempDir::new()?;
let sub = td.path().join("sub");
fs::create_dir(&sub)?;
// Fija CARGO_MANIFEST_DIR para que "sub" se resuelva contra td.path()
let prev_manifest_dir = env::var_os("CARGO_MANIFEST_DIR");
env::set_var("CARGO_MANIFEST_DIR", td.path());
let res = util::resolve_absolute_dir("sub");
// Restaura entorno.
match prev_manifest_dir {
Some(v) => env::set_var("CARGO_MANIFEST_DIR", v),
None => env::remove_var("CARGO_MANIFEST_DIR"),
}
assert_eq!(res?, std::fs::canonicalize(&sub)?);
Ok(())
}
#[pagetop::test] #[pagetop::test]
async fn error_not_a_directory() -> io::Result<()> { async fn error_not_a_directory() -> io::Result<()> {
let _app = service::test::init_service(Application::new().test()).await; let _app = service::test::init_service(Application::new().test()).await;
@ -63,7 +105,7 @@ mod windows {
let file = td.path().join("foo.txt"); let file = td.path().join("foo.txt");
fs::write(&file, b"data")?; fs::write(&file, b"data")?;
let err = util::absolute_dir(td.path(), "foo.txt").unwrap_err(); let err = util::resolve_absolute_dir(&file).unwrap_err();
assert_eq!(err.kind(), io::ErrorKind::InvalidInput); assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
Ok(()) Ok(())
} }