# Build stage
FROM golang:1.21-alpine AS builder
WORKDIR /src

# sqlite3 driver requires CGO; install build deps
RUN apk add --no-cache build-base sqlite-dev

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

RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 \
    go build -ldflags "-s -w" -o /app/server ./cmd/server

# Final image
FROM alpine:3.18
RUN apk add --no-cache ca-certificates netcat-openbsd curl sqlite-libs
COPY --from=builder /app/server /usr/local/bin/server
COPY scripts/wait-for-mqtt.sh /usr/local/bin/wait-for-mqtt.sh
RUN chmod +x /usr/local/bin/wait-for-mqtt.sh
EXPOSE 8080
ENTRYPOINT ["/usr/local/bin/wait-for-mqtt.sh"]
