# syntax=docker/dockerfile:1
# Go XRPC service (cmd/puzzlemethis). The SvelteKit client has its own
# Dockerfile in client/; docker-compose.yml runs both.
#
# Example:
#   docker build -t puzzlemethis-api .
#   docker run -p 8080:8080 -v ./data:/data \
#     -e SERVICE_DID=did:web:api.example.com \
#     -e SERVICE_HOST=api.example.com \
#     -e SERVICE_PRIVATE_KEY=... puzzlemethis-api

FROM golang:1.26-bookworm AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
# mattn/go-sqlite3 compiles SQLite from C source: CGO is required, and the
# runtime image only needs a matching glibc (hence bookworm on both stages),
# no libsqlite3 package. themes.xml and board-image sprites are go:embed'd.
RUN CGO_ENABLED=1 go build -trimpath -o /out/puzzlemethis ./cmd/puzzlemethis

FROM debian:bookworm-slim
# curl is for the compose healthcheck; ca-certificates for HTTPS to PLC/slingshot.
RUN apt-get update \
 && apt-get install -y --no-install-recommends ca-certificates curl \
 && rm -rf /var/lib/apt/lists/* \
 && useradd --uid 1000 --create-home app
COPY --from=build /out/puzzlemethis /usr/local/bin/
# /data holds the SQLite db plus its WAL sidecars — mount a directory there
# that uid 1000 can write.
ENV SERVER_HOST=0.0.0.0 \
    DATABASE_URL=/data/puzzles.db \
    LOG_FORMAT=json
USER app
EXPOSE 8080
# Exec form so SIGTERM reaches the process directly — the server checkpoints
# the WAL on graceful shutdown.
ENTRYPOINT ["/usr/local/bin/puzzlemethis"]
