diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b0199e3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,47 @@ +# Multi-stage build to create a minimal Docker image +FROM golang:1.25.6-alpine AS builder + +# Install git for go mod download +RUN apk add --no-cache git + +# Set working directory +WORKDIR /app + +# Copy go mod files +COPY go.mod go.sum ./ + +# Download dependencies +RUN go mod download + +# Copy source code +COPY . . + +# Build the binary +RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o mcgod ./cmd/mcgod + +# Final stage +FROM alpine:latest + +# Install ca-certificates for HTTPS requests +RUN apk --no-cache add ca-certificates + +# Create non-root user +RUN adduser -D -u 10001 mcgod + +# Set working directory +WORKDIR /home/mcgod + +# Copy the binary from builder stage +COPY --from=builder /app/mcgod . + +# Copy the environment file +COPY --from=builder /app/.env ./.env + +# Change ownership to non-root user +RUN chown -R mcgod:mcgod /home/mcgod + +# Switch to non-root user +USER mcgod + +# Command to run the application +CMD ["./mcgod"] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f27204d --- /dev/null +++ b/Makefile @@ -0,0 +1,89 @@ +# Makefile for mcgod project + +# Variables +BINARY_NAME = mcgod +DOCKER_IMAGE = docker.tipsy.codes/mcgod:latest +DOCKER_PLATFORMS = linux/amd64,linux/arm64 + +# Default target +.PHONY: build +build: build-amd64 + +# Build for AMD64 +.PHONY: build-amd64 +build-amd64: + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -o ${BINARY_NAME} ./cmd/mcgod + +# Build for ARM64 +.PHONY: build-arm64 +build-arm64: + CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -a -installsuffix cgo -o ${BINARY_NAME} ./cmd/mcgod + +# Build multi-architecture Docker image +.PHONY: build-docker +build-docker: + docker buildx build --platform ${DOCKER_PLATFORMS} -t ${DOCKER_IMAGE} --push . + +# Build Docker image for current architecture +.PHONY: build-docker-local +build-docker-local: + docker build -t ${DOCKER_IMAGE} . + +# Clean build artifacts +.PHONY: clean +clean: + rm -f ${BINARY_NAME} + rm -f ${BINARY_NAME}.tar.gz + +# Run the application locally +.PHONY: run +run: + ./${BINARY_NAME} + +# Run with Docker +.PHONY: run-docker +run-docker: + docker run --rm -it ${DOCKER_IMAGE} + +# Format Go code +.PHONY: fmt +fmt: + go fmt ./... + +# Lint Go code +.PHONY: lint +lint: + golangci-lint run + +# Test the application +.PHONY: test +test: + go test ./... + +# All targets +.PHONY: all +all: build clean + +# Generate Go documentation +.PHONY: doc +doc: + go doc ./... + +# Check dependencies +.PHONY: deps +deps: + go list -m all + +# Update dependencies +.PHONY: update +update: + go get -u ./... + +# Run vet +.PHONY: vet +vet: + go vet ./... + +# Run all checks +.PHONY: check +check: fmt vet lint test