diff --git a/Makefile b/Makefile index 11e511f..a473eab 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ run-worker: go run ./cmd/worker docker-up: - docker-compose up --build --detach + docker-compose up --build --detach --wait docker-down: docker-compose down \ No newline at end of file diff --git a/README.md b/README.md index 2736ab9..f7f8ef7 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ curl http://localhost:8080/stats/123 | Requirement | Minimum / Bonus | | - | - | -| Go 1.22+, only stdlib + light libs | sqlx / chi / validator OK | +| Go 1.24+, only stdlib + light libs | sqlx / chi / validator OK | | `go vet` and `go test -race` pass | required | | Test coverage | ≥ 60% | | Dockerfile + docker-compose.yml | bonus (+1) | diff --git a/cmd/server/Dockerfile b/cmd/server/Dockerfile new file mode 100644 index 0000000..8068e2f --- /dev/null +++ b/cmd/server/Dockerfile @@ -0,0 +1,37 @@ +# Build stage +FROM golang:1.24-alpine AS builder + +WORKDIR /app + +# Install build dependencies +RUN apk add --no-cache gcc musl-dev + +# Copy go mod files +COPY go.mod go.sum ./ +RUN go mod download + +# Copy source code +COPY . . + +# Build the application +RUN CGO_ENABLED=1 GOOS=linux go build -o server ./cmd/server + +# Final stage +FROM alpine:latest + +WORKDIR /app + +# Install runtime dependencies +RUN apk add --no-cache sqlite + +# Copy the binary from builder +COPY --from=builder /app/server . + +# Create directory for database +RUN mkdir -p /data + +# Expose the port +EXPOSE 8080 + +# Run the application +CMD ["./server", "--db", "/data/data.db", "--listen", "8080"] diff --git a/cmd/worker/Dockerfile b/cmd/worker/Dockerfile new file mode 100644 index 0000000..94e9cd5 --- /dev/null +++ b/cmd/worker/Dockerfile @@ -0,0 +1,34 @@ +# Build stage +FROM golang:1.24-alpine AS builder + +WORKDIR /app + +# Install build dependencies +RUN apk add --no-cache gcc musl-dev + +# Copy go mod files +COPY go.mod go.sum ./ +RUN go mod download + +# Copy source code +COPY . . + +# Build the application +RUN CGO_ENABLED=1 GOOS=linux go build -o worker ./cmd/worker + +# Final stage +FROM alpine:latest + +WORKDIR /app + +# Install runtime dependencies +RUN apk add --no-cache sqlite + +# Copy the binary from builder +COPY --from=builder /app/worker . + +# Create directory for database +RUN mkdir -p /data + +# Run the application +CMD ["./worker", "--db", "/data/data.db", "--poll", "100ms"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..f31d1ee --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,20 @@ +services: + server: + build: + context: ./ + dockerfile: ./cmd/server/Dockerfile + volumes: + - broker-db:/data + ports: + - "8080:8080" + + worker: + build: + context: ./ + dockerfile: ./cmd/worker/Dockerfile + volumes: + - broker-db:/data + +volumes: + broker-db: + driver: local \ No newline at end of file