FROM golang:1.26-alpine AS build

WORKDIR /src
ARG GOOSE_VERSION=v3.27.1
ARG GOPROXY=https://goproxy.cn,direct
ARG GOSUMDB=sum.golang.google.cn
ENV GOPROXY=${GOPROXY} \
    GOSUMDB=${GOSUMDB}

COPY go.mod go.sum* ./
RUN --mount=type=cache,target=/go/pkg/mod go mod download
COPY . .
RUN --mount=type=cache,target=/go/pkg/mod \
    --mount=type=cache,target=/root/.cache/go-build \
    go install github.com/pressly/goose/v3/cmd/goose@${GOOSE_VERSION}
RUN --mount=type=cache,target=/go/pkg/mod \
    --mount=type=cache,target=/root/.cache/go-build \
    CGO_ENABLED=0 GOOS=linux go build -o /out/hfb-api ./cmd/api

FROM alpine:3.22

WORKDIR /app
ENV TZ=Asia/Shanghai
RUN apk add --no-cache tzdata
COPY --from=build /out/hfb-api /app/hfb-api
COPY --from=build /go/bin/goose /app/goose
COPY --from=build /src/migrations /app/migrations
EXPOSE 8080
CMD ["/app/hfb-api"]
