FROM node:latest as frontend-builder
RUN mkdir -p /app/frontend
COPY web/frontend/yarn.lock /app/frontend/yarn.lock
COPY web/frontend/package.json /app/frontend/package.json
WORKDIR /app/frontend
RUN yarn install
COPY web/frontend /app/frontend
WORKDIR /app/frontend
RUN GENERATE_SOURCEMAP=false yarn build

FROM golang:latest as builder
WORKDIR /app
COPY go.mod /app/go.mod
COPY go.sum /app/go.sum
RUN go mod download
COPY . /app
RUN rm -rf /app/server/frontend
COPY --from=frontend-builder /app/frontend/dist /app/web/frontend/dist
RUN CGO_ENABLED=0 INSTALL_PREFIX=/app make install

FROM alpine:latest
RUN apk add --no-cache curl
WORKDIR /app
COPY --from=builder /app/etc/apiary/apiary.toml /app/apiary.toml
COPY --from=builder /app/bin/apiary /app/apiary
EXPOSE 8080
EXPOSE 2222
CMD ["/app/apiary", "serve"]