From 9e479d38fe602094171ba5a87ab688ce27f492fd Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Sun, 5 Mar 2023 12:45:30 +0100 Subject: [PATCH] app: get rid of rollup, gzip the wasm --- .github/workflows/Dockerfile.dev | 13 +++++-------- Cargo.toml | 6 ++++++ Dockerfile | 3 +-- app/build.sh | 20 +++++-------------- app/index.html | 2 +- app/index_local.html | 2 +- app/src/infra/modal.rs | 10 +++++----- app/{ => static}/main.js | 2 +- server/src/infra/tcp_server.rs | 33 ++++++++++++++++++++++++-------- 9 files changed, 50 insertions(+), 41 deletions(-) rename app/{ => static}/main.js (62%) diff --git a/.github/workflows/Dockerfile.dev b/.github/workflows/Dockerfile.dev index 7bca4e4..8e23543 100644 --- a/.github/workflows/Dockerfile.dev +++ b/.github/workflows/Dockerfile.dev @@ -6,20 +6,17 @@ ENV PATH="/opt/aarch64-linux-musl-cross/:/opt/aarch64-linux-musl-cross/bin/:/opt ### Install build deps x86_64 RUN apt update && \ - apt install -y --no-install-recommends curl git wget build-essential make perl pkg-config curl tar jq musl-tools && \ + apt install -y --no-install-recommends curl git wget build-essential make perl pkg-config curl tar jq musl-tools gzip && \ curl -fsSL https://deb.nodesource.com/setup_lts.x | bash - && \ apt update && \ apt install -y --no-install-recommends nodejs && \ apt clean && \ - rm -rf /var/lib/apt/lists/* && \ - npm install -g npm && \ - npm install -g yarn && \ - npm install -g pnpm + rm -rf /var/lib/apt/lists/* ### Install build deps aarch64 build RUN dpkg --add-architecture arm64 && \ apt update && \ - apt install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu libc6-arm64-cross libc6-dev-arm64-cross && \ + apt install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu libc6-arm64-cross libc6-dev-arm64-cross gzip && \ apt clean && \ rm -rf /var/lib/apt/lists/* && \ rustup target add aarch64-unknown-linux-gnu @@ -27,7 +24,7 @@ RUN dpkg --add-architecture arm64 && \ ### armhf deps RUN dpkg --add-architecture armhf && \ apt update && \ - apt install -y gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf libc6-armhf-cross libc6-dev-armhf-cross && \ + apt install -y gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf libc6-armhf-cross libc6-dev-armhf-cross gzip && \ apt clean && \ rm -rf /var/lib/apt/lists/* && \ rustup target add armv7-unknown-linux-gnueabihf @@ -43,6 +40,6 @@ RUN wget -c https://musl.cc/x86_64-linux-musl-cross.tgz && \ ### Add musl target RUN rustup target add x86_64-unknown-linux-musl && \ rustup target add aarch64-unknown-linux-musl - + CMD ["bash"] diff --git a/Cargo.toml b/Cargo.toml index f9096f4..2c6830c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,12 @@ members = [ default-members = ["server"] +[profile.release] +lto = true + +[profile.release.package.lldap_app] +opt-level = 's' + [patch.crates-io.opaque-ke] git = 'https://github.com/nitnelave/opaque-ke/' branch = 'zeroize_1.5' diff --git a/Dockerfile b/Dockerfile index c82aff3..4407e30 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,7 +11,7 @@ RUN set -x \ --uid 10001 \ app \ # Install required packages - && apk add npm openssl-dev musl-dev make perl curl + && apk add openssl-dev musl-dev make perl curl gzip USER app WORKDIR /app @@ -19,7 +19,6 @@ WORKDIR /app RUN set -x \ # Install build tools && RUSTFLAGS=-Ctarget-feature=-crt-static cargo install wasm-pack cargo-chef \ - && npm install rollup \ && rustup target add wasm32-unknown-unknown # Prepare the dependency list. diff --git a/app/build.sh b/app/build.sh index 10ef9f3..dde5d3f 100755 --- a/app/build.sh +++ b/app/build.sh @@ -6,22 +6,12 @@ then >&2 echo '`wasm-pack` not found. Try running `cargo install wasm-pack`' exit 1 fi - -wasm-pack build --target web - -ROLLUP_BIN=$(which rollup 2>/dev/null) -if [ -f ../node_modules/rollup/dist/bin/rollup ] +if ! which gzip > /dev/null 2>&1 then - ROLLUP_BIN=../node_modules/rollup/dist/bin/rollup -elif [ -f node_modules/rollup/dist/bin/rollup ] -then - ROLLUP_BIN=node_modules/rollup/dist/bin/rollup -fi - -if [ -z "$ROLLUP_BIN" ] -then - >&2 echo '`rollup` not found. Try running `npm install rollup`' + >&2 echo '`gzip` not found.' exit 1 fi -$ROLLUP_BIN ./main.js --format iife --file ./pkg/bundle.js --globals bootstrap:bootstrap +wasm-pack build --target web --release + +gzip -9 -f pkg/lldap_app_bg.wasm diff --git a/app/index.html b/app/index.html index ea3b6fc..cbe820c 100644 --- a/app/index.html +++ b/app/index.html @@ -4,7 +4,7 @@ LLDAP Administration - + LLDAP Administration - + Modal; - #[wasm_bindgen(method)] + #[wasm_bindgen(method, js_namespace = bootstrap)] pub fn show(this: &Modal); - #[wasm_bindgen(method)] + #[wasm_bindgen(method, js_namespace = bootstrap)] pub fn hide(this: &Modal); } diff --git a/app/main.js b/app/static/main.js similarity index 62% rename from app/main.js rename to app/static/main.js index 8ab5e77..4dc13f5 100644 --- a/app/main.js +++ b/app/static/main.js @@ -1,4 +1,4 @@ -import init, { run_app } from './pkg/lldap_app.js'; +import init, { run_app } from '/pkg/lldap_app.js'; async function main() { await init('/pkg/lldap_app_bg.wasm'); run_app(); diff --git a/server/src/infra/tcp_server.rs b/server/src/infra/tcp_server.rs index 00d131e..c20358d 100644 --- a/server/src/infra/tcp_server.rs +++ b/server/src/infra/tcp_server.rs @@ -13,10 +13,10 @@ use crate::{ }, }; use actix_files::{Files, NamedFile}; -use actix_http::HttpServiceBuilder; +use actix_http::{header, HttpServiceBuilder}; use actix_server::ServerBuilder; use actix_service::map_config; -use actix_web::{dev::AppConfig, web, App, HttpResponse}; +use actix_web::{dev::AppConfig, guard, middleware, web, App, HttpResponse, Responder}; use anyhow::{Context, Result}; use hmac::Hmac; use sha2::Sha512; @@ -67,6 +67,16 @@ pub(crate) fn error_to_http_response(error: TcpError) -> HttpResponse { .body(error.to_string()) } +async fn wasm_handler() -> actix_web::Result { + Ok( + actix_files::NamedFile::open_async("./app/pkg/lldap_app_bg.wasm.gz") + .await? + .customize() + .insert_header(header::ContentEncoding::Gzip) + .insert_header((header::CONTENT_TYPE, "application/wasm")), + ) +} + fn http_config( cfg: &mut web::ServiceConfig, backend_handler: Backend, @@ -99,6 +109,13 @@ fn http_config( .wrap(auth_service::CookieToHeaderTranslatorFactory) .configure(super::graphql::api::configure_endpoint::), ) + .service( + web::resource("/pkg/lldap_app_bg.wasm").route( + web::route() + .wrap(middleware::Compress::default()) + .to(wasm_handler), + ), + ) // Serve the /pkg path with the compiled WASM app. .service(Files::new("/pkg", "./app/pkg")) // Serve static files @@ -106,11 +123,7 @@ fn http_config( // Serve static fonts .service(Files::new("/static/fonts", "./app/static/fonts")) // Default to serve index.html for unknown routes, to support routing. - .service( - web::scope("/") - .route("", web::get().to(index)) // this is necessary because the below doesn't match a request for "/" - .route(".*", web::get().to(index)), - ); + .default_service(web::route().guard(guard::Get()).to(index)); } pub(crate) struct AppState { @@ -157,6 +170,7 @@ where .context("while getting the jwt blacklist")?; let server_url = config.http_url.clone(); let mail_options = config.smtp_options.clone(); + let verbose = config.verbose; info!("Starting the API/web server on port {}", config.http_port); server_builder .bind( @@ -171,7 +185,10 @@ where HttpServiceBuilder::default() .finish(map_config( App::new() - .wrap(tracing_actix_web::TracingLogger::::new()) + .wrap(actix_web::middleware::Condition::new( + verbose, + tracing_actix_web::TracingLogger::::new(), + )) .configure(move |cfg| { http_config( cfg,