# 後端共用 image：一次 build gateway / worker / tool 三個 binary。
# compose 以 entrypoint/command 覆寫，決定該容器當 gateway、job worker 或一次性 init。
# build context = repo root（見 deploy/docker-compose.yml）。

FROM golang:1.23-alpine AS build
WORKDIR /src
RUN apk add --no-cache git
COPY backend/go.mod backend/go.sum ./
RUN go mod download
COPY backend/ ./
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags "-s -w" -o /out/gateway . \
 && CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags "-s -w" -o /out/worker ./cmd/worker \
 && CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags "-s -w" -o /out/tool ./cmd/tool

FROM alpine:3.20
RUN apk add --no-cache ca-certificates tzdata wget
ENV TZ=UTC
WORKDIR /app
COPY --from=build /out/ /app/
COPY backend/etc/ /app/etc/

# 預設角色為 gateway；compose 會把 worker / init 用 entrypoint 覆寫。
EXPOSE 8890
ENTRYPOINT ["/app/gateway"]
CMD ["-f", "/app/etc/gateway.prod.yaml"]
