Mejora gestión de archivos estáticos en el binario

This commit is contained in:
Manuel Cillero 2022-08-05 00:05:26 +02:00
parent c0a269f009
commit 9997a6b0b1
14 changed files with 163 additions and 72 deletions

View file

@ -19,4 +19,4 @@ static-files = "0.2.3"
maud = { git = "https://github.com/lambda-fairy/maud", rev = "e6787cd6" }
[build-dependencies]
static-files = "0.2.3"
pagetop = { path = "../pagetop" }

View file

@ -1,17 +1,5 @@
use static_files::resource_dir;
use std::env;
use std::path::Path;
use pagetop::util::bundle_resources;
fn main() -> std::io::Result<()> {
build_resource_dir("./static", "mdbook")
}
fn build_resource_dir(dir: &str, name: &str) -> std::io::Result<()> {
let mut resource = resource_dir(dir);
resource.with_generated_filename(
Path::new(env::var("OUT_DIR").unwrap().as_str()).join(format!("{}.rs", name)),
);
resource.with_module_name(format!("resources_{}", name));
resource.build()
bundle_resources("./static", "mdbook", None)
}

View file

@ -1,11 +1,11 @@
use pagetop::prelude::*;
pub mod util;
pub_const_handler!(MODULE_MDBOOK);
include!(concat!(env!("OUT_DIR"), "/mdbook.rs"));
pub type BookMapResources = std::collections::HashMap<&'static str, static_files::Resource>;
pub struct MdBook;
impl ModuleTrait for MdBook {
@ -15,14 +15,14 @@ impl ModuleTrait for MdBook {
}
impl MdBook {
pub fn configure_mdbook_common(cfg: &mut app::web::ServiceConfig) {
theme_static_files!(cfg, "/mdbook/static");
pub fn configure_service_for_common_resources(cfg: &mut app::web::ServiceConfig) {
configure_service_for_static_files!(cfg, "/mdbook/static", bundle_mdbook);
}
pub fn configure_mdbook_service(
pub fn configure_service_for_mdbook(
cfg: &mut app::web::ServiceConfig,
mdbook_path: &'static str,
mdbook_map: &'static BookMapResources,
mdbook_map: &'static HashMapResources,
) {
let path = mdbook_path.trim_end_matches('/');
cfg.service(
@ -46,7 +46,7 @@ impl MdBook {
async fn mdbook_page(
request: app::HttpRequest,
mdbook_path: &'static str,
mdbook_map: &'static BookMapResources,
mdbook_map: &'static HashMapResources,
) -> ResultPage<Markup, FatalError> {
let path_len = mdbook_path.len() + 1;
if let Some(content) = mdbook_map.get(&request.path()[path_len..]) {
@ -115,7 +115,7 @@ async fn mdbook_page(
async fn mdbook_resource(
request: app::HttpRequest,
mdbook_path: &'static str,
mdbook_map: &'static BookMapResources,
mdbook_map: &'static HashMapResources,
) -> app::HttpResponse {
let path_len = mdbook_path.len() + 1;
// From https://github.com/kilork/actix-web-static-files/blob/master/src/resource_files.rs, see

View file

@ -0,0 +1,21 @@
use std::path::Path;
pub fn except_mdbook_common_resources(p: &Path) -> bool {
match p.to_str() {
Some("ayu-highlight.css") => false,
Some("highlight.css") => false,
Some("tomorrow-niht.css") => false,
_ => {
if let Some(parent) = p.parent() {
match parent.to_str() {
Some("/css") => false,
Some("/FontAwesome") => false,
Some("/fonts") => false,
_ => true,
}
} else {
true
}
}
}
}