jerryibrahim/go-canary

By jerryibrahim

Updated 9 days ago

Mock backend

Image
Integration & delivery
API management
Developer tools
0

1.3K

jerryibrahim/go-canary repository overview

go-canary

A lightweight, configurable canary/test API server written in Go. Perfect for testing backend integrations, simulating slow or large responses, and validating infrastructure configurations.

Quick Start

Docker Run
docker run -d -p 8080:8080 jerryibrahim/go-canary
Docker Compose
services:
  api:
    image: jerryibrahim/go-canary
    ports:
      - "8080:8080"
    environment:
      - PORT=8080
      - COLOR=blue
      - LOG_LEVEL=INFO
With TLS
docker run -d \
  -p 8080:8080 \
  -p 8443:8443 \
  -v /path/to/certs:/secrets:ro \
  -e TLS_CERT_FILE=/secrets/tls-cert \
  -e TLS_KEY_FILE=/secrets/tls-key \
  jerryibrahim/go-canary

Environment Variables

VariableDefaultDescription
PORT8080HTTP listen port
PORT_TLS8443HTTPS listen port (if TLS enabled)
LOG_LEVELINFOLogging level: VERBOSE, TRACE, DEBUG, INFO, WARN, ERROR
LOG_COLORfalseEnable colorized log headers
LOG_TIMESTAMP_NANOfalseAdd nanoseconds to timestamp
VERSION0.0.0Override app version (reads from /assets/version.txt by default)
COLORblueColor label for blue/green deployments
GCR_ENVfalseEnable Google Cloud Run metadata fetching
PROJECT_IDlocal_svcGoogle Project ID
TLS_CERT_FILE/secrets/tls-certPath to TLS certificate file
TLS_KEY_FILE/secrets/tls-keyPath to TLS private key file
TLS_MIN_VERSION1.2Minimum TLS version (1.2 or 1.3)
API_KEY(empty)API key for api-key header authorization. If empty, no API key required
ADMIN_KEY(empty)Admin key for admin-key header authorization on /admin/* endpoints. If empty, no auth required
CORSfalseEnable CORS headers
CORS_DOMAINS(empty)Comma-separated allowed origin domains for CORS. Empty = all domains
DOMAINS_FILTER(empty)Comma-separated allowed outbound domains for HTTP client requests (egress filter). Empty = all domains

API Endpoints

Health & Status
EndpointMethodDescription
/statusGETStandard health check endpoint
/statsGETReturns server statistics (memory, threads, cache, queue)

Example: /stats response

{
  "timestamp": "2024-01-15T10:30:00Z",
  "proc_mem": 12.345,
  "proc_threads": 8,
  "domains_cache": 5,
  "dlq": 0,
  "queue": 0,
  "cache": 3
}

Version & Color (Blue/Green)
EndpointMethodDescription
/GETReturns color as plain text
/versionGETReturns version as JSON
/colorGETReturns version and color as JSON

Example: /version response

{
  "version": "1.0.0"
}

Example: /color response

{
  "version": "1.0.0",
  "color": "blue"
}

Testing Endpoints
Simulated Delay: /time

Simulates a slow response by waiting a specified number of seconds.

ParameterTypeDefaultDescription
kint1Number of seconds to wait before responding

Examples:

# Wait 1 second (default)
curl http://localhost:8080/time

# Wait 5 seconds
curl http://localhost:8080/time?k=5

Response:

{
  "version": "1.0.0",
  "time": "5"
}

Large Response: /size

Returns a configurable payload size for testing bandwidth and response handling.

ParameterTypeDefaultDescription
kint1Response size in KB

Examples:

# Return ~1KB response (default)
curl http://localhost:8080/size

# Return ~15KB response
curl http://localhost:8080/size?k=15

Memory Cache: /cache

Test in-memory caching functionality. First request sets the cache, subsequent requests return cached value.

ParameterTypeDefaultDescription
kstring1Cache key identifier

Examples:

# Get/set cache with default key
curl http://localhost:8080/cache

# Get/set cache with custom key
curl http://localhost:8080/cache?k=mykey

Response:

{
  "type": "CACHE",
  "version": "1.0.0",
  "key": "mykey",
  "timestamp": "2024-01-15T10:30:00Z"
}

Request Queue: /queue

Add requests to an internal queue for async processing demonstration.

ParameterTypeDefaultDescription
kstring1Queue item key identifier

Examples:

# Add to queue with default key
curl http://localhost:8080/queue

# Add to queue with custom key
curl http://localhost:8080/queue?k=order123

Response:

{
  "type": "QUEUE",
  "version": "1.0.0",
  "key": "order123",
  "timestamp": "2024-01-15T10:30:00Z"
}

POST Data Echo: /post

Echo back POST form data and headers (POST method only).

Example:

curl -X POST -d "param1=value1&param2=value2" http://localhost:8080/post

Runtime Configuration: /admin/setenv

Dynamically update environment variables and control application state (POST method only). Protected by MiddlewareAdminKey — if ADMIN_KEY env var is set, requests must include the admin-key header.

Update Log Level:

curl -X POST -d "LOG_LEVEL=DEBUG" http://localhost:8080/admin/setenv

Update Version:

curl -X POST -d "VERSION=1.1.1" http://localhost:8080/admin/setenv

Application Control:

# Reset application (clears cache, queues, memory)
curl -X POST -d "app=reset" http://localhost:8080/admin/setenv

# Pause queue consumers
curl -X POST -d "app=pause" http://localhost:8080/admin/setenv

# Resume queue consumers
curl -X POST -d "app=resume" http://localhost:8080/admin/setenv

With ADMIN_KEY authentication:

curl -X POST -H "admin-key: <key>" -d "LOG_LEVEL=DEBUG" http://localhost:8080/admin/setenv

HTTP Error Simulation

Simulate various HTTP error responses for testing error handling.

EndpointResponse
/400400 Bad Request
/401401 Unauthorized
/403403 Forbidden
/404404 Not Found
/405405 Method Not Allowed
/500500 Internal Server Error

Example:

curl -i http://localhost:8080/500
# HTTP/1.1 500 Internal Server Error

API Key Authentication

When API_KEY environment variable is set, all requests must include the api-key header:

# Set API key on container
docker run -d -p 8080:8080 -e API_KEY=mysecretkey jerryibrahim/go-canary

# Make authenticated request
curl -H "api-key: mysecretkey" http://localhost:8080/status

CORS Configuration

Enable CORS support for browser-based applications:

# Enable CORS for all domains
docker run -d -p 8080:8080 -e CORS=true jerryibrahim/go-canary

# Enable CORS for specific domains only
docker run -d -p 8080:8080 \
  -e CORS=true \
  -e CORS_DOMAINS=example.com,myapp.com \
  jerryibrahim/go-canary

TLS/HTTPS Support

The container automatically enables HTTPS when TLS certificates are provided:

docker run -d \
  -p 8080:8080 \
  -p 8443:8443 \
  -v /path/to/certs:/secrets:ro \
  -e TLS_CERT_FILE=/secrets/tls.crt \
  -e TLS_KEY_FILE=/secrets/tls.key \
  jerryibrahim/go-canary

The server will:

  • Listen on HTTP port (default: 8080)
  • Listen on HTTPS port (default: 8443) if valid certificates are found
  • Automatically reload certificates when they change

Use Cases

  • Blue/Green Deployments: Use the COLOR environment variable to identify deployment versions
  • Load Testing: Use /size and /time endpoints to simulate various response characteristics
  • Infrastructure Testing: Validate load balancers, proxies, and API gateways
  • Error Handling: Test application behavior with various HTTP error codes
  • CORS Testing: Validate cross-origin configurations
  • Health Checks: Use /status endpoint for container orchestration health probes

License

Copyright © 2019-2026 @jerryibrahim

Tag summary

Content type

Image

Digest

sha256:c421713b6

Size

7.3 MB

Last updated

9 days ago

docker pull jerryibrahim/go-canary:2.3.3