FROM node:12 AS builder
LABEL maintainer="Plup <plup@plup.io>"

# install dependencies
WORKDIR /build
ADD package.json package-lock.json ./
ADD src/ src/
RUN npm install --only=production

# build the app
ADD docker/config.json .
RUN npm run build

# change index to template
RUN mv /build/dist/index.html /build/dist/index.tpl

# generate a self signed certificate
RUN openssl req -x509 -newkey rsa:4096 -keyout /tmp/key.pem -out /tmp/cert.pem -days 365 -nodes -subj '/CN=localhost'

# serve
FROM nginx:latest
COPY --from=builder /build/dist /var/www
COPY --from=builder /tmp/*.pem /etc/nginx/
WORKDIR /var/www

# get specific config
COPY docker/nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 443

# get server address
ENV SERVER http://localhost:8000

# replace addresses at runtime
CMD /bin/bash -c "envsubst '\$SERVER' < index.tpl > index.html && \
    exec nginx -g 'daemon off;'"

# vim: ft=dockerfile:
