# The public player: the embed page, the sandboxed engine frame and the small
# service that serves them. See player/README.md.
#
# Build from a git checkout. The image carries the source of the commit it
# was built from and offers it at /source.tar.gz, as the AGPL asks.

FROM node:26-alpine AS build
RUN apk add --no-cache git
WORKDIR /src
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
# The archive is cut from the commit, the player is built from the files.
# With uncommitted changes the two would differ, so such a tree is refused.
RUN git config --global --add safe.directory /src \
 && if [ -n "$(git status --porcelain)" ]; then \
      echo "The checkout has uncommitted changes. Build from a clean commit:" >&2; \
      git status --short >&2; \
      exit 1; \
    fi \
 && mkdir /out \
 && git rev-parse HEAD > /out/COMMIT \
 && git archive --format=tar.gz --prefix="strudels-$(git rev-parse --short=12 HEAD)/" -o /out/source.tar.gz HEAD
RUN npm run build:player

FROM node:26-alpine
ENV NODE_ENV=production PORT=8080
WORKDIR /app
COPY --from=build /src/LICENSE ./
COPY --from=build /src/player/server ./player/server
COPY --from=build /src/dist-player ./dist-player
COPY --from=build /out/COMMIT /out/source.tar.gz ./
# The service needs no write access and no privileges.
USER node
EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s \
  CMD wget -qO- http://127.0.0.1:8080/healthz || exit 1
CMD ["node", "player/server/main.mjs"]
