# Dockerfile for go-places.
#
# The Go module lives in api/, so the build runs with api/ as the working
# directory. The version here has to be at least the `go` directive in
# api/go.mod, otherwise the toolchain refuses to build.

# --- Build stage ---
FROM golang:1.24-alpine AS builder

WORKDIR /src

# Copy the module definition first so dependency downloads stay cached when only
# application code changes.
COPY api/go.mod api/go.sum ./
RUN go mod download

COPY api/ ./

# CGO is not needed: the SQLite driver is the pure-Go modernc.org/sqlite.
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags '-s -w' -o /out/go-places ./cmd

# --- Final stage ---
FROM alpine:3.20

# ca-certificates lets the remote analyzer reach an HTTPS inference service.
# wget backs the container healthcheck below.
RUN apk add --no-cache ca-certificates wget

# The service runs unprivileged and needs to own the directories it writes to.
RUN adduser -D -u 10001 places \
 && mkdir -p /app/data/media \
 && chown -R places:places /app
WORKDIR /app

COPY --from=builder /out/go-places /app/go-places

USER places

ENV ADDR=":8080" \
    DATABASE_URL="file:/app/data/go-places.db?_pragma=busy_timeout(5000)&_pragma=journal_mode(WAL)&_pragma=foreign_keys(1)" \
    MEDIA_DIR="/app/data/media"

EXPOSE 8080

HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
  CMD wget -qO- "http://127.0.0.1:8080/healthz" || exit 1

ENTRYPOINT ["/app/go-places"]