add static files support with minifization on release #2
This commit is contained in:
parent
5c6e3e6941
commit
830d61f28c
4 changed files with 136 additions and 2 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
||||||
/target
|
/target
|
||||||
/config
|
/config
|
||||||
|
/dist
|
||||||
|
|
96
Cargo.lock
generated
96
Cargo.lock
generated
|
@ -19,6 +19,29 @@ dependencies = [
|
||||||
"tokio-util",
|
"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]]
|
[[package]]
|
||||||
name = "actix-http"
|
name = "actix-http"
|
||||||
version = "3.3.0"
|
version = "3.3.0"
|
||||||
|
@ -395,6 +418,17 @@ dependencies = [
|
||||||
"typenum",
|
"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]]
|
[[package]]
|
||||||
name = "derive_more"
|
name = "derive_more"
|
||||||
version = "0.99.17"
|
version = "0.99.17"
|
||||||
|
@ -431,8 +465,11 @@ dependencies = [
|
||||||
name = "ewp"
|
name = "ewp"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"actix-files",
|
||||||
"actix-web",
|
"actix-web",
|
||||||
"askama",
|
"askama",
|
||||||
|
"glob",
|
||||||
|
"minify-html",
|
||||||
"serde",
|
"serde",
|
||||||
"toml",
|
"toml",
|
||||||
]
|
]
|
||||||
|
@ -513,6 +550,12 @@ dependencies = [
|
||||||
"wasi",
|
"wasi",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "glob"
|
||||||
|
version = "0.3.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "h2"
|
name = "h2"
|
||||||
version = "0.3.15"
|
version = "0.3.15"
|
||||||
|
@ -558,6 +601,12 @@ dependencies = [
|
||||||
"itoa",
|
"itoa",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "http-range"
|
||||||
|
version = "0.1.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "21dec9db110f5f872ed9699c3ecf50cf16f423502706ba5c72462e28d3157573"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "httparse"
|
name = "httparse"
|
||||||
version = "1.8.0"
|
version = "1.8.0"
|
||||||
|
@ -617,6 +666,12 @@ version = "0.3.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d4345964bb142484797b161f473a503a434de77149dd8c7427788c6e13379388"
|
checksum = "d4345964bb142484797b161f473a503a434de77149dd8c7427788c6e13379388"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "lazy_static"
|
||||||
|
version = "1.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.139"
|
version = "0.2.139"
|
||||||
|
@ -682,6 +737,30 @@ dependencies = [
|
||||||
"unicase",
|
"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]]
|
[[package]]
|
||||||
name = "minimal-lexical"
|
name = "minimal-lexical"
|
||||||
version = "0.2.1"
|
version = "0.2.1"
|
||||||
|
@ -767,6 +846,17 @@ dependencies = [
|
||||||
"windows-sys 0.45.0",
|
"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]]
|
[[package]]
|
||||||
name = "paste"
|
name = "paste"
|
||||||
version = "1.0.11"
|
version = "1.0.11"
|
||||||
|
@ -877,6 +967,12 @@ version = "0.6.28"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848"
|
checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rustc-hash"
|
||||||
|
version = "1.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rustc_version"
|
name = "rustc_version"
|
||||||
version = "0.4.0"
|
version = "0.4.0"
|
||||||
|
|
|
@ -10,6 +10,9 @@ publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "4"
|
actix-web = "4"
|
||||||
|
actix-files = "0.6"
|
||||||
askama = "0.11.1"
|
askama = "0.11.1"
|
||||||
toml = "0.5.10"
|
toml = "0.5.10"
|
||||||
serde = { version = "1.0.152", features = ["derive"] }
|
serde = { version = "1.0.152", features = ["derive"] }
|
||||||
|
minify-html = "0.10.8"
|
||||||
|
glob = "0.3.1"
|
||||||
|
|
38
src/main.rs
38
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 actix_web::{web, App, HttpServer};
|
||||||
|
use glob::glob;
|
||||||
|
use minify_html::{minify, Cfg};
|
||||||
mod config;
|
mod config;
|
||||||
|
|
||||||
#[path = "routes/index.rs"]
|
#[path = "routes/index.rs"]
|
||||||
|
@ -11,16 +20,41 @@ mod not_found;
|
||||||
mod agreements;
|
mod agreements;
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> io::Result<()> {
|
||||||
let config = config::get_config("config/config.toml");
|
let config = config::get_config("/config/config.toml");
|
||||||
|
|
||||||
let addr = ("127.0.0.1", config.port);
|
let addr = ("127.0.0.1", config.port);
|
||||||
println!("Listening to {}://{}:{}", config.scheme, addr.0, addr.1);
|
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 || {
|
HttpServer::new(move || {
|
||||||
App::new()
|
App::new()
|
||||||
.app_data(web::Data::new(config.clone()))
|
.app_data(web::Data::new(config.clone()))
|
||||||
.service(index::page)
|
.service(index::page)
|
||||||
|
.service(Files::new("/", &folder))
|
||||||
.service(agreements::security)
|
.service(agreements::security)
|
||||||
.service(agreements::humans)
|
.service(agreements::humans)
|
||||||
.service(agreements::robots)
|
.service(agreements::robots)
|
||||||
|
|
Loading…
Reference in a new issue