FROM golang:1.21-alpine AS builder

WORKDIR /app

RUN apk add --no-cache ca-certificates

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

COPY . .

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o mengyaconnect-backend .

FROM alpine:3.19

WORKDIR /app

RUN apk add --no-cache ca-certificates && \
    adduser -D -H appuser

COPY --from=builder /app/mengyaconnect-backend /app/mengyaconnect-backend

ENV DATA_DIR=/app/data
ENV PORT=8080

RUN mkdir -p "$DATA_DIR" && chown -R appuser:appuser /app

USER appuser

EXPOSE 8080

CMD ["/app/mengyaconnect-backend"]

