# syntax=docker/dockerfile:1.7
#
# Multi-stage build for the x0gp api-gateway (and other Go services later).
# Target: linux/arm64 (Orange Pi 5 Pro, Raspberry Pi 5) by default.
#         Override with --build-arg GOARCH=amd64 for x86 dev boxes.
#
# Stage 1: build
# Stage 2: minimal runtime on distroless base.

ARG GO_VERSION=1.26
ARG GOARCH=arm64

FROM --platform=linux/$GOARCH golang:${GO_VERSION}-alpine AS build

WORKDIR /src

# Cache deps first.
COPY go.mod go.sum* ./
RUN go mod download

# Build.
COPY . .
ARG CMD=poc-server
RUN CGO_ENABLED=0 GOOS=linux GOARCH=$GOARCH \
    go build -ldflags="-s -w" -o /out/app ./cmd/${CMD}

# Stage 2: runtime.
FROM --platform=linux/$GOARCH gcr.io/distroless/static-debian12:nonroot AS runtime

COPY --from=build /out/app /app

USER nonroot:nonroot
EXPOSE 8080
ENTRYPOINT ["/app"]
