app: get rid of rollup, gzip the wasm

This commit is contained in:
Valentin Tolmer 2023-03-05 12:45:30 +01:00 committed by nitnelave
parent 2593606f16
commit 9e479d38fe
9 changed files with 50 additions and 41 deletions

View File

@ -6,20 +6,17 @@ ENV PATH="/opt/aarch64-linux-musl-cross/:/opt/aarch64-linux-musl-cross/bin/:/opt
### Install build deps x86_64 ### Install build deps x86_64
RUN apt update && \ 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 - && \ curl -fsSL https://deb.nodesource.com/setup_lts.x | bash - && \
apt update && \ apt update && \
apt install -y --no-install-recommends nodejs && \ apt install -y --no-install-recommends nodejs && \
apt clean && \ apt clean && \
rm -rf /var/lib/apt/lists/* && \ rm -rf /var/lib/apt/lists/*
npm install -g npm && \
npm install -g yarn && \
npm install -g pnpm
### Install build deps aarch64 build ### Install build deps aarch64 build
RUN dpkg --add-architecture arm64 && \ RUN dpkg --add-architecture arm64 && \
apt update && \ 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 && \ apt clean && \
rm -rf /var/lib/apt/lists/* && \ rm -rf /var/lib/apt/lists/* && \
rustup target add aarch64-unknown-linux-gnu rustup target add aarch64-unknown-linux-gnu
@ -27,7 +24,7 @@ RUN dpkg --add-architecture arm64 && \
### armhf deps ### armhf deps
RUN dpkg --add-architecture armhf && \ RUN dpkg --add-architecture armhf && \
apt update && \ 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 && \ apt clean && \
rm -rf /var/lib/apt/lists/* && \ rm -rf /var/lib/apt/lists/* && \
rustup target add armv7-unknown-linux-gnueabihf rustup target add armv7-unknown-linux-gnueabihf

View File

@ -8,6 +8,12 @@ members = [
default-members = ["server"] default-members = ["server"]
[profile.release]
lto = true
[profile.release.package.lldap_app]
opt-level = 's'
[patch.crates-io.opaque-ke] [patch.crates-io.opaque-ke]
git = 'https://github.com/nitnelave/opaque-ke/' git = 'https://github.com/nitnelave/opaque-ke/'
branch = 'zeroize_1.5' branch = 'zeroize_1.5'

View File

@ -11,7 +11,7 @@ RUN set -x \
--uid 10001 \ --uid 10001 \
app \ app \
# Install required packages # 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 USER app
WORKDIR /app WORKDIR /app
@ -19,7 +19,6 @@ WORKDIR /app
RUN set -x \ RUN set -x \
# Install build tools # Install build tools
&& RUSTFLAGS=-Ctarget-feature=-crt-static cargo install wasm-pack cargo-chef \ && RUSTFLAGS=-Ctarget-feature=-crt-static cargo install wasm-pack cargo-chef \
&& npm install rollup \
&& rustup target add wasm32-unknown-unknown && rustup target add wasm32-unknown-unknown
# Prepare the dependency list. # Prepare the dependency list.

View File

@ -6,22 +6,12 @@ then
>&2 echo '`wasm-pack` not found. Try running `cargo install wasm-pack`' >&2 echo '`wasm-pack` not found. Try running `cargo install wasm-pack`'
exit 1 exit 1
fi fi
if ! which gzip > /dev/null 2>&1
wasm-pack build --target web
ROLLUP_BIN=$(which rollup 2>/dev/null)
if [ -f ../node_modules/rollup/dist/bin/rollup ]
then then
ROLLUP_BIN=../node_modules/rollup/dist/bin/rollup >&2 echo '`gzip` not found.'
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`'
exit 1 exit 1
fi 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

View File

@ -4,7 +4,7 @@
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<title>LLDAP Administration</title> <title>LLDAP Administration</title>
<script src="/pkg/bundle.js" defer></script> <script src="/static/main.js" type="module" defer></script>
<link <link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css"
rel="preload stylesheet" rel="preload stylesheet"

View File

@ -4,7 +4,7 @@
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<title>LLDAP Administration</title> <title>LLDAP Administration</title>
<script src="/pkg/bundle.js" defer></script> <script src="/static/main.js" type="module" defer></script>
<link <link
href="/static/bootstrap.min.css" href="/static/bootstrap.min.css"
rel="preload stylesheet" rel="preload stylesheet"

View File

@ -1,16 +1,16 @@
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "bootstrap")] #[wasm_bindgen]
extern "C" { extern "C" {
#[wasm_bindgen] #[wasm_bindgen(js_namespace = bootstrap)]
pub type Modal; pub type Modal;
#[wasm_bindgen(constructor)] #[wasm_bindgen(constructor, js_namespace = bootstrap)]
pub fn new(e: web_sys::Element) -> Modal; pub fn new(e: web_sys::Element) -> Modal;
#[wasm_bindgen(method)] #[wasm_bindgen(method, js_namespace = bootstrap)]
pub fn show(this: &Modal); pub fn show(this: &Modal);
#[wasm_bindgen(method)] #[wasm_bindgen(method, js_namespace = bootstrap)]
pub fn hide(this: &Modal); pub fn hide(this: &Modal);
} }

View File

@ -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() { async function main() {
await init('/pkg/lldap_app_bg.wasm'); await init('/pkg/lldap_app_bg.wasm');
run_app(); run_app();

View File

@ -13,10 +13,10 @@ use crate::{
}, },
}; };
use actix_files::{Files, NamedFile}; use actix_files::{Files, NamedFile};
use actix_http::HttpServiceBuilder; use actix_http::{header, HttpServiceBuilder};
use actix_server::ServerBuilder; use actix_server::ServerBuilder;
use actix_service::map_config; 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 anyhow::{Context, Result};
use hmac::Hmac; use hmac::Hmac;
use sha2::Sha512; use sha2::Sha512;
@ -67,6 +67,16 @@ pub(crate) fn error_to_http_response(error: TcpError) -> HttpResponse {
.body(error.to_string()) .body(error.to_string())
} }
async fn wasm_handler() -> actix_web::Result<impl Responder> {
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<Backend>( fn http_config<Backend>(
cfg: &mut web::ServiceConfig, cfg: &mut web::ServiceConfig,
backend_handler: Backend, backend_handler: Backend,
@ -99,6 +109,13 @@ fn http_config<Backend>(
.wrap(auth_service::CookieToHeaderTranslatorFactory) .wrap(auth_service::CookieToHeaderTranslatorFactory)
.configure(super::graphql::api::configure_endpoint::<Backend>), .configure(super::graphql::api::configure_endpoint::<Backend>),
) )
.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. // Serve the /pkg path with the compiled WASM app.
.service(Files::new("/pkg", "./app/pkg")) .service(Files::new("/pkg", "./app/pkg"))
// Serve static files // Serve static files
@ -106,11 +123,7 @@ fn http_config<Backend>(
// Serve static fonts // Serve static fonts
.service(Files::new("/static/fonts", "./app/static/fonts")) .service(Files::new("/static/fonts", "./app/static/fonts"))
// Default to serve index.html for unknown routes, to support routing. // Default to serve index.html for unknown routes, to support routing.
.service( .default_service(web::route().guard(guard::Get()).to(index));
web::scope("/")
.route("", web::get().to(index)) // this is necessary because the below doesn't match a request for "/"
.route(".*", web::get().to(index)),
);
} }
pub(crate) struct AppState<Backend> { pub(crate) struct AppState<Backend> {
@ -157,6 +170,7 @@ where
.context("while getting the jwt blacklist")?; .context("while getting the jwt blacklist")?;
let server_url = config.http_url.clone(); let server_url = config.http_url.clone();
let mail_options = config.smtp_options.clone(); let mail_options = config.smtp_options.clone();
let verbose = config.verbose;
info!("Starting the API/web server on port {}", config.http_port); info!("Starting the API/web server on port {}", config.http_port);
server_builder server_builder
.bind( .bind(
@ -171,7 +185,10 @@ where
HttpServiceBuilder::default() HttpServiceBuilder::default()
.finish(map_config( .finish(map_config(
App::new() App::new()
.wrap(tracing_actix_web::TracingLogger::<CustomRootSpanBuilder>::new()) .wrap(actix_web::middleware::Condition::new(
verbose,
tracing_actix_web::TracingLogger::<CustomRootSpanBuilder>::new(),
))
.configure(move |cfg| { .configure(move |cfg| {
http_config( http_config(
cfg, cfg,