| # Build Stage | |
| FROM golang:1.21-alpine AS builder | |
| WORKDIR /app | |
| # Copy the server and cli source code | |
| COPY main.go go.mod ./ | |
| COPY cli/ cli/ | |
| # Create directory to store precompiled binaries | |
| RUN mkdir -p bin | |
| # Cross-compile CLI for various target OS and architectures | |
| RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o bin/linux-amd64 cli/main.go | |
| RUN CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="-s -w" -o bin/linux-arm64 cli/main.go | |
| RUN CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags="-s -w" -o bin/windows-amd64 cli/main.go | |
| RUN CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w" -o bin/darwin-amd64 cli/main.go | |
| RUN CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags="-s -w" -o bin/darwin-arm64 cli/main.go | |
| # Build the main server binary statically linked | |
| RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o server main.go | |
| # Production Stage | |
| FROM scratch | |
| WORKDIR /app | |
| # Copy the server binary and precompiled CLI binaries | |
| COPY --from=builder /app/server /app/server | |
| COPY --from=builder /app/bin /app/bin | |
| # Environment variables (Can be overridden in HF Spaces configuration) | |
| ENV DATA_DIR=/data | |
| ENV BIN_DIR=/app/bin | |
| ENV PORT=7860 | |
| # Expose default HF Spaces port | |
| EXPOSE 7860 | |
| # Command to run backend | |
| CMD ["/app/server"] | |