# syntax=docker/dockerfile:1

FROM golang:1.26-bookworm AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=1 go build -trimpath -buildvcs=false -ldflags='-s -w' -o /out/tts-server .
# cgo links against the shared libs shipped inside the Go module.
RUN set -eux; \
    case "$(go env GOARCH)" in \
      amd64) lib=x86_64-unknown-linux-gnu ;; \
      arm64) lib=aarch64-unknown-linux-gnu ;; \
      *) echo "unsupported arch $(go env GOARCH)"; exit 1 ;; \
    esac; \
    mkdir -p /out/lib; \
    cp /go/pkg/mod/github.com/k2-fsa/sherpa-onnx-go-linux/v2@v2.0.1/lib/$lib/*.so /out/lib/

FROM debian:bookworm-slim
RUN apt-get update \
 && apt-get install -y --no-install-recommends libstdc++6 libgomp1 \
 && rm -rf /var/lib/apt/lists/* \
 && mkdir -p /cache && chown 65534:65534 /cache

WORKDIR /app
COPY --from=build /out/tts-server /app/tts-server
COPY --from=build /out/lib/ /app/lib/
ENV LD_LIBRARY_PATH=/app/lib \
    TTS_LISTEN=:8080 \
    TTS_VOICES=/voices \
    TTS_CACHE=/cache

EXPOSE 8080
USER 65534:65534
ENTRYPOINT ["/app/tts-server"]