Skip to content

Commit

Permalink
fix: yew-rs wasm deploy v1
Browse files Browse the repository at this point in the history
add: crate-type on Cargo.toml as prerequisite for wasm-pack
feat: multi-stage build with final artifacts for node
todo: go back one stage and serve the spa with trunk instead
feat: nginx.conf basic configuration for wasm mimetypes and
CORS handling by connecting to the server api; port defined on
compose.yml
  • Loading branch information
deomorxsy committed Apr 23, 2024
1 parent f3edf99 commit 07da604
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 25 deletions.
7 changes: 7 additions & 0 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@ edition = "2021"
keywords = ["yew", "trunk"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html


[dependencies]
#yew = "0.21.0"
# this is the development version of Yew
yew = { git = "https://github.com/yewstack/yew/", features = ["csr"] }
yew-router = "0.18.0"

[lib]
name = "client"
path = "src/main.rs"
crate-type = ["rlib", "cdylib"]


53 changes: 31 additions & 22 deletions client/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,40 +1,49 @@
FROM rust:1.77.1-alpine3.18 as build
# This is the first stage. This image is used to compile the Rust application.
#FROM rust:bookworm as builder
FROM rust:1.77.1-alpine3.18 as builder
WORKDIR /app

#
# build stage
#
WORKDIR /usr/src/mcl/
COPY . .
COPY .env.docker .env

#
#RUN apt-update && apt-get --yes install pkg-config libssl-dev \
#cargo install trunk --version 0.16.0 \
RUN apk update && apk add --upgrade "pkgconf" "openssl-dev" && \
apk add --no-cache musl-dev && \
wget -qO- https://github.com/trunk-rs/trunk/releases/download/v0.19.2/trunk-x86_64-unknown-linux-musl.tar.gz | tar -xzf- && \
rustup target add wasm32-unknown-unknown && \
rustup component add rustfmt && \
rustup component add clippy-preview && \
cargo install --path .
# Install the package in the current directory
CMD ["./target/release/app"]

#
# deploy stage
#
#FROM gcr.io/distroless/cc-debian11 as relay
#FROM debian:bookworm-slim
FROM alpine:3.18 as relay
#FROM rust:1.77.1-alpine3.18 as relay

#COPY --from=builder /usr/local/cargo/bin/app /usr/local/bin/app
WORKDIR /app

# will work with others, but not wasm-bindgen. Use trunk or others
COPY --from=builder /usr/local/cargo/bin/client /usr/local/bin/app
COPY --from=builder ["/app/index.html", "/app/Cargo.toml", "/app/Cargo.lock", "/app/dist/", "/app/trunk", "/app/.env", "/app/src/", "."]
COPY --from=builder /app/src/ ./src/

RUN apk add --no-cache musl-dev wasm-pack && \
cargo update -p bumpalo@3.15.4 --precise 3.12.0 && \
wasm-pack build --target nodejs

ARG ARCH=aarch64
EXPOSE 8081

#WORKDIR /usr/src/mcl/
CMD ["./trunk", "serve", "--address", "0.0.0.0", "--port", "8081"]
#CMD ["wasm-pack", "build"]
#works
##./trunk serve --address 0.0.0.0 --port 8080
ENTRYPOINT ["/bin/sh"]

#COPY --from=build /usr/local/cargo/bin/mcl /usr/local/bin/mcl
COPY --from=build /usr/local/cargo/bin/client /usr/local/bin/mcl
#COPY --from=build /usr/src/mcl/.env /usr/local/bin/.env
COPY --from=build /usr/src/mcl/.env /.env
FROM node:21-alpine3.18

WORKDIR /app/
COPY --from=relay /app/pkg/ /app/pkg/

CMD ["mcl"]
#CMD ["cargo", "run"]
ENTRYPOINT [ "/bin/sh" ]
EXPOSE 8081

ENTRYPOINT ["/bin/sh"]
7 changes: 4 additions & 3 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,18 @@ services:
client:
#image: mcl_slimRust:v01
build:
context: ./client
#context: ./client
dockerfile: ./client/Dockerfile.another
target: relay # relay release/artifact app stage
tags:
- "yew_mcl:01"
command: bash -c "cd app/yew-ui && trunk serve --address 0.0.0.0 --port ${TRUNK_SERVE_PORT:-80}"
command: /bin/sh -c "./trunk serve --address 0.0.0.0 --port ${TRUNK_SERVE_PORT:-8081}"
environment:
- SPRING_PORT=${SPRING_PORT:-8080}
- TRUNK_SERVE_PORT=${TRUNK_SERVE_PORT:-80}
- TRUNK_SERVE_HOST=localhost
ports:
- "${TRUNK_SERVE_PORT:-8081}:${TRUNK_SERVE_PORT:-80}"
- "${TRUNK_SERVE_PORT:-8081}:${TRUNK_SERVE_PORT:-8081}"
volumes:
- type: bind
source: ../
Expand Down
29 changes: 29 additions & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
include /etc/nginx/mime.types;

server {
# ipv4
listen 443 ssl;
# ipv6
listen [::]:443 ssl;
server_name ${gh_user}.github.io/${repo_subdir};

types {
application/wasm wasm;
}

# yew-rs frontend
location / {
root ./client/dist/index.html;
try_files $uri $uri/ ./client/index.html;
include /etc/nginx/mime.types;
default_type application/octet-stream;
}
# SpringBoot backend
location /api {
proxy_pass http://localhost:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;

}
}

0 comments on commit 07da604

Please sign in to comment.