ARG ARCH

# STEP 1 build executable binary
FROM golang:1.16.7-alpine3.13@sha256:b26480f595fd4c03dacd793eeec866bc73e47f9e5673843cbb3a0e2820268742 AS builder-amd64
FROM arm32v7/golang:1.16.7-alpine3.13@sha256:1f52cd915cb844d697536be79c45f775daa5e3d954c23c0691938370670ed7dd AS builder-arm
FROM arm64v8/golang:1.16.7-alpine3.13@sha256:2ce14a0842a200c1edd14e9535f13b15ca03fd98b237d589f8ca971f10a411a3 AS builder-arm64
FROM builder-${ARCH} AS builder

ENV FILTRON_VERSION="v0.2.0"

WORKDIR $GOPATH/src/github.com/asciimoo/filtron

RUN apk upgrade --no-cache && \
    apk add --no-cache tar git curl && \
    curl -L "https://github.com/asciimoo/filtron/archive/${FILTRON_VERSION}.tar.gz" | tar xvz --strip 1

RUN go get -d -v && gofmt -l ./ && go build .

# STEP 2 build the image including only the binary
FROM alpine:3.14.1@sha256:eb3e4e175ba6d212ba1d6e04fc0782916c08e1c9d7b45892e9796141b1d379ae AS base-amd64
FROM arm32v7/alpine:3.14.1@sha256:fb9ac82b4cc94c5a6c416a1c656b3ee84df9290ab4106c260eb959997e759e5e AS base-arm
FROM arm64v8/alpine:3.14.1@sha256:bd9137c3bb45dbc40cde0f0e19a8b9064c2bc485466221f5e95eb72b0d0cf82e AS base-arm64
FROM base-${ARCH}

RUN apk --no-cache add ca-certificates="20191127-r5" && \
    adduser -D -h /usr/local/filtron -s /bin/false filtron filtron

COPY --from=builder /go/src/github.com/asciimoo/filtron/example_rules.json /etc/filtron/rules.json
COPY --from=builder /go/src/github.com/asciimoo/filtron/filtron /usr/local/filtron/filtron

EXPOSE 3000
USER filtron

ENTRYPOINT ["/usr/local/filtron/filtron", "--rules", "/etc/filtron/rules.json"]