File size: 7,383 Bytes
a58ece3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278

from __future__ import annotations

from string import Template

GO_VERSIONS = ["1.21", "1.22", "1.23", "1.24"]

_DOCKERFILE_TEMPLATE = Template("""\
<go_file>
<go_version> go$go_version
<go_pkg> dockerfile

# Dockerfile β€” multi-stage build for a Go Fiber application
# Pattern: golang builder + debian:bookworm-slim runtime
# Observed in: Medical-App-Core/Dockerfile

# ─── Stage 1: Build ────────────────────────────────────────────────────────
FROM golang:${go_version} AS builder

WORKDIR /app

# Copy dependency manifests first for better layer caching.
# Only re-downloads modules when go.mod or go.sum changes.
COPY go.mod go.sum ./
RUN go mod download

# Copy source and build the binary from cmd/
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o app ./cmd

# ─── Stage 2: Runtime ──────────────────────────────────────────────────────
FROM debian:bookworm-slim

WORKDIR /app

# CA certificates are required for outbound HTTPS (e.g., Stripe API).
RUN apt-get update && \\
    apt-get install -y --no-install-recommends ca-certificates curl && \\
    rm -rf /var/lib/apt/lists/*

COPY --from=builder /app/app .

EXPOSE $port

# Document all required environment variables (values supplied at runtime).
ENV DB_HOST="" \\
    DB_PORT="5432" \\
    DB_USER="" \\
    DB_PASSWORD="" \\
    DB_NAME="" \\
    DB_TIMEZONE="UTC" \\
    JWT_SECRET="" \\
    RABBITMQ_BASE_URL="" \\
    SWAGGER_USER="" \\
    SWAGGER_PASSWORD="" \\
    PORT="$port"

HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \\
  CMD curl -f http://localhost:$port/health || exit 1

COPY ./docker/api/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh /app/app

ENTRYPOINT ["/entrypoint.sh"]
</go_file>
""")

_ENTRYPOINT_TEMPLATE = Template("""\
<go_file>
<go_version> go$go_version
<go_pkg> shell

#!/bin/sh
# docker/api/entrypoint.sh
# Pattern: validate required env vars before exec'ing the binary
# Observed in: Medical-App-Core/docker/api/entrypoint.sh
set -e

echo "Checking required environment variables..."

MISSING=0
for VAR in DB_HOST DB_PORT DB_USER DB_PASSWORD DB_NAME JWT_SECRET; do
  if [ -z "$$(eval echo \\$$$${VAR})" ]; then
    echo "ERROR: $$VAR is not set"
    MISSING=1
  else
    echo "OK: $$VAR is set"
  fi
done

if [ "$$MISSING" -eq 1 ]; then
  echo "One or more required environment variables are missing. Aborting."
  exit 1
fi

echo "All required variables present. Starting application..."
exec ./app
</go_file>
""")

_DOCKER_COMPOSE_TEMPLATE = Template("""\
<go_file>
<go_version> go$go_version
<go_pkg> docker-compose

# docker-compose.yml β€” local development environment
# Pattern: app + postgres + rabbitmq services with health checks
# Observed from: Medical-App-Core infrastructure patterns
version: "3.9"

services:
  api:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "$port:$port"
    environment:
      - DB_HOST=postgres
      - DB_PORT=5432
      - DB_USER=postgres
      - DB_PASSWORD=postgres
      - DB_NAME=appdb
      - DB_TIMEZONE=UTC
      - JWT_SECRET=local-dev-secret-change-in-prod
      - RABBITMQ_BASE_URL=amqp://guest:guest@rabbitmq:5672/
      - SWAGGER_USER=admin
      - SWAGGER_PASSWORD=admin
      - PORT=$port
    depends_on:
      postgres:
        condition: service_healthy
      rabbitmq:
        condition: service_healthy
    restart: unless-stopped

  postgres:
    image: postgres:16-alpine
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: appdb
    ports:
      - "5432:5432"
    volumes:
      - pg_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5

  rabbitmq:
    image: rabbitmq:3.13-management-alpine
    ports:
      - "5672:5672"
      - "15672:15672"
    healthcheck:
      test: ["CMD", "rabbitmq-diagnostics", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

volumes:
  pg_data:
</go_file>
""")

_JENKINSFILE_TEMPLATE = Template("""\
<go_file>
<go_version> go$go_version
<go_pkg> jenkins

// Jenkinsfile β€” CI/CD pipeline
// Pattern: multi-stage pipeline: test β†’ build β†’ docker β†’ deploy
// Observed in: Medical-App-Core/Jenkinsfile
pipeline {
    agent any

    environment {
        DOCKER_IMAGE = "$image_name"
        DOCKER_TAG   = "$${BUILD_NUMBER}"
        GO_VERSION   = "$go_version"
    }

    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }

        stage('Test') {
            steps {
                sh '''
                    go test ./... -v -coverprofile=coverage.out
                    go tool cover -html=coverage.out -o coverage.html
                '''
            }
            post {
                always {
                    publishHTML(target: [
                        reportName:  'Go Coverage',
                        reportDir:   '.',
                        reportFiles: 'coverage.html',
                    ])
                }
            }
        }

        stage('Lint') {
            steps {
                sh 'go vet ./...'
            }
        }

        stage('Build Binary') {
            steps {
                sh 'CGO_ENABLED=0 GOOS=linux go build -o app ./cmd'
            }
        }

        stage('Docker Build') {
            steps {
                sh 'docker build -t $${DOCKER_IMAGE}:$${DOCKER_TAG} .'
                sh 'docker tag $${DOCKER_IMAGE}:$${DOCKER_TAG} $${DOCKER_IMAGE}:latest'
            }
        }

        stage('Docker Push') {
            steps {
                withCredentials([usernamePassword(
                    credentialsId: 'dockerhub-creds',
                    usernameVariable: 'DOCKER_USER',
                    passwordVariable: 'DOCKER_PASS'
                )]) {
                    sh '''
                        echo "$$DOCKER_PASS" | docker login -u "$$DOCKER_USER" --password-stdin
                        docker push $${DOCKER_IMAGE}:$${DOCKER_TAG}
                        docker push $${DOCKER_IMAGE}:latest
                    '''
                }
            }
        }
    }

    post {
        always {
            sh 'docker system prune -f || true'
        }
        failure {
            echo "Pipeline failed β€” check logs above"
        }
    }
}
</go_file>
""")


class DockerPatternGenerator:
    """Generate Docker + CI/CD training examples."""

    PORTS    = ["8080", "3040", "3000"]
    IMAGES   = ["myorg/my-api", "myorg/medical-api", "myorg/ecommerce-api"]

    def all_examples(self) -> list[str]:
        examples: list[str] = []
        for ver in GO_VERSIONS:
            for port in self.PORTS:
                examples.append(_DOCKERFILE_TEMPLATE.substitute(go_version=ver, port=port))
                examples.append(_DOCKER_COMPOSE_TEMPLATE.substitute(go_version=ver, port=port))
            examples.append(_ENTRYPOINT_TEMPLATE.substitute(go_version=ver))
            for img in self.IMAGES:
                examples.append(
                    _JENKINSFILE_TEMPLATE.substitute(go_version=ver, image_name=img)
                )
        return examples