From 0dcb5d9b877224de924990be276d1a7913c02261 Mon Sep 17 00:00:00 2001 From: aovantsev Date: Thu, 2 Oct 2025 22:30:16 +0300 Subject: [PATCH] init --- .drone.yml | 41 +++++++++++++++++++++++++++++++++++++++++ .gitignore | 33 +++++++++++++++++++++++++++++++++ Dockerfile | 33 +++++++++++++++++++++++++++++++++ README.md | 2 -- docker-compose.yml | 24 ++++++++++++++++++++++++ go.mod | 3 +++ main.go | 25 +++++++++++++++++++++++++ 7 files changed, 159 insertions(+), 2 deletions(-) create mode 100644 .drone.yml create mode 100644 .gitignore create mode 100644 Dockerfile delete mode 100644 README.md create mode 100644 docker-compose.yml create mode 100644 go.mod create mode 100644 main.go diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..d887163 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,41 @@ +kind: pipeline +type: docker +name: default + +steps: + - name: build-and-deploy + image: docker:20.10.16 + environment: + DOCKER_HOST: unix:///var/run/docker.sock + volumes: + - name: docker-sock + path: /var/run/docker.sock + commands: + # Build the Docker image + - docker build -t localhost:5000/counter:latest . + + # Push to local registry + - docker push localhost:5000/counter:latest + + # Stop and remove existing container if it exists + - docker stop counter-app || true + - docker rm counter-app || true + + # Run the new container + - docker run -d --name counter-app -p 8080:8080 localhost:5000/counter:latest + + # Wait a moment for the container to start + - sleep 5 + + # Test the API + - curl -f http://localhost:8080/ || exit 1 + +volumes: + - name: docker-sock + host: + path: /var/run/docker.sock + +trigger: + branch: + - main + - master diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..91a6c95 --- /dev/null +++ b/.gitignore @@ -0,0 +1,33 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Go workspace file +go.work + +# IDE files +.vscode/ +.idea/ +*.swp +*.swo + +# OS generated files +.DS_Store +.DS_Store? +._* +.Spotlight-V100 +.Trashes +ehthumbs.db +Thumbs.db diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..266ec7f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,33 @@ +# Build stage +FROM golang:1.21-alpine AS builder + +WORKDIR /app + +# Copy go mod and sum files +COPY go.mod go.sum ./ + +# Download dependencies +RUN go mod download + +# Copy source code +COPY . . + +# Build the application +RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main . + +# Final stage +FROM alpine:latest + +# Install ca-certificates for HTTPS requests +RUN apk --no-cache add ca-certificates + +WORKDIR /root/ + +# Copy the binary from builder stage +COPY --from=builder /app/main . + +# Expose port +EXPOSE 8080 + +# Run the binary +CMD ["./main"] diff --git a/README.md b/README.md deleted file mode 100644 index c5c0d76..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# counter - diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..43537b1 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,24 @@ +version: '3.8' + +services: + # Local Docker Registry for testing + registry: + image: registry:2 + ports: + - "5000:5000" + volumes: + - registry-data:/var/lib/registry + environment: + REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY: /var/lib/registry + + # The counter application + counter-app: + build: . + ports: + - "8080:8080" + depends_on: + - registry + image: localhost:5000/counter:latest + +volumes: + registry-data: diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..26f3382 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module counter + +go 1.21 diff --git a/main.go b/main.go new file mode 100644 index 0000000..e1dbe3c --- /dev/null +++ b/main.go @@ -0,0 +1,25 @@ +package main + +import ( + "log" + "net/http" +) + +func helloHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + return + } + + w.Header().Set("Content-Type", "text/plain") + w.WriteHeader(http.StatusOK) + w.Write([]byte("Hello world")) +} + +func main() { + http.HandleFunc("/", helloHandler) + + port := ":8080" + log.Printf("Server starting on port %s", port) + log.Fatal(http.ListenAndServe(port, nil)) +}