# Multi-stage build for broad-announce services.
# Stage 1: build the Go service binaries + loadgen + testfakes.
# Stage 2: tiny alpine with just the binaries + ca-certs.

FROM golang:1.25-alpine AS build
WORKDIR /src

# Cache deps
COPY go.mod go.sum ./
COPY loadgen/go.mod ./loadgen/go.mod
RUN go mod download

# Build
COPY . .
RUN go mod tidy
RUN --mount=type=cache,target=/root/.cache/go-build CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/ingestd ./cmd/ingestd && CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/routerd ./cmd/routerd && CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/deliverd-fcm ./cmd/deliverd-fcm && CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/deliverd-telegram ./cmd/deliverd-telegram && CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/deliverd-bench ./cmd/deliverd-bench && CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/telegramd ./cmd/telegramd && CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/admind ./cmd/admind && CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/archiverd ./cmd/archiverd && CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/authd ./cmd/authd && CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/seed ./cmd/seed && cd loadgen && CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/loadgen-http ./cmd/http && CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/loadgen-mqtt ./cmd/mqtt && CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/loadgen-grpc ./cmd/grpc && cd .. && CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/fakefcmd ./testfakes/fakefcmd && CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/faketgmd ./testfakes/faketgmd

FROM alpine:3.20
RUN apk add --no-cache ca-certificates
COPY --from=build /out/ /app/
# Default: print which binary to invoke; compose overrides command.
CMD ["/app/ingestd"]
