# 第 2 层 业务网关。多阶段构建：编译静态二进制 → distroless 最小运行镜像。
# 构建上下文须为「仓库根」（因 go.mod 用 replace ../sundynix-shared 引本地共享模块）：
#   docker build -f sundynix-gateway/Dockerfile -t sundynix/gateway .
FROM golang:1.25-alpine AS build
WORKDIR /src
# 先拷依赖清单分层缓存（命中则跳过重下）
COPY sundynix-shared/go.mod sundynix-shared/go.sum ./sundynix-shared/
COPY sundynix-gateway/go.mod sundynix-gateway/go.sum ./sundynix-gateway/
WORKDIR /src/sundynix-gateway
ENV GOWORK=off GOFLAGS=-mod=mod
RUN go mod download
# 再拷源码编译
WORKDIR /src
COPY sundynix-shared/ ./sundynix-shared/
COPY sundynix-gateway/ ./sundynix-gateway/
WORKDIR /src/sundynix-gateway
RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /out/gateway ./cmd/server

FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=build /out/gateway /gateway
EXPOSE 8080
ENTRYPOINT ["/gateway"]
