FROM golang:1.19 as prod-builder

RUN go install github.com/bufbuild/buf/cmd/buf@v1.17.0
RUN go install github.com/kyleconroy/sqlc/cmd/sqlc@latest

COPY . /go/src

WORKDIR /go/src/

RUN /go/bin/sqlc generate -f /go/src/sqlc.yaml
RUN /go/bin/buf mod update /go/src && /go/bin/buf generate

ARG service

WORKDIR /go/src/${service}

RUN go mod tidy \
	&& CGO_ENABLED=0 go build -v -gcflags="-trimpath=$(go env GOPATH)" -asmflags="-trimpath=$(go env GOPATH)" -o /opt/${service} /go/src/${service}

FROM alpine as production

ARG service

COPY --from=prod-builder /opt/${service} /opt/${service}

ENV SERVICE=${SERVICE}

ENTRYPOINT ["/opt/${SERVICE}"]
