diff --git a/.gitignore b/.gitignore index 56080a1..5541cbc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target /config +/dist diff --git a/Cargo.lock b/Cargo.lock index 09b8bc1..a43a709 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -19,6 +19,29 @@ dependencies = [ "tokio-util", ] +[[package]] +name = "actix-files" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d832782fac6ca7369a70c9ee9a20554623c5e51c76e190ad151780ebea1cf689" +dependencies = [ + "actix-http", + "actix-service", + "actix-utils", + "actix-web", + "askama_escape", + "bitflags", + "bytes", + "derive_more", + "futures-core", + "http-range", + "log", + "mime", + "mime_guess", + "percent-encoding", + "pin-project-lite", +] + [[package]] name = "actix-http" version = "3.3.0" @@ -395,6 +418,17 @@ dependencies = [ "typenum", ] +[[package]] +name = "css-minify" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "874c6e2d19f8d4a285083b11a3241bfbe01ac3ed85f26e1e6b34888d960552bd" +dependencies = [ + "derive_more", + "indexmap", + "nom", +] + [[package]] name = "derive_more" version = "0.99.17" @@ -431,8 +465,11 @@ dependencies = [ name = "ewp" version = "0.1.0" dependencies = [ + "actix-files", "actix-web", "askama", + "glob", + "minify-html", "serde", "toml", ] @@ -513,6 +550,12 @@ dependencies = [ "wasi", ] +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + [[package]] name = "h2" version = "0.3.15" @@ -558,6 +601,12 @@ dependencies = [ "itoa", ] +[[package]] +name = "http-range" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21dec9db110f5f872ed9699c3ecf50cf16f423502706ba5c72462e28d3157573" + [[package]] name = "httparse" version = "1.8.0" @@ -617,6 +666,12 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4345964bb142484797b161f473a503a434de77149dd8c7427788c6e13379388" +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.139" @@ -682,6 +737,30 @@ dependencies = [ "unicase", ] +[[package]] +name = "minify-html" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7754d4669873379ea6a8a5b56e406eb83de713af8a791517ef35a0c832b1e7d5" +dependencies = [ + "aho-corasick", + "css-minify", + "lazy_static", + "memchr", + "minify-js", + "rustc-hash", +] + +[[package]] +name = "minify-js" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c300f90ba1138b5c5daf5d9441dc9bdc67b808aac22cf638362a2647bc213be4" +dependencies = [ + "lazy_static", + "parse-js", +] + [[package]] name = "minimal-lexical" version = "0.2.1" @@ -767,6 +846,17 @@ dependencies = [ "windows-sys 0.45.0", ] +[[package]] +name = "parse-js" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30534759e6ad87aa144c396544747e1c25b1020bd133356fd758c8facec764e5" +dependencies = [ + "aho-corasick", + "lazy_static", + "memchr", +] + [[package]] name = "paste" version = "1.0.11" @@ -877,6 +967,12 @@ version = "0.6.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" +[[package]] +name = "rustc-hash" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" + [[package]] name = "rustc_version" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 025ee23..a09e983 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,9 @@ publish = false [dependencies] actix-web = "4" +actix-files = "0.6" askama = "0.11.1" toml = "0.5.10" serde = { version = "1.0.152", features = ["derive"] } +minify-html = "0.10.8" +glob = "0.3.1" diff --git a/src/main.rs b/src/main.rs index 4d1632c..bd950dc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,13 @@ +use std::{ + fs::{self, File}, + io::{self, Write}, + path::Path, +}; + +use actix_files::Files; use actix_web::{web, App, HttpServer}; +use glob::glob; +use minify_html::{minify, Cfg}; mod config; #[path = "routes/index.rs"] @@ -11,16 +20,41 @@ mod not_found; mod agreements; #[actix_web::main] -async fn main() -> std::io::Result<()> { - let config = config::get_config("config/config.toml"); +async fn main() -> io::Result<()> { + let config = config::get_config("/config/config.toml"); let addr = ("127.0.0.1", config.port); println!("Listening to {}://{}:{}", config.scheme, addr.0, addr.1); + let static_folder = "static"; + let dist_folder = "dist"; + + // The static folder is minimized only in release mode + let folder = if !cfg!(debug_assertions) { + format!("{static_folder}/") + } else { + let cfg = Cfg::new(); + + for entry in glob(&format!("{static_folder}/**/*.*")).unwrap() { + let path = entry.unwrap(); + let data = fs::read_to_string(&path).unwrap(); + let minified = minify(data.as_bytes(), &cfg); + + let path_with_dist = path.to_string_lossy().replace(static_folder, dist_folder); + let new_path = Path::new(&path_with_dist); + fs::create_dir_all(new_path.parent().unwrap()).unwrap(); + let mut file = File::create(path_with_dist)?; + file.write_all(&minified)?; + } + + format!("{dist_folder}/") + }; + HttpServer::new(move || { App::new() .app_data(web::Data::new(config.clone())) .service(index::page) + .service(Files::new("/", &folder)) .service(agreements::security) .service(agreements::humans) .service(agreements::robots)