Skip to content

platformfuzz/go-hello-service

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

47 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Go Hello Service

A minimal Go HTTP service with health checks, built with Docker and deployed via GitHub Actions.

πŸš€ Features

  • HTTP Server: Simple Go HTTP server with / and /health endpoints
  • Health Checks: Built-in health check endpoint for container orchestration
  • Containerized: Built with Docker using Alpine Linux base image
  • CI/CD: Automated build, test, and deployment pipeline
  • Multi-platform: Supports Linux AMD64 and ARM64
  • Security: Automated vulnerability scanning and linting

πŸ“‹ Prerequisites

  • Go 1.24+: For local development
  • Docker: For container operations
  • GitHub: For CI/CD pipeline

πŸ—οΈ Local Development

Using DevContainer (Recommended)

  1. Open in VS Code: Clone and open the repository
  2. DevContainer: VS Code will prompt to reopen in container
  3. Ready: All tools pre-installed in the container

Manual Setup

# Clone the repository
git clone git@github.com:platformfuzz/go-hello-service.git
cd go-hello-service

# Install dependencies
go mod tidy

# Run locally
go run ./cmd/server

# Test
go test ./cmd/server

🐳 Container Build

Using Docker (Recommended)

# Build locally
docker build -t go-hello-service .

# Run locally
docker run -p 8080:8080 go-hello-service

# Build for multiple platforms
docker buildx build --platform linux/amd64,linux/arm64 -t go-hello-service .

Container Details

  • Base Image: alpine:latest
  • Binary: /app/server
  • Port: 8080 (configurable via PORT env var)
  • Health Check: Built-in Docker health check with curl
  • User: Non-root user (appuser)

πŸ₯ Health Checks

HTTP Health Check

The service provides a /health endpoint that returns:

{
  "status": "healthy",
  "timestamp": "2024-01-01T12:00:00Z",
  "version": "1.0.0"
}

Docker Health Check

The container includes a built-in health check:

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

Container Orchestration Health Check

For ECS, Kubernetes, etc., use:

{
  "healthCheck": {
    "command": [
      "CMD-SHELL",
      "curl -f http://localhost:8080/health || exit 1"
    ],
    "interval": 30,
    "timeout": 5,
    "retries": 3,
    "startPeriod": 60
  }
}

Note: Uses curl (available in Alpine base image) for HTTP health checks.

πŸ”„ CI/CD Pipeline

GitHub Actions Workflow

The .github/workflows/ci.yml workflow provides:

PR Validation

  • βœ… Go Module Check: Ensures go.mod is clean
  • βœ… Binary Build: Validates Go compilation
  • βœ… Container Build: Tests Docker build process
  • βœ… Health Check Test: Validates container health check
  • βœ… Security Scan: Basic security checks
  • βœ… Linting: golangci-lint integration
  • βœ… Vulnerability Check: govulncheck integration
  • βœ… Tests: Automated test suite

Main Branch Deployment

  • πŸš€ Build & Push: Publishes to GitHub Container Registry
  • 🏷️ Tagging: Automatic latest and versioned tags
  • πŸ“¦ Multi-platform: AMD64 and ARM64 images

Registry

Images are published to: ghcr.io/platformfuzz/go-hello-service:latest

πŸ› οΈ Configuration

Environment Variables

  • PORT: Server port (default: 8080)

Docker Configuration

See Dockerfile for build configuration:

  • Multi-stage build: Go builder + Alpine runtime
  • Base Image: Alpine Linux with curl
  • Security: Non-root user
  • Health Check: Built-in curl-based health check

πŸ“Š API Endpoints

GET /

Returns a hello message with hostname and timestamp.

Response:

{
  "message": "Hello, World!",
  "timestamp": "2024-01-01T12:00:00Z",
  "hostname": "container-hostname"
}

GET /health

Returns service health status.

Response:

{
  "status": "healthy",
  "timestamp": "2024-01-01T12:00:00Z",
  "version": "1.0.0"
}

πŸ”§ Development

Project Structure

.
β”œβ”€β”€ cmd/
β”‚   └── server/
β”‚       β”œβ”€β”€ main.go          # Main server implementation
β”‚       └── main_test.go     # Unit tests
β”œβ”€β”€ .github/
β”‚   └── workflows/
β”‚       └── ci.yml           # CI/CD pipeline
β”œβ”€β”€ .devcontainer/
β”‚   └── devcontainer.json    # VS Code DevContainer
β”œβ”€β”€ .vscode/
β”‚   β”œβ”€β”€ extensions.json      # Recommended extensions
β”‚   └── settings.json        # Go development settings
β”œβ”€β”€ Dockerfile               # Docker build configuration
β”œβ”€β”€ go.mod                   # Go module definition
└── README.md               # This file

Adding Dependencies

# Add a new dependency
go get github.com/example/package

# Update go.mod
go mod tidy

Running Tests

# Run all tests
go test ./...

# Run with coverage
go test -cover ./...

# Run specific test
go test ./cmd/server

Docker Commands

# Build image
docker build -t go-hello-service .

# Run container
docker run -p 8080:8080 go-hello-service

# Check health
docker inspect go-hello-service --format='{{.State.Health.Status}}'

# Build multi-platform
docker buildx build --platform linux/amd64,linux/arm64 -t go-hello-service .

πŸš€ Deployment

ECS Fargate

{
  "family": "go-hello-service",
  "containerDefinitions": [
    {
      "name": "server",
      "image": "ghcr.io/platformfuzz/go-hello-service:latest",
      "portMappings": [
        {
          "containerPort": 8080,
          "protocol": "tcp"
        }
      ],
      "healthCheck": {
        "command": [
          "CMD-SHELL",
          "curl -f http://localhost:8080/health || exit 1"
        ],
        "interval": 30,
        "timeout": 5,
        "retries": 3,
        "startPeriod": 60
      }
    }
  ]
}

Docker Compose

version: '3.8'
services:
  server:
    image: ghcr.io/platformfuzz/go-hello-service:latest
    ports:
      - "8080:8080"
    environment:
      - PORT=8080
    healthcheck:
      test: ["CMD-SHELL", "curl -f http://localhost:8080/health || exit 1"]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 60s

πŸ”’ Security

  • Vulnerability Scanning: Automated govulncheck integration
  • Code Quality: golangci-lint enforcement
  • Minimal Base Image: Alpine Linux for reduced attack surface
  • Non-root User: Container runs as non-root user
  • Graceful Shutdown: Proper signal handling
  • Error Handling: Comprehensive error management

πŸ“ License

MIT License - see LICENSE file for details.

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Ensure all tests pass
  5. Submit a pull request

The CI/CD pipeline will automatically validate your changes before merging.

About

Minimal Go-based HTTP service with health check

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors