# Build context: app/
# Example:
#   docker build -f Dockerfile -t iotlcm-app-web:latest .

# 基础镜像仓库。默认使用 Docker Hub 官方镜像；内网环境可替换为自己的仓库：
#   docker build --build-arg BASE_REGISTRY=harbor.example.com/library ...
ARG BASE_REGISTRY=docker.io/library

FROM ${BASE_REGISTRY}/node:24-alpine AS builder

WORKDIR /build/app

COPY package.json pnpm-lock.yaml .npmrc ./
COPY patches ./patches

RUN npm config set registry https://registry.npmmirror.com/ && \
    npm install -g pnpm@10.10.0 --registry=https://registry.npmmirror.com/ && \
    pnpm config set registry https://registry.npmmirror.com/ && \
    pnpm install --frozen-lockfile --ignore-scripts

COPY index.html vite.config.ts manifest.config.ts pages.config.ts tsconfig.json ./
COPY uno.config.ts wot-ui-resolver.ts openapi-ts-request.config.ts ./
COPY folder-alias.json favicon.ico ./
COPY env ./env
COPY public ./public
COPY scripts ./scripts
COPY src ./src
COPY vite-plugins ./vite-plugins

ARG VITE_SERVER_BASEURL=/app/api/v1
ARG VITE_MP_SERVER_BASEURL=/app/api/v1
ARG VITE_APP_PUBLIC_BASE=/app/

RUN printf '%s\n' \
    "VITE_SERVER_BASEURL='${VITE_SERVER_BASEURL}'" \
    "VITE_MP_SERVER_BASEURL='${VITE_MP_SERVER_BASEURL}'" \
    "VITE_APP_PUBLIC_BASE='${VITE_APP_PUBLIC_BASE}'" \
    > env/.env.production.local

RUN pnpm init-baseFiles
RUN CI=true pnpm build:h5:prod

FROM ${BASE_REGISTRY}/nginx:1.30-alpine AS runtime

COPY --from=builder /build/app/dist/build/h5 /usr/share/nginx/html

RUN printf '%s\n' \
    'server {' \
    '    listen 80;' \
    '    server_name _;' \
    '    root /usr/share/nginx/html;' \
    '    index index.html;' \
    '    resolver 127.0.0.11 ipv6=off valid=30s;' \
    '    set $app_backend http://iotlcm-app-server:8010;' \
    '' \
    '    client_max_body_size 200m;' \
    '' \
    '    location = /app {' \
    '        return 301 /app/;' \
    '    }' \
    '' \
    '    location ^~ /app/api/ {' \
    '        rewrite ^/app/(.*)$ /$1 break;' \
    '        proxy_http_version 1.1;' \
    '        proxy_set_header Host $host;' \
    '        proxy_set_header X-Real-IP $remote_addr;' \
    '        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' \
    '        proxy_set_header X-Forwarded-Proto $scheme;' \
    '        proxy_pass $app_backend;' \
    '    }' \
    '' \
    '    location ^~ /app/upload/ {' \
    '        rewrite ^/app/(.*)$ /$1 break;' \
    '        proxy_http_version 1.1;' \
    '        proxy_set_header Host $host;' \
    '        proxy_set_header X-Real-IP $remote_addr;' \
    '        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' \
    '        proxy_set_header X-Forwarded-Proto $scheme;' \
    '        proxy_pass $app_backend;' \
    '    }' \
    '' \
    '    location ^~ /app/assets/ {' \
    '        rewrite ^/app/(.*)$ /$1 break;' \
    '        expires 30d;' \
    '        add_header Cache-Control "public, immutable";' \
    '        try_files $uri =404;' \
    '    }' \
    '' \
    '    location ^~ /app/ {' \
    '        rewrite ^/app/(.*)$ /$1 break;' \
    '        try_files $uri $uri/ /index.html;' \
    '    }' \
    '' \
    '    gzip on;' \
    '    gzip_types text/plain text/css application/json application/javascript application/xml image/svg+xml;' \
    '' \
    '    location / {' \
    '        try_files $uri $uri/ /index.html;' \
    '    }' \
    '' \
    '    location = /favicon.ico {' \
    '        alias /usr/share/nginx/html/static/images/shandong-yunze-logo.png;' \
    '        add_header Cache-Control "no-cache";' \
    '    }' \
    '' \
    '    location /api/ {' \
    '        proxy_http_version 1.1;' \
    '        proxy_set_header Host $host;' \
    '        proxy_set_header X-Real-IP $remote_addr;' \
    '        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' \
    '        proxy_set_header X-Forwarded-Proto $scheme;' \
    '        proxy_pass $app_backend;' \
    '    }' \
    '' \
    '    location /upload/ {' \
    '        proxy_http_version 1.1;' \
    '        proxy_set_header Host $host;' \
    '        proxy_set_header X-Real-IP $remote_addr;' \
    '        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' \
    '        proxy_set_header X-Forwarded-Proto $scheme;' \
    '        proxy_pass $app_backend;' \
    '    }' \
    '' \
    '    location /assets/ {' \
    '        expires 30d;' \
    '        add_header Cache-Control "public, immutable";' \
    '        try_files $uri =404;' \
    '    }' \
    '}' \
    > /etc/nginx/conf.d/default.conf

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]
