♻️ (statics): Renombra StaticResource a StaticFile
Clarifica la distinción entre un fichero estático individual (`StaticFile`) y el contenedor de varios ficheros (`StaticResources`).
This commit is contained in:
parent
b1ce79c78f
commit
2c52af4b9d
6 changed files with 32 additions and 37 deletions
|
|
@ -11,30 +11,25 @@
|
|||
|
||||
</div>
|
||||
|
||||
## 🧭 Sobre PageTop
|
||||
## Sobre PageTop
|
||||
|
||||
[PageTop](https://docs.rs/pagetop) es un entorno de desarrollo que reivindica la esencia de la web
|
||||
clásica para crear soluciones web SSR (*renderizadas en el servidor*) modulares, extensibles y
|
||||
configurables, basadas en HTML, CSS y JavaScript.
|
||||
|
||||
|
||||
## 🗺️ Descripción general
|
||||
## Descripción general
|
||||
|
||||
Este *crate* permite incluir archivos estáticos en el ejecutable de las aplicaciones PageTop para
|
||||
servirlos de forma eficiente vía web, con detección de cambios que optimizan el tiempo de
|
||||
compilación.
|
||||
|
||||
## Créditos
|
||||
|
||||
## 📚 Créditos
|
||||
|
||||
Para ello, adapta el código de los *crates* [static-files](https://crates.io/crates/static_files)
|
||||
(versión [0.2.5](https://github.com/static-files-rs/static-files/tree/v0.2.5)) y
|
||||
[actix-web-static-files](https://crates.io/crates/actix_web_static_files) (versión
|
||||
[4.0.1](https://github.com/kilork/actix-web-static-files/tree/v4.0.1)), desarrollados ambos por
|
||||
[Alexander Korolev](https://crates.io/users/kilork).
|
||||
|
||||
Estas implementaciones se integran en PageTop para evitar que cada proyecto tenga que declarar
|
||||
`static-files` manualmente como dependencia en su `Cargo.toml`.
|
||||
Para ello, adapta el código de [static-files](https://crates.io/crates/static_files) (versión
|
||||
[0.2.5](https://github.com/static-files-rs/static-files/tree/v0.2.5)) desarrollado por
|
||||
[Alexander Korolev](https://crates.io/users/kilork), bajo licencia MIT/Apache 2.0. La implementación
|
||||
se integra en PageTop para evitar que cada proyecto tenga que declarar `static-files` manualmente
|
||||
como dependencia en su `Cargo.toml`.
|
||||
|
||||
|
||||
## 🚧 Advertencia
|
||||
|
|
|
|||
|
|
@ -26,14 +26,11 @@ compilación.
|
|||
|
||||
## Créditos
|
||||
|
||||
Para ello, adapta el código de los *crates* [static-files](https://crates.io/crates/static_files)
|
||||
(versión [0.2.5](https://github.com/static-files-rs/static-files/tree/v0.2.5)) y
|
||||
[actix-web-static-files](https://crates.io/crates/actix_web_static_files) (versión
|
||||
[4.0.1](https://github.com/kilork/actix-web-static-files/tree/v4.0.1)), desarrollados ambos por
|
||||
[Alexander Korolev](https://crates.io/users/kilork).
|
||||
|
||||
Estas implementaciones se integran en PageTop para evitar que cada proyecto tenga que declarar
|
||||
`static-files` manualmente como dependencia en su `Cargo.toml`.
|
||||
Para ello, adapta el código de [static-files](https://crates.io/crates/static_files) (versión
|
||||
[0.2.5](https://github.com/static-files-rs/static-files/tree/v0.2.5)) desarrollado por
|
||||
[Alexander Korolev](https://crates.io/users/kilork), bajo licencia MIT/Apache 2.0. La implementación
|
||||
se integra en PageTop para evitar que cada proyecto tenga que declarar `static-files` manualmente
|
||||
como dependencia en su `Cargo.toml`.
|
||||
*/
|
||||
|
||||
#![doc(test(no_crate_inject))]
|
||||
|
|
@ -44,7 +41,7 @@ Estas implementaciones se integran en PageTop para evitar que cada proyecto teng
|
|||
|
||||
/// Resource definition and single module based generation.
|
||||
pub mod resource;
|
||||
pub use resource::Resource as StaticResource;
|
||||
pub use resource::Resource as StaticFile;
|
||||
|
||||
mod resource_dir;
|
||||
pub use resource_dir::{ResourceDir, resource_dir};
|
||||
|
|
|
|||
|
|
@ -93,9 +93,9 @@ pub fn generate_resources<P: AsRef<Path>, G: AsRef<Path>>(
|
|||
/// ```rust
|
||||
/// use std::collections::HashMap;
|
||||
///
|
||||
/// use pagetop_statics::StaticResource;
|
||||
/// use pagetop_statics::StaticFile;
|
||||
///
|
||||
/// fn generate_mapping() -> HashMap<&'static str, StaticResource> {
|
||||
/// fn generate_mapping() -> HashMap<&'static str, StaticFile> {
|
||||
/// include!(concat!(env!("OUT_DIR"), "/generated_mapping.rs"))
|
||||
/// }
|
||||
///
|
||||
|
|
@ -221,7 +221,7 @@ pub(crate) fn generate_function_header<F: Write>(
|
|||
) -> io::Result<()> {
|
||||
writeln!(
|
||||
f,
|
||||
"#[allow(clippy::unreadable_literal)] pub fn {fn_name}() -> ::std::collections::HashMap<&'static str, ::{crate_name}::StaticResource> {{",
|
||||
"#[allow(clippy::unreadable_literal)] pub fn {fn_name}() -> ::std::collections::HashMap<&'static str, ::{crate_name}::StaticFile> {{",
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -116,7 +116,7 @@ where
|
|||
writeln!(
|
||||
module_file,
|
||||
"
|
||||
use ::{crate_name}::StaticResource;
|
||||
use ::{crate_name}::StaticFile;
|
||||
use ::std::collections::HashMap;"
|
||||
)?;
|
||||
|
||||
|
|
@ -177,7 +177,7 @@ fn create_set_module_file(module_dir: &Path, module_index: usize) -> io::Result<
|
|||
"#[allow(clippy::wildcard_imports)]
|
||||
use super::*;
|
||||
#[allow(clippy::unreadable_literal)]
|
||||
pub(crate) fn generate({DEFAULT_VARIABLE_NAME}: &mut HashMap<&'static str, StaticResource>) {{",
|
||||
pub(crate) fn generate({DEFAULT_VARIABLE_NAME}: &mut HashMap<&'static str, StaticFile>) {{",
|
||||
)?;
|
||||
|
||||
Ok(set_module)
|
||||
|
|
|
|||
|
|
@ -134,26 +134,26 @@ pub const PAGETOP_VERSION: &str = env!("CARGO_PKG_VERSION");
|
|||
|
||||
pub use pagetop_macros::{AutoDefault, builder_fn, html, main, test};
|
||||
|
||||
pub use pagetop_statics::{StaticResource, resource};
|
||||
pub use pagetop_statics::{StaticFile, resource};
|
||||
|
||||
pub use getter_methods::Getters;
|
||||
|
||||
/// Contenedor para un conjunto de recursos embebidos.
|
||||
#[derive(AutoDefault)]
|
||||
pub struct StaticResources {
|
||||
bundle: HashMap<&'static str, StaticResource>,
|
||||
bundle: HashMap<&'static str, StaticFile>,
|
||||
}
|
||||
|
||||
impl StaticResources {
|
||||
/// Crea un contenedor para un conjunto de recursos generado por `build.rs` (consultar
|
||||
/// [`pagetop_build`](https://docs.rs/pagetop-build)).
|
||||
pub fn new(bundle: HashMap<&'static str, StaticResource>) -> Self {
|
||||
pub fn new(bundle: HashMap<&'static str, StaticFile>) -> Self {
|
||||
Self { bundle }
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for StaticResources {
|
||||
type Target = HashMap<&'static str, StaticResource>;
|
||||
type Target = HashMap<&'static str, StaticFile>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.bundle
|
||||
|
|
|
|||
15
src/web.rs
15
src/web.rs
|
|
@ -5,6 +5,8 @@
|
|||
//! el módulo `http` para tipos de bajo nivel como `StatusCode`, `HeaderName` o `Method`. También
|
||||
//! ofrece utilidades para servir archivos estáticos, [`ServeDir`] y [`ServeEmbedded`].
|
||||
|
||||
use crate::StaticFile;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::convert::Infallible;
|
||||
use std::task::{Context, Poll};
|
||||
|
|
@ -25,10 +27,6 @@ pub use axum::response::{IntoResponse, Response};
|
|||
// Operaciones HTTP para registrar rutas.
|
||||
pub use axum::routing::{delete, get, patch, post, put};
|
||||
|
||||
// Servicios para archivos estáticos (disco y embebidos).
|
||||
pub use pagetop_statics::StaticResource;
|
||||
pub use tower_http::services::ServeDir;
|
||||
|
||||
// **< HttpRequest >********************************************************************************
|
||||
|
||||
/// Representa una petición HTTP.
|
||||
|
|
@ -91,6 +89,11 @@ impl<S: Send + Sync> FromRequestParts<S> for HttpRequest {
|
|||
}
|
||||
}
|
||||
|
||||
// **< ServeDir >***********************************************************************************
|
||||
|
||||
// Servicio para archivos estáticos en disco.
|
||||
pub use tower_http::services::ServeDir;
|
||||
|
||||
// **< ServeEmbedded >******************************************************************************
|
||||
|
||||
/// Servicio para archivos estáticos embebidos en el binario.
|
||||
|
|
@ -104,12 +107,12 @@ impl<S: Send + Sync> FromRequestParts<S> for HttpRequest {
|
|||
/// recursos con un [`Arc`](std::sync::Arc) para evitar copias innecesarias.
|
||||
#[derive(Clone)]
|
||||
pub struct ServeEmbedded {
|
||||
files: std::sync::Arc<HashMap<&'static str, StaticResource>>,
|
||||
files: std::sync::Arc<HashMap<&'static str, StaticFile>>,
|
||||
}
|
||||
|
||||
impl ServeEmbedded {
|
||||
/// Crea un nuevo servicio a partir del mapa de recursos embebidos generado por `build.rs`.
|
||||
pub fn new(files: HashMap<&'static str, StaticResource>) -> Self {
|
||||
pub fn new(files: HashMap<&'static str, StaticFile>) -> Self {
|
||||
Self {
|
||||
files: std::sync::Arc::new(files),
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue