diff --git a/Dockerfile b/Dockerfile index 67456ed..fa04475 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,26 +1,16 @@ -# Build frontend -FROM node:20-alpine AS frontend-build -WORKDIR /app -COPY frontend/package.json frontend/package-lock.json ./ -RUN npm ci -COPY frontend/ . -RUN npm run build - -# Build Rust backend -FROM rust:1.75 AS rust-builder +# Build stage +FROM rust:1.80-slim AS builder WORKDIR /app COPY Cargo.toml Cargo.lock ./ -COPY src/ ./src/ +RUN mkdir src && echo "fn main() {}" > src/main.rs +RUN cargo build --release +COPY src ./src RUN cargo build --release -RUN cp target/release/hello-web /app/hello-web -# Final image +# Production stage FROM debian:bookworm-slim -RUN apt-get update && apt-get install -y --no-install-recommends \ - ca-certificates \ - && rm -rf /var/lib/apt/lists/* +RUN apt-get update && apt-get install -y ca-certificates && rm -rf /var/lib/apt/lists/* WORKDIR /app -COPY --from=rust-builder /app/hello-web /app/hello-web -COPY --from=frontend-build /app/dist /app/frontend/dist +COPY --from=builder /app/target/release/hello-web /app/hello-web EXPOSE 3000 CMD ["/app/hello-web"] diff --git a/frontend/Dockerfile b/frontend/Dockerfile new file mode 100644 index 0000000..62ae9d3 --- /dev/null +++ b/frontend/Dockerfile @@ -0,0 +1,14 @@ +# Build stage +FROM node:20-alpine AS build +WORKDIR /app +COPY package.json package-lock.json* ./ +RUN npm ci +COPY . . +RUN npm run build + +# Production stage +FROM nginx:alpine +COPY --from=build /app/dist /usr/share/nginx/html +COPY nginx.conf /etc/nginx/conf.d/default.conf +EXPOSE 80 +CMD ["nginx", "-g", "daemon off;"] diff --git a/frontend/nginx.conf b/frontend/nginx.conf new file mode 100644 index 0000000..981548a --- /dev/null +++ b/frontend/nginx.conf @@ -0,0 +1,21 @@ +server { + listen 80; + server_name localhost; + root /usr/share/nginx/html; + index index.html; + + location / { + try_files $uri $uri/ /index.html; + } + + location /api/ { + proxy_pass http://hello-web:80/; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + } + + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/share/nginx/html; + } +} diff --git a/src/main.rs b/src/main.rs index 558e2b8..eaaff4f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,8 +7,9 @@ use axum::{ }; use serde::{Deserialize, Serialize}; use std::collections::HashMap; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use tokio::sync::RwLock; +use tower_http::services::ServeDir; use tower_http::trace::TraceLayer; use tracing::{info, instrument};