Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions app_go/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Go build cache
*.exe
*.exe~
*.dll
*.so
*.dylib
*.test
*.out
go.work

# Compiled binary
devops-info-service

# Go workspace
vendor/

# Git
.git/
.gitignore
.gitattributes

# IDE
.vscode/
.idea/
*.swp
*.swo
*~

# Documentation (not needed in container)
README.md
docs/
*.md

# Screenshots
*.png
*.jpg
*.jpeg

# OS
.DS_Store
Thumbs.db

# Lab files
labs/
68 changes: 68 additions & 0 deletions app_go/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
###############################################################################
# Stage 1: Builder
# Purpose: Compile the Go application
# Base image: Full Go SDK with build tools
###############################################################################
FROM golang:1.21-alpine AS builder

# Set the working directory inside the container
WORKDIR /build

# Install git and other build dependencies (if needed for go mod download)
RUN apk add --no-cache git ca-certificates

# Copy go mod files first for better layer caching
# This layer will only be rebuilt when dependencies change
COPY go.mod go.sum* ./

# Download dependencies
RUN go mod download

# Copy source code
COPY main.go .

# Build the application
# -ldflags="-s -w" strips debug information to reduce binary size
# -o specifies the output filename
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o devops-info-service .


###############################################################################
# Stage 2: Runtime
# Purpose: Run the application with minimal footprint
# Base image: Alpine Linux (minimal but with basic tools)
###############################################################################
FROM alpine:3.19

# Install ca-certificates for HTTPS and wget for healthcheck
RUN apk add --no-cache ca-certificates wget

# Create non-root user
RUN addgroup -g 1000 appuser && \
adduser -D -u 1000 -G appuser appuser

# Copy CA certificates from builder
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/

# Copy the compiled binary from builder stage
COPY --from=builder /build/devops-info-service /usr/local/bin/devops-info-service

# Set ownership to non-root user
RUN chown appuser:appuser /usr/local/bin/devops-info-service

# Switch to non-root user
USER appuser

# Expose the application port
EXPOSE 8080

# Set default environment variables
ENV HOST=0.0.0.0 \
PORT=8080

# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:8080/health || exit 1

# Run the binary
ENTRYPOINT ["/usr/local/bin/devops-info-service"]
193 changes: 193 additions & 0 deletions app_go/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
# DevOps Info Service (Go)

A production-ready Go web service that provides comprehensive information about itself and its runtime environment. This is the compiled language version of the Python service, demonstrating multi-stage Docker build capabilities.

## Overview

The Go implementation of the DevOps Info Service is a lightweight, high-performance REST API that returns detailed system information, health status, and service metadata. This version demonstrates the advantages of compiled languages for containerized applications.

## Prerequisites

- Go 1.21 or higher

## Building

### Build for current platform
```bash
go build -o devops-info-service main.go
```

### Build for specific platforms
```bash
# Linux
GOOS=linux GOARCH=amd64 go build -o devops-info-service-linux main.go

# macOS (Apple Silicon)
GOOS=darwin GOARCH=arm64 go build -o devops-info-service-darwin-arm64 main.go

# macOS (Intel)
GOOS=darwin GOARCH=amd64 go build -o devops-info-service-darwin-amd64 main.go

# Windows
GOOS=windows GOARCH=amd64 go build -o devops-info-service.exe main.go
```

## Running the Application

### Using go run
```bash
go run main.go
```

### Using compiled binary
```bash
./devops-info-service
```

### With custom configuration
```bash
# Custom port
PORT=9090 go run main.go

# Custom host and port
HOST=127.0.0.1 PORT=3000 go run main.go
```

## API Endpoints

### GET /

Returns comprehensive service and system information.

**Response:**
```json
{
"service": {
"name": "devops-info-service",
"version": "1.0.0",
"description": "DevOps course info service",
"framework": "Go net/http"
},
"system": {
"hostname": "Mac",
"platform": "darwin",
"platform_version": "unknown",
"architecture": "arm64",
"cpu_count": 10,
"go_version": "go1.24.0"
},
"runtime": {
"uptime_seconds": 27,
"uptime_human": "27 seconds",
"current_time": "2026-01-27T19:29:02Z",
"timezone": "UTC"
},
"request": {
"client_ip": "127.0.0.1",
"user_agent": "curl/8.7.1",
"method": "GET",
"path": "/"
},
"endpoints": [
{
"path": "/",
"method": "GET",
"description": "Service information"
},
{
"path": "/health",
"method": "GET",
"description": "Health check"
}
]
}
```

### GET /health

Simple health check endpoint for monitoring and Kubernetes probes.

**Response:**
```json
{
"status": "healthy",
"timestamp": "2026-01-27T20:04:18Z",
"uptime_seconds": 84
}
```

## Configuration

The application can be configured via environment variables:

| Variable | Default | Description |
|----------|---------|-------------|
| `HOST` | `0.0.0.0` | Host to bind the server to |
| `PORT` | `8080` | Port number for the server |

## Binary Size Comparison

| Language | Binary Size | Startup Time | Memory Usage |
|----------|-------------|--------------|--------------|
| **Go** | ~2-3 MB | Instant | ~2-3 MB |
| Python | N/A (interpreter) | ~100ms | ~20-30 MB |

## Advantages of Go Implementation

1. **Small Binary Size**: The compiled binary is only 2-3 MB, compared to Python's interpreter + dependencies
2. **Fast Startup**: Instant startup time vs Python's interpreter overhead
3. **Low Memory Usage**: Significantly lower memory footprint
4. **Single Binary**: No dependencies to manage, just copy the binary
5. **Cross-Compilation**: Easily build for any platform from any machine
6. **Performance**: Better performance and concurrency support

## Project Structure

```
app_go/
├── main.go # Main application
├── go.mod # Go module definition
├── README.md # This file
└── docs/ # Lab documentation
├── LAB01.md # Implementation details
├── GO.md # Language justification
└── screenshots/ # Proof of work
```

## Examples

### Testing with curl
```bash
# Main endpoint
curl http://localhost:8080/

# Health check
curl http://localhost:8080/health

# Pretty print JSON
curl http://localhost:8080/ | jq
```

### Build and run
```bash
# Build
go build -o devops-info-service main.go

# Run
./devops-info-service

# Test
curl http://localhost:8080/
```

## Future Enhancements

This Go implementation will be used in Lab 2 to demonstrate:
- Multi-stage Docker builds
- Smaller final image size
- Static binary compilation
- Alpine-based containers

## License

Educational use for DevOps course.
Binary file added app_go/devops-info-service
Binary file not shown.
Loading