# syntax=docker/dockerfile:1

# ── App tier: Node/Express ─────────────────────────────────────────────────────
FROM node:22-alpine AS app

WORKDIR /app

# Production dependencies only
COPY package*.json ./
RUN npm ci --omit=dev

# Application source
COPY src/    ./src/
COPY public/ ./public/

# SQLite database + traffic snapshots live on a mounted volume
VOLUME ["/app/data"]

EXPOSE 3000
ENV NODE_ENV=production

HEALTHCHECK --interval=30s --timeout=10s --start-period=15s --retries=3 \
    CMD wget -qO- http://localhost:3000/health || exit 1

CMD ["node", "src/index.js"]

# ── Web tier: nginx in front of the app ────────────────────────────────────────
FROM nginx:1.27-alpine AS nginx

WORKDIR /app

COPY docker/nginx/default.conf /etc/nginx/conf.d/default.conf
# Static assets served directly by nginx (rebuilt when public/ changes)
COPY --from=app /app/public ./public

EXPOSE 80

HEALTHCHECK --interval=30s --timeout=10s --start-period=15s --retries=3 \
    CMD wget -qO- http://localhost/health || exit 1
