FROM golang:1.24-alpine AS base

WORKDIR /src

COPY go.mod go.sum ./
RUN go mod download

COPY . .

FROM base AS test
RUN go test ./...

FROM base AS builder
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /out/api ./cmd/api \
    && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /out/worker ./cmd/worker \
    && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /out/seed-sample-job ./cmd/tools/seed-sample-job

FROM alpine:3.20

RUN addgroup -S app && adduser -S app -G app

WORKDIR /app

COPY --from=builder /out/api /app/api
COPY --from=builder /out/worker /app/worker
COPY --from=builder /out/seed-sample-job /app/seed-sample-job
COPY job-config-list /app/job-config-list
COPY docker-entrypoint.sh /app/docker-entrypoint.sh

RUN chmod +x /app/api /app/worker /app/seed-sample-job /app/docker-entrypoint.sh \
    && chown -R app:app /app

USER app

ENV HTTP_PORT=16811 \
    WORKER_HTTP_PORT=16812

EXPOSE 16811 16812

ENTRYPOINT ["/app/docker-entrypoint.sh"]
