FROM oven/bun:1@sha256:9114c058aeae42162ee16dd5084b95fe9473970bb6bcb5b232ab1630f0546895

WORKDIR /app

# Install the e2e npm-compat registry server.
# Source mirrors packages/registry-server/'s shape but is a separate program:
# this one serves npm packuments + .tgz, the other serves the Cargo sparse
# protocol for Celilo modules.
COPY npm-registry-server/package.json ./package.json
COPY npm-registry-server/tsconfig.json ./tsconfig.json
COPY npm-registry-server/src ./src

RUN bun install --production

# Tarballs staged by `pack-celilo-packages.ts` at e2e-build time. These are
# the @celilo/* workspace packages that install.sh asks bun to fetch:
#   @celilo/cli, @celilo/event-bus, @celilo/e2e (named in install.sh)
#   @celilo/capabilities, @celilo/cli-display (transitive deps)
COPY .npm-registry-cache /var/lib/npm

ENV PACKUMENT_DIR=/var/lib/npm \
    PORT=80 \
    REGISTRY_URL=http://npm-registry.lab

# Route the customer's public prefix via the ISP edge, like every other host
# on internet-external. See config/routing/public-sim-entrypoint.sh.
RUN apt-get update \
 && apt-get install -y --no-install-recommends iproute2 \
 && rm -rf /var/lib/apt/lists/*
COPY config/routing/public-sim-entrypoint.sh /usr/local/bin/public-sim-entrypoint.sh

EXPOSE 80
ENTRYPOINT ["/bin/sh", "/usr/local/bin/public-sim-entrypoint.sh"]
CMD ["bun", "run", "src/server.ts"]
