# Node 22 runs TypeScript directly with --experimental-strip-types, so there
# is no build step and no compiled artefact to keep in sync with the source.
FROM node:22-alpine

WORKDIR /app

# Workspace manifests first, so a dependency change is the only thing that
# busts the install layer.
COPY package.json package-lock.json ./
COPY server/package.json ./server/
RUN npm ci --omit=dev --workspace @hankan/server --include-workspace-root

# shared/ is imported by both the app and the server; the server needs the
# sync protocol at runtime.
COPY shared ./shared
COPY server ./server

ENV NODE_ENV=production
ENV PORT=8787
EXPOSE 8787

# The health route needs no token, precisely so this can reach it.
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s \
  CMD node -e "fetch('http://127.0.0.1:'+(process.env.PORT||8787)+'/health').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"

CMD ["node", "--experimental-strip-types", "server/src/main.ts"]
