FROM golang:1.24-alpine AS build

WORKDIR /src
RUN apk add --no-cache ca-certificates 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 ./gateway.go \
  && 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 \
  && addgroup -S haixun \
  && adduser -S -G haixun -h /app haixun
WORKDIR /app
COPY --from=build /out/gateway /app/gateway
COPY --from=build /out/worker /app/worker
COPY --from=build /out/tool /app/tool
COPY backend/etc /app/etc
USER haixun
EXPOSE 8890
CMD ["/app/gateway", "-f", "/app/etc/gateway.prod.yaml"]
