diff --git a/.github/workflows/push-build.yml b/.github/workflows/push-build.yml index fc9e93da..bb266a17 100644 --- a/.github/workflows/push-build.yml +++ b/.github/workflows/push-build.yml @@ -9,13 +9,6 @@ jobs: - uses: actions/checkout@v2 with: fetch-depth: 0 - - name: Install Rust - uses: actions-rs/toolchain@v1 - with: - profile: minimal - toolchain: stable - override: true - components: rustfmt, clippy - name: Lint (rustfmt) working-directory: src/backend run: cargo fmt -- --check @@ -47,20 +40,20 @@ jobs: - name: Build frontend working-directory: src/frontend run: yarn build - - name: Copy "dist" folder - run: cp -r src/frontend/dist/. src/backend/dist/ + - name: Compress frontend files + run: make compress - name: Build x86_64 working-directory: src/backend - run: cross build --release --target x86_64-unknown-linux-musl + run: cross build --release --target x86_64-unknown-linux-musl --features compression - name: Build armv6l working-directory: src/backend - run: cross build --release --target arm-unknown-linux-musleabihf + run: cross build --release --target arm-unknown-linux-musleabihf --features compression - name: Build armv7l working-directory: src/backend - run: cross build --release --target armv7-unknown-linux-musleabihf + run: cross build --release --target armv7-unknown-linux-musleabihf --features compression - name: Build aarch64 working-directory: src/backend - run: cross build --release --target aarch64-unknown-linux-musl + run: cross build --release --target aarch64-unknown-linux-musl --features compression - uses: actions/upload-artifact@v2 with: name: dietpi-dashboard-x86_64 diff --git a/Makefile b/Makefile index ffa36f7c..1bac9baf 100644 --- a/Makefile +++ b/Makefile @@ -1,18 +1,30 @@ -default: yarn publiccopy fmt +build: yarn rustbuild +rustbuild: distcopy fmt +ifdef TARGET + cd src/backend; cargo build --target $(TARGET) + mv src/backend/target/$(TARGET)/debug/dietpi-dashboard . +else cd src/backend; cargo build + mv src/backend/target/debug/dietpi-dashboard . +endif - $(MAKE) publicdelete - - mv src/backend/target/debug/dietpi-dashboard ./dietpi-dashboard - -rust: publiccopy fmt + rm -r src/backend/dist - cd src/backend; cargo build +release: yarn compress +ifdef TARGET + cd src/backend; cargo build --target $(TARGET) --release --features compression + mv src/backend/target/$(TARGET)/release/dietpi-dashboard . +else + cd src/backend; cargo build --release --features compression + mv src/backend/target/release/dietpi-dashboard . +endif - $(MAKE) publicdelete + rm -r src/backend/dist - mv src/backend/target/debug/dietpi-dashboard ./dietpi-dashboard +# There may be a better, more 'make'y way of doing this, but find works for now +compress: distcopy + find src/backend/dist ! -name '*.png' -type f -exec gzip -9 {} \; -exec mv {}.gz {} \; yarn: cd src/frontend; yarn build @@ -23,12 +35,6 @@ else rm -f src/backend/target/debug/deps/dietpi_dashboard-* endif -publiccopy: - cp -r src/frontend/dist src/backend - -publicdelete: - rm -r src/backend/dist - fmt: cd src/backend; cargo fmt ifdef TARGET @@ -37,10 +43,5 @@ else cd src/backend; cargo clippy endif -rustdev: publiccopy fmt - cd src/backend; cargo build --target $(TARGET) - mv src/backend/target/$(TARGET)/debug/dietpi-dashboard ./dietpi-dashboard - - $(MAKE) publicdelete - -dev: yarn rustdev +distcopy: + cp -r src/frontend/dist src/backend diff --git a/src/backend/Cargo.lock b/src/backend/Cargo.lock index 16c95515..f424d5ee 100644 --- a/src/backend/Cargo.lock +++ b/src/backend/Cargo.lock @@ -8,41 +8,12 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" -[[package]] -name = "alloc-no-stdlib" -version = "2.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "35ef4730490ad1c4eae5c4325b2a95f521d023e5c885853ff7aca0a6a1631db3" - -[[package]] -name = "alloc-stdlib" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "697ed7edc0f1711de49ce108c541623a0af97c6c60b2f6e2b65229847ac843c2" -dependencies = [ - "alloc-no-stdlib", -] - [[package]] name = "anyhow" version = "1.0.57" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08f9b8508dccb7687a1d6c4ce66b2b0ecef467c94667de27d8d7fe1f8d2a9cdc" -[[package]] -name = "async-compression" -version = "0.3.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5443ccbb270374a2b1055fc72da40e1f237809cd6bb0e97e66d264cd138473a6" -dependencies = [ - "brotli", - "flate2", - "futures-core", - "memchr", - "pin-project-lite", - "tokio", -] - [[package]] name = "atomic" version = "0.5.1" @@ -90,27 +61,6 @@ dependencies = [ "generic-array", ] -[[package]] -name = "brotli" -version = "3.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "71cb90ade945043d3d53597b2fc359bb063db8ade2bcffe7997351d0756e9d50" -dependencies = [ - "alloc-no-stdlib", - "alloc-stdlib", - "brotli-decompressor", -] - -[[package]] -name = "brotli-decompressor" -version = "2.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59ad2d4653bf5ca36ae797b1f4bb4dbddb60ce49ca4aed8a2ce4829f60425b80" -dependencies = [ - "alloc-no-stdlib", - "alloc-stdlib", -] - [[package]] name = "bumpalo" version = "3.8.0" @@ -261,13 +211,11 @@ dependencies = [ [[package]] name = "flate2" -version = "1.0.22" +version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e6988e897c1c9c485f43b47a529cef42fde0547f9d8d41a7062518f1d8fc53f" +checksum = "f82b0f4c27ad9f8bfd1f3208d882da2b09c301bc1c828fd3a00d0216d2fbbff6" dependencies = [ - "cfg-if", "crc32fast", - "libc", "miniz_oxide", ] @@ -684,12 +632,11 @@ dependencies = [ [[package]] name = "miniz_oxide" -version = "0.4.4" +version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b" +checksum = "6f5c75688da582b8ffc1f1799e9db273f32133c49e048f614d22ec3256773ccc" dependencies = [ "adler", - "autocfg", ] [[package]] @@ -1413,7 +1360,6 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3cef4e1e9114a4b7f1ac799f16ce71c14de5778500c5450ec6b7b920c55b587e" dependencies = [ - "async-compression", "bytes", "futures-channel", "futures-util", diff --git a/src/backend/Cargo.toml b/src/backend/Cargo.toml index 127b9e1a..d25f0de7 100644 --- a/src/backend/Cargo.toml +++ b/src/backend/Cargo.toml @@ -28,7 +28,8 @@ anyhow = "1.0.57" [features] default = ["frontend"] -frontend = ["include_dir", "warp/compression"] +frontend = ["include_dir"] +compression = ["frontend"] [profile.dev] debug = 1 diff --git a/src/backend/src/main.rs b/src/backend/src/main.rs index b364f572..7a74b37b 100644 --- a/src/backend/src/main.rs +++ b/src/backend/src/main.rs @@ -42,6 +42,8 @@ fn main() -> anyhow::Result<()> { headers.insert("X-Permitted-Cross-Domain_Policies", header::HeaderValue::from_static("none")); headers.insert(header::REFERRER_POLICY, header::HeaderValue::from_static("no-referrer")); headers.insert("Content-Security-Policy", header::HeaderValue::from_static("default-src 'self'; font-src 'self'; img-src 'self' blob:; script-src 'self'; style-src 'unsafe-inline' 'self'; connect-src * ws:;")); + #[cfg(feature = "compression")] + headers.insert(header::CONTENT_ENCODING, header::HeaderValue::from_static("gzip")); } #[cfg(feature = "frontend")] @@ -58,7 +60,8 @@ fn main() -> anyhow::Result<()> { .and(warp::path::param()) .map(|path: String| { let ext = path.rsplit('.').next().unwrap_or("plain"); - warp::reply::with_header( + #[allow(unused_mut)] // Mute warning, variable is mut because it's used with the compression feature + let mut reply = warp::reply::with_header( match DIR.get_file(format!("assets/{}", path)) { Some(file) => file.contents(), None => { @@ -66,7 +69,7 @@ fn main() -> anyhow::Result<()> { &[] } }, - "content-type", + header::CONTENT_TYPE, if ext == "js" { "text/javascript".to_string() } else if ext == "svg" { @@ -76,7 +79,14 @@ fn main() -> anyhow::Result<()> { } else { format!("text/{}", ext) }, - ) + ).into_response(); + + #[cfg(feature = "compression")] + if ext != "png" { + reply.headers_mut().insert(header::CONTENT_ENCODING, header::HeaderValue::from_static("gzip")); + }; + + reply }); let login_route = warp::path("login") @@ -137,16 +147,14 @@ fn main() -> anyhow::Result<()> { #[cfg(feature = "frontend")] let main_route = warp::any().map(|| { - let file_bytes = handle_error!(DIR.get_file("index.html").context("Couldn't get main HTML file"), return warp::reply::with_status("Couldn't get main HTML file", warp::http::StatusCode::INTERNAL_SERVER_ERROR).into_response()); - let file = handle_error!(file_bytes.contents_utf8().context("Invalid main HTML file"), return warp::reply::with_status("Invalid main HTML file", warp::http::StatusCode::INTERNAL_SERVER_ERROR).into_response()); + let file = handle_error!(DIR.get_file("index.html").context("Couldn't get main HTML file"), return warp::reply::with_status("Couldn't get main HTML file", warp::http::StatusCode::INTERNAL_SERVER_ERROR).into_response()).contents(); warp::reply::html(file).into_response() }).with(warp::reply::with::headers(headers)); #[cfg(feature = "frontend")] let page_routes = favicon_route .or(assets_route) - .or(main_route) - .with(warp::compression::gzip()); + .or(main_route); let socket_routes = terminal_route.or(file_route).or(socket_route);