FROM golang:1.24-bullseye AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o main .

FROM alpine:latest  AS runner
ARG POSTGRES_USER
ARG POSTGRES_PASSWORD
ARG POSTGRES_DB
ARG PGHOST
ARG PGPORT
ARG IMAGEOPTION
ARG CONCURRENT_IMAGES
ARG STORAGE_URL
ARG STORAGE_TOKEN

RUN apk add --no-cache ffmpeg ca-certificates libc6-compat curl
ENV GOMEMLIMIT=2750MiB
ENV GOGC=100
ENV PGHOST=$PGHOST
ENV POSTGRES_DB=$POSTGRES_DB
ENV PGPORT=$PGPORT
ENV POSTGRES_USER=$POSTGRES_USER
ENV POSTGRES_PASSWORD=$POSTGRES_PASSWORD
ENV IMAGEOPTION=$IMAGEOPTION
ENV CONCURRENT_IMAGES=$CONCURRENT_IMAGES
ENV STORAGE_URL=$STORAGE_URL
ENV STORAGE_TOKEN=$STORAGE_TOKEN

WORKDIR /app
COPY --from=builder /app/main /app/.
COPY --from=builder /app/public /app/public
COPY ./test.jpg /app/test.jpg
COPY ./scripts /app/scripts
COPY ./categorie.xml /app/categorie.xml
COPY ./caratteristiche.xml /app/caratteristiche.xml
RUN chmod +x /app/scripts/*.sh

RUN mkdir images
RUN mkdir videos
RUN mkdir failed_requests
EXPOSE 1323
CMD ["./main"]