# Prism 代理网关（Go 控制面 + 嵌入 Mihomo + Vue 管理台）
#
# 构建:
#   docker build -t prism:latest .
#
# 运行:
#   docker run -d --name prism \
#     -p 8080:8080 -p 7890:7890 -p 7891:7891 \
#     -v prism-data:/app/data \
#     prism:latest
#
# 生产镜像由 Gitea Actions 构建，见 README「部署」

# syntax=docker/dockerfile:1

# 基础镜像默认 Docker Hub；CI 默认 1Panel 加速（docker.1panel.live），本地可传：
#   docker build --build-arg IMAGE_PREFIX=docker.1panel.live/library/ -t prism:latest .
ARG IMAGE_PREFIX=

FROM --platform=$BUILDPLATFORM ${IMAGE_PREFIX}node:20-alpine AS web-builder

WORKDIR /src/web
COPY web/package.json web/package-lock.json web/.npmrc ./
RUN npm ci
COPY web/ ./
RUN npm run build

FROM --platform=$BUILDPLATFORM ${IMAGE_PREFIX}golang:1.25-alpine AS go-builder

ARG TARGETARCH
ARG GOPROXY=https://goproxy.cn,direct
ARG GOSUMDB=sum.golang.google.cn
ENV GOPROXY=${GOPROXY} GOSUMDB=${GOSUMDB}

WORKDIR /src
COPY go.mod go.sum go.env ./
RUN --mount=type=cache,target=/go/pkg/mod \
    go mod download

COPY cmd/ ./cmd/
COPY internal/ ./internal/

RUN rm -rf internal/api/static/* && mkdir -p internal/api/static
COPY --from=web-builder /src/web/dist/ ./internal/api/static/

RUN --mount=type=cache,target=/go/pkg/mod \
    --mount=type=cache,target=/root/.cache/go-build \
    CGO_ENABLED=0 GOOS=linux GOARCH="$TARGETARCH" \
    go build -ldflags="-w -s" -o /prism ./cmd/prism

FROM ${IMAGE_PREFIX}alpine:3.20

RUN apk add --no-cache ca-certificates tzdata \
    && addgroup -S prism \
    && adduser -S prism -G prism

WORKDIR /app

COPY --from=go-builder /prism /usr/local/bin/prism

RUN mkdir -p /app/data/geo \
    && chown -R prism:prism /app

USER prism

VOLUME ["/app/data"]

EXPOSE 8080 7890 7891

ENTRYPOINT ["/usr/local/bin/prism"]
CMD ["-data", "/app/data"]
