ARG GO_VERSION

FROM golang:${GO_VERSION}-alpine AS builder

RUN apk update && apk upgrade && \
    apk add --no-cache bash git openssh gcc libc-dev
ENV GOPATH /app

RUN mkdir -p /app/src/github.com/darkweak/cmd
RUN mkdir -p /app/src/github.com/darkweak/souin
ADD ./go.* /app/src/github.com/darkweak/souin/
RUN mkdir -p /ssl
ADD ./context /app/src/github.com/darkweak/souin/context
ADD ./pkg /app/src/github.com/darkweak/souin/pkg
ADD ./tests /app/src/github.com/darkweak/souin/tests
ADD ./configuration /app/src/github.com/darkweak/souin/configuration
ADD ./configurationtypes /app/src/github.com/darkweak/souin/configurationtypes
ADD ./helpers /app/src/github.com/darkweak/souin/helpers
ADD ./plugins /app/src/github.com/darkweak/souin/plugins

WORKDIR /app/src/github.com/darkweak/souin
RUN go mod download

WORKDIR /app/src/github.com/darkweak/souin/plugins/souin
RUN go mod download

RUN CGO_ENABLED=0 go build -a \
  -tags netgo -ldflags '-w -extldflags "-static"' -o /app/cmd/souin .

EXPOSE 80

FROM alpine:latest AS souin

COPY --from=builder /app/cmd/souin .
COPY --from=builder /app/src/github.com/darkweak/souin/configuration .
RUN mkdir -p /ssl
COPY --from=builder /app/src/github.com/darkweak/souin/plugins/souin/default .
RUN mkdir -p configuration
RUN mv *.yml configuration/
RUN mkdir -p default
RUN mv server.* default/
RUN chmod +x ./souin

CMD ["./souin"]
