A comprehensive Go/Gin API for card games with full blackjack and cribbage support, player hand tracking, multi-deck functionality, custom deck creation, and enterprise-grade observability.
- Features
- Quick Start
- API Documentation
- API Endpoints
- Game Types
- Deck Types
- Custom Deck Features
- Tech Stack
- Architecture
- Development
- Observability and Debugging
- Container Deployment
- Production Best Practices
- Contributing
- License
New to the Card Game API? Start here:
- Quick Start - Get the API running in minutes
- API Documentation - Interactive API explorer
- Examples - Complete game flows and usage examples
- CLAUDE.md - Development guide and project structure
- SECURITY.md - Security implementation details
- Multiple Card Games: Blackjack, Glitchjack (blackjack with random deck), Cribbage (with full scoring), Poker, War, Go Fish
- Multiple Deck Types: Standard 52-card, Spanish 21 (48-card, no 10s)
- Custom Decks: Create completely free-form custom decks with custom cards, suits, ranks, and attributes
- Player Management: Add/remove players, track individual hands
- Game Logic: Complete blackjack and cribbage implementations with automatic scoring
- Face Up/Down Cards: Full control over card visibility
- Multi-Pile Discard System: Support for multiple discard piles
- Concurrent Games: Thread-safe operations for multiple simultaneous games
- Session Management: UUID-based game sessions with cleanup
- Card Images: Auto-generated PNG images for all cards in icon (32x48), small (64x90), and large (200x280) formats
- Image URLs: All card responses include URLs for card images in three sizes
- Security: Comprehensive input validation and sanitization for all parameters
- API Documentation: Complete OpenAPI 3.0 specification with interactive Swagger UI
- Observability: OpenTelemetry metrics, Prometheus integration, structured logging
- Production Ready: Docker support, health checks, metrics endpoints, cross-platform builds
go run .Server runs on http://localhost:8080
# Build and run with Docker
docker build -t cardgame-api .
docker run -p 8080:8080 cardgame-api
# Or use Docker Compose
docker-compose updocker run -p 8080:8080 \
-e LOG_LEVEL=INFO \
-e GIN_MODE=release \
-e PORT=8080 \
-e TRUSTED_PROXIES="10.0.0.0/8" \
cardgame-api:latest# Build and run locally
make run
# Run with Docker
make docker-run
# Run with hot reload (development)
make devcurl http://localhost:8080/hello- Interactive API Documentation: http://localhost:8080/api-docs
- OpenAPI Specification: http://localhost:8080/openapi.yaml
- Usage Examples: See EXAMPLES.md for complete game flows
Create completely free-form custom decks with custom cards, suits, ranks, and attributes:
- Free-form Cards: Create any card with any name, rank, suit, and attributes
- Game Compatibility: Cards with numeric ranks automatically work in traditional games
- Custom Suits: Use traditional suits or create your own (hyenas, 4-leaf-clovers, etc.)
- Flexible Ranks: Use numbers, strings, or leave blank
- Rich Attributes: Add up to 100 custom key-value attributes per card
- Tombstone Deletion: Deleted cards remain queryable but are marked as deleted
- Auto-indexing: Cards get sequential indices for easy reference
- Security: All inputs are validated and sanitized
- Deck Name: 1-128 characters
- Cards per Deck: Maximum 2,000 cards
- Card Name: Maximum 100 characters
- Suit Name: Maximum 50 characters
- Attributes: Maximum 100 attributes per card
Example: See Custom Deck Examples for complete usage flows.
GET /hello- Health check endpointGET /metrics- Prometheus metrics endpointGET /stats- Application statistics in JSON formatGET /version- Build version informationGET /api-docs- Interactive API documentationGET /openapi.yaml- OpenAPI specificationGET /static/*- Static file serving (card images)
GET /deck-types- List available deck typesGET /game/new- Create new game (1 standard deck, 6 max players)GET /game/new/:decks- Create game with multiple decksGET /game/new/:decks/:type- Create game with specific deck typeGET /game/new/:decks/:type/:players- Create game with max player limitGET /games- List all active gamesDELETE /game/:gameId- Delete a game
GET /game/:gameId- Get basic game infoGET /game/:gameId/state- Get complete game state with hand valuesGET /game/:gameId/shuffle- Shuffle the deck
POST /game/:gameId/players- Add player{"name": "PlayerName"}DELETE /game/:gameId/players/:playerId- Remove player
POST /game/:gameId/start- Start blackjack game (deals initial cards)POST /game/:gameId/hit/:playerId- Player takes a cardPOST /game/:gameId/stand/:playerId- Player stands (ends turn)GET /game/:gameId/results- Get game results and winners
GET /game/new/glitchjack- Create new Glitchjack game (1 random deck, 6 max players)GET /game/new/glitchjack/:decks- Create Glitchjack game with multiple random decksGET /game/new/glitchjack/:decks/:players- Create Glitchjack game with specified decks and max playersPOST /game/:gameId/glitchjack/start- Start Glitchjack game (deals initial cards)POST /game/:gameId/glitchjack/hit/:playerId- Player takes a cardPOST /game/:gameId/glitchjack/stand/:playerId- Player stands (ends turn)GET /game/:gameId/glitchjack/results- Get game results and winners
GET /game/new/cribbage- Create new cribbage game (2 players, 1 deck)POST /game/:gameId/cribbage/start- Start cribbage game (deals 6 cards each)POST /game/:gameId/cribbage/discard/:playerId- Discard 2 cards to crib{"card_indices": [0, 1]}POST /game/:gameId/cribbage/play/:playerId- Play a card during play phase{"card_index": 0}POST /game/:gameId/cribbage/go/:playerId- Say "go" when can't play without exceeding 31GET /game/:gameId/cribbage/show- Score hands and crib (moves to next deal or ends game)GET /game/:gameId/cribbage/state- Get complete cribbage game state with scores
GET /game/:gameId/deal- Deal one card from deckGET /game/:gameId/deal/:count- Deal multiple cards from deckGET /game/:gameId/deal/player/:playerId- Deal card to specific player (face down)GET /game/:gameId/deal/player/:playerId/:faceUp- Deal card with face up/down control
POST /game/:gameId/discard/:pileId- Discard card{"player_id": "id", "card_index": 0}GET /game/:gameId/reset- Reset deck with same configurationGET /game/:gameId/reset/:decks- Reset with different deck countGET /game/:gameId/reset/:decks/:type- Reset with different deck type
POST /custom-decks- Create custom deck{"name": "Deck Name"}GET /custom-decks- List all custom decks with summariesGET /custom-decks/:deckId- Get custom deck details with all cardsDELETE /custom-decks/:deckId- Delete custom deck permanentlyPOST /custom-decks/:deckId/cards- Add card to deck{"name": "Card Name", "rank": 9, "suit": "custom", "attributes": {...}}GET /custom-decks/:deckId/cards- List cards in deck (?include_deleted=truefor deleted cards)GET /custom-decks/:deckId/cards/:cardIndex- Get specific card by indexDELETE /custom-decks/:deckId/cards/:cardIndex- Delete card (tombstone - remains queryable)
- ID: 0
- Cards: Ace through King in all 4 suits
- Total: 52 cards per deck
- ID: 1
- Cards: Ace through King in all 4 suits, excluding all 10s
- Total: 48 cards per deck
- Use Case: Spanish Blackjack variant
- Blackjack: Full blackjack implementation with automatic dealer play
- Glitchjack: Blackjack variant with randomly generated deck composition (each deck contains 52 random cards from standard deck, can have duplicates)
- Cribbage: Complete cribbage implementation with pegging, hand scoring, and crib
- Poker: Framework ready (cards, players, hands)
- War: Framework ready
- Go Fish: Framework ready
- Number cards (2-9): Face value
- Face cards (J, Q, K): 10 points each
- Aces: 11 points (automatically converted to 1 if hand would bust)
- Players join game (up to configured maximum)
- Game starts - deals 2 cards to each player and dealer
- Players' cards are face up, dealer's first card is face down
- Players take turns hitting or standing
- When all players finish, dealer plays automatically
- Dealer hits on 16 or less, stands on 17 or more
- Winners determined by comparing final hand values
- Blackjack: 21 with exactly 2 cards (beats regular 21)
- Win: Higher value than dealer without busting
- Push: Same value as dealer (tie)
- Bust: Hand value over 21 (automatic loss)
- Lose: Lower value than dealer
- Random Generation: Each deck contains 52 cards randomly selected from standard playing cards
- Duplicates Allowed: Multiple copies of the same card can appear in a single deck (e.g., multiple Ace of Hearts)
- Multi-Deck Support: Can create games with multiple random decks (each independently generated)
- Fresh Each Game: Each new game generates a completely new random deck composition
- Identical to Blackjack: Same hand values, dealer rules, and win conditions as standard Blackjack
- Hand Values: Aces (1 or 11), Face cards (10), Number cards (face value)
- Dealer Rules: Hits on 16 or less, stands on 17 or more
- Win Conditions: Same as Blackjack (21, bust, push, etc.)
- Card Counting Impossible: Random deck composition makes traditional card counting ineffective
- Unpredictable Probabilities: Each game has unique card distribution
- Same Rules, Different Experience: Familiar gameplay with unpredictable deck dynamics
- Players: Exactly 2 players
- Cards: Each player starts with 6 cards, discards 2 to the crib
- Goal: First player to reach 121 points wins
- Scoring: Points earned during play (pegging) and hand evaluation (show)
- Deal: Each player receives 6 cards
- Discard: Each player puts 2 cards in the crib (dealer's bonus hand)
- Play: Players alternate playing cards, keeping running total ≤ 31
- Show: Score hands and crib, check for winner
- Fifteen: Playing a card that makes the total exactly 15 (2 points)
- Thirty-One: Playing a card that makes the total exactly 31 (2 points)
- Pairs: Playing a card of the same rank as the previous card (2 points)
- Runs: Playing cards that form a sequence with recent cards (1 point per card)
- Go: Last card played when opponent can't play (1 point)
- Fifteens: Any combination of cards totaling 15 (2 points each)
- Pairs: Two or more cards of the same rank (2 points per pair)
- Runs: Three or more consecutive ranks (1 point per card)
- Flush: All hand cards same suit (4 points, 5 if starter matches)
- Nobs: Jack in hand matching starter suit (1 point)
- Ace: 1 point
- 2-10: Face value
- Face Cards (J, Q, K): 10 points each
- Ownership: Crib belongs to the dealer
- Scoring: Scored after both players' hands
- Composition: 4 cards (2 from each player) plus starter card
- Dealer Alternation: Dealer alternates each hand
- His Heels: If starter card is a Jack, dealer gets 2 points immediately
- Pegging Order: Non-dealer plays first in each round
- Go Rules: When a player can't play without exceeding 31, they say "go"
- Last Card: Player who plays the last card of a round gets 1 point
- Concurrent Games: Thread-safe operations for multiple simultaneous games
- Face Up/Down Cards: Full control over card visibility
- Multi-Deck Support: Perfect for casino-style blackjack (up to 100 decks)
- Spanish 21 Support: 48-card decks (no 10s) for Spanish Blackjack variant
- Session Management: UUID-based game sessions with automatic cleanup
- Real-time State: Live game state tracking with instant updates
See Advanced Examples for detailed usage.
The API returns appropriate HTTP status codes and descriptive error messages:
400 Bad Request- Invalid input parameters404 Not Found- Game, player, or resource not found409 Conflict- Game state conflicts (e.g., game is full)422 Unprocessable Entity- Valid input but cannot process (e.g., no cards remaining)500 Internal Server Error- Server errors
All errors include descriptive messages to help with debugging.
# Build for current platform
make build
# Build for all platforms (Linux, Windows, macOS)
make build-all
# Build Docker image
make docker-build
# Clean build artifacts
make clean# Run all tests
make test
# Run tests with coverage
make test-coverage
# Run specific package tests
go test ./handlers -v
go test ./services -v
go test ./models -v
# Run integration tests
make test-integration
# Generate coverage report
make coverage-html# Test card image functionality
./test_images.sh
# Test API documentation endpoints
./test_docs.sh
# Verify all card images exist
./verify_cards.sh
# Visual card testing (open in browser)
# Start server first, then open test_cards.html- Unit Tests: Models, services, validators, handlers (80%+ coverage target)
- Integration Tests: End-to-end API tests, game flows
- Visual Tests: Card image rendering and layout verification
- Security Tests: Input validation and sanitization functions
- Performance Tests: Load testing with concurrent games
# Run with hot reload
make dev
# Format code
make fmt
# Run linter
make lint
# Update dependencies
make deps
# Generate mocks
make mocksAll cards include auto-generated PNG images in three sizes:
- Icon: 32x48 pixels - Perfect for UI indicators
- Small: 64x90 pixels - Good for hand displays
- Large: 200x280 pixels - Full-size card display
Every card response includes image URLs for all three sizes. Face-down cards show card back images.
Generate Images: go run generate_cards.go (creates 157 total images)
Comprehensive security measures protect against malicious input:
- Input Validation: UUID format, parameter sanitization, length limits
- Injection Prevention: SQL injection, XSS, path traversal protection
- DoS Protection: Rate limiting, resource bounds, reasonable limits
- Container Security: Non-root user, read-only filesystem, security scanning
- Observability: Security event logging, metrics, audit trails
Details: See SECURITY.md for complete security implementation documentation.
The API is fully documented with OpenAPI 3.0 specification:
- Interactive Documentation: Visit http://localhost:8080/api-docs while the server is running for a complete interactive API reference with the ability to test endpoints directly
- OpenAPI Spec: Available at http://localhost:8080/openapi.yaml
- Complete Coverage: All 27 endpoints documented with request/response schemas, examples, and validation rules
- Request/Response Examples: Every endpoint includes realistic examples
- Schema Validation: Complete data models for all request and response objects
- Security Documentation: Input validation and sanitization details
- Error Responses: Documented error cases with appropriate HTTP status codes
- Try It Out: Interactive testing directly from the documentation
- Language: Go 1.24.4
- Framework: Gin HTTP web framework
- UUID: Google UUID for unique identifiers
- Testing: Testify for assertions
- Logging: Zap structured logging
- Metrics: OpenTelemetry with Prometheus exporter
- Concurrency: Go sync package for thread safety
- Image Generation: Go's image package with custom rendering
- Build: Cross-platform support with Make and Docker
The application follows clean architecture principles with clear separation of concerns:
├── api/ - Request/response DTOs
├── config/ - Configuration and observability setup
├── handlers/ - HTTP request handlers (organized by feature)
├── managers/ - Business logic managers
├── middleware/ - HTTP middleware (logging, metrics)
├── models/ - Domain models and entities
├── services/ - Business logic services
└── validators/ - Input validation logic
- Models: Domain entities (
Card,Deck,Player,Game,CustomDeck) - Services: Business logic layer for game operations
- Handlers: HTTP request handling separated by feature
- Managers: Thread-safe state management
- Middleware: Cross-cutting concerns (logging, metrics, recovery)
- Clean dependency management through
HandlerDependenciesstruct - Service interfaces for testability
- Separation of HTTP concerns from business logic
All game operations are protected by read-write mutexes to ensure safe concurrent access across multiple games and players.
See EXAMPLES.md for complete game flows:
- Blackjack Game Flow - Full blackjack game with 2 players
- Cribbage Game Flow - Complete cribbage game with scoring
- Custom Deck Creation - Create fantasy decks with custom cards
- Advanced Examples - Concurrent games, face up/down cards, multi-deck support
The Card Game API includes comprehensive observability features to monitor application performance and troubleshoot issues in production.
The application uses Zap for high-performance structured JSON logging:
LOG_LEVEL: Set logging level (DEBUG, INFO, WARN, ERROR) - defaults to INFOLOG_FORMAT: Log format (json, console) - defaults to json for productionTRUSTED_PROXIES: Comma-separated list of trusted proxy IPs for correct client IP extraction
- DEBUG: Detailed operations (card dealing, game state changes, validation steps)
- INFO: Normal operations (requests, game creation, successful operations)
- WARN: Validation failures, invalid requests, recoverable errors
- ERROR: System errors, failed operations, server failures
- Performance: Zap's zero-allocation design for minimal overhead
- Context: Automatic request ID and correlation tracking
- Searchability: JSON format for easy parsing and analysis
- Integration: Compatible with ELK, Splunk, Datadog, etc.
{
"level": "info",
"ts": 1722389931.234567,
"caller": "middleware/logging.go:45",
"msg": "HTTP request",
"request_id": "550e8400-e29b-41d4-a716-446655440000",
"method": "GET",
"path": "/game/abc123/deal",
"client_ip": "192.168.1.100",
"game_id": "abc123",
"status": 200,
"latency_ms": 0.101,
"user_agent": "curl/8.7.1"
}The application uses OpenTelemetry for comprehensive metrics collection with Prometheus export:
HTTP Metrics
http_requests_total: Total requests by method, path, statushttp_request_duration_seconds: Request latency histogramhttp_requests_in_flight: Current concurrent requestshttp_request_size_bytes: Request body size distributionhttp_response_size_bytes: Response body size distribution
Business Metrics
games_active: Current number of active games by typegames_created_total: Total games created by typegames_completed_total: Games finished by type and resultplayers_active: Current number of active playerscards_dealt_total: Total cards dealt by game typecustom_decks_active: Current number of custom deckscustom_cards_created_total: Total custom cards created
System Metrics
go_*: Go runtime metrics (goroutines, memory, GC)process_*: Process metrics (CPU, memory, file descriptors)cardgame_build_info: Build version and commit information
- Prometheus Format:
GET /metrics- Standard Prometheus exposition format - JSON Stats:
GET /stats- Human-readable metrics summary - Version Info:
GET /version- Detailed build information
{
"service": {
"name": "cardgame-api",
"version": "1.0.0",
"uptime": "2h34m15s"
},
"games": {
"active_count": 15,
"total_created": 247
},
"metrics": {
"http_requests_total": 1523,
"cards_dealt_total": 8934,
"api_errors_total": 12
},
"system": {
"timestamp": "2025-07-29T20:52:15Z",
"log_level": "info"
}
}- Check logs for game creation:
game_idfield in request logs - Verify game exists:
curl http://localhost:8080/games - Check if game was deleted: Look for DELETE operations in logs
- No cards remaining: Check
remaining_cardsin game state - Invalid game state: Verify game status is not "finished"
- Player not found: Confirm player was added to game
- Monitor
/metricsendpoint for high latency - Check
http_requests_in_flightfor request queuing - Review
http_request_duration_secondshistogram
# Filter by game ID
cat app.log | jq 'select(.game_id == "your-game-id")'
# Monitor error rates
cat app.log | jq 'select(.level == "error")'
# Track API performance
cat app.log | jq 'select(.latency != null) | {path, latency_human, status_code}'
# Watch live logs
tail -f app.log | jq 'select(.level == "error" or .status_code >= 400)'Development
export LOG_LEVEL=DEBUG
export LOG_FORMAT=console
export GIN_MODE=debug
export METRICS_ENABLED=trueProduction
export LOG_LEVEL=INFO
export LOG_FORMAT=json
export GIN_MODE=release
export TRUSTED_PROXIES="10.0.0.0/8,172.16.0.0/12"
export METRICS_ENABLED=true
export OTEL_SERVICE_NAME=cardgame-api
export OTEL_RESOURCE_ATTRIBUTES="environment=production,region=us-east-1"- Set up alerts on
api_errors_totalmetric spikes - Monitor
http_request_duration_seconds95th percentile - Track
active_gamesgrowth over time - Alert on sustained high
http_requests_in_flight
INFO Request started: POST /game/new
DEBUG Creating new game: decks=1, type=standard
INFO Game created successfully: game_id=abc123
INFO Request completed: status_code=200
INFO Request started: GET /game/abc123/deal
DEBUG Dealing card: game_id=abc123, remaining_cards=51
INFO Card dealt successfully: card="Ace of Hearts", remaining_cards=50
INFO Request completed: status_code=200
WARN Invalid game ID provided: game_id=invalid-id
WARN Game not found: game_id=nonexistent-game
ERROR Request failed: status_code=404
The Card Game API is fully containerized with production-grade Docker support.
Multi-Stage Build Process
- Dependencies Stage: Caches Go modules for faster rebuilds
- Build Stage: Compiles with optimizations and version injection
- Runtime Stage: Minimal Alpine image with security hardening
Image Characteristics
- Base: Alpine Linux 3.19 (minimal attack surface)
- Size: ~15MB base + ~5MB static assets
- User: Non-root (UID 65532)
- Security: No shell, read-only filesystem capable
# Build the Docker image
docker build -t cardgame-api:latest .
# Build with custom tags
docker build -t cardgame-api:v1.0.0 -t cardgame-api:latest .# Basic run
docker run -p 8080:8080 cardgame-api:latest
# Run with custom configuration
docker run -d \
--name cardgame-api \
-p 8080:8080 \
-e LOG_LEVEL=DEBUG \
-e PORT=8080 \
-e GIN_MODE=release \
-e TRUSTED_PROXIES="10.0.0.0/8" \
--restart unless-stopped \
cardgame-api:latest
# View logs
docker logs -f cardgame-api
# Check health
docker inspect cardgame-api --format='{{.State.Health.Status}}'The included docker-compose.yml provides a complete development environment:
# Start the application
docker-compose up -d
# View logs
docker-compose logs -f
# Stop the application
docker-compose down
# Start with monitoring stack (Prometheus + Grafana)
docker-compose --profile monitoring up -dConfigure the container using these environment variables:
| Variable | Description | Default |
|---|---|---|
| Server | ||
PORT |
Server port | 8080 |
GIN_MODE |
Gin framework mode (debug, release) | release |
TRUSTED_PROXIES |
Comma-separated trusted proxy IPs | "" |
| Logging | ||
LOG_LEVEL |
Logging level (DEBUG, INFO, WARN, ERROR) | INFO |
LOG_FORMAT |
Log format (json, console) | json |
| Metrics | ||
METRICS_ENABLED |
Enable metrics collection | true |
OTEL_SERVICE_NAME |
OpenTelemetry service name | cardgame-api |
OTEL_RESOURCE_ATTRIBUTES |
Additional resource attributes | "" |
The container includes built-in health checks:
- Endpoint:
/hello - Interval: 30 seconds
- Timeout: 3 seconds
- Retries: 3
-
Security:
- Runs as non-root user (UID 1000)
- Minimal Alpine base image
- No unnecessary packages
-
Resource Limits (via docker-compose):
- CPU: 1 core limit, 0.5 core reservation
- Memory: 512MB limit, 256MB reservation
-
Logging:
- JSON structured logs to stdout/stderr
- Log rotation configured in docker-compose
- Compatible with log aggregation systems
-
Monitoring:
- Prometheus metrics at
/metrics - Optional Prometheus/Grafana stack
- Built-in health endpoint
- Prometheus metrics at
Example Kubernetes deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: cardgame-api
spec:
replicas: 3
selector:
matchLabels:
app: cardgame-api
template:
metadata:
labels:
app: cardgame-api
spec:
containers:
- name: cardgame-api
image: cardgame-api:latest
ports:
- containerPort: 8080
env:
- name: LOG_LEVEL
value: "INFO"
- name: GIN_MODE
value: "release"
livenessProbe:
httpGet:
path: /hello
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
readinessProbe:
httpGet:
path: /hello
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
resources:
limits:
cpu: "1"
memory: "512Mi"
requests:
cpu: "500m"
memory: "256Mi"- Reverse Proxy: Use nginx/traefik for SSL termination
- Secrets: Use Docker secrets or Kubernetes secrets for sensitive data
- Persistence: Games are in-memory; add Redis for persistence if needed
- Scaling: Stateless design allows horizontal scaling
- Monitoring: Connect to your existing Prometheus/Grafana stack
The Card Game API implements comprehensive security measures for containerized deployments.
- Multi-stage build: Minimal attack surface
- Non-root user: Runs as UID 65532
- No shell: Reduced exploitation risk
- Read-only filesystem: Prevents runtime modifications
- Security labels: Enables automated scanning
Run comprehensive security scans:
# Run all security scans
make security-scan
# Build with security validation
make docker-build-secure
# Run with enhanced security
make compose-securityGenerate and manage secrets securely:
# Generate secure secrets
./scripts/generate-secrets.sh
# Run with secrets
docker-compose -f docker-compose.secrets.yml updocker-compose.security.yml includes:
- Read-only root filesystem
- Dropped Linux capabilities
- No new privileges flag
- Seccomp and AppArmor profiles
- Resource limits (CPU, memory, PIDs)
- Network isolation
- Localhost-only port binding
- Regular Updates: Rebuild images weekly for security patches
- Vulnerability Scanning: Use Trivy/Clair before deployment
- SBOM Generation: Track all dependencies
- Minimal Base: Alpine Linux with only essential packages
- Network Policies: Restrict egress/ingress traffic
- Resource Limits: Prevent DoS attacks
- Health Checks: Ensure service availability
- Audit Logging: Track all API access
- Never hardcode: Use environment variables or files
- Rotate regularly: Implement key rotation
- Encrypt at rest: Use Docker secrets or Kubernetes secrets
- Minimal exposure: Mount secrets as read-only files
Before deploying to production:
- Run
make security-scanand fix all HIGH/CRITICAL issues - Generate new secrets with
./scripts/generate-secrets.sh - Review Dockerfile with
hadolint Dockerfile - Enable read-only root filesystem
- Configure resource limits
- Set up network policies
- Enable audit logging
- Configure HTTPS/TLS
- Set up WAF rules
- Implement rate limiting
Example secure Kubernetes deployment:
apiVersion: v1
kind: ServiceAccount
metadata:
name: cardgame-api
namespace: cardgame
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: cardgame-api
namespace: cardgame
spec:
replicas: 3
selector:
matchLabels:
app: cardgame-api
template:
metadata:
labels:
app: cardgame-api
spec:
serviceAccountName: cardgame-api
securityContext:
runAsNonRoot: true
runAsUser: 65532
fsGroup: 65532
seccompProfile:
type: RuntimeDefault
containers:
- name: cardgame-api
image: cardgame-api:latest
imagePullPolicy: Always
ports:
- containerPort: 8080
protocol: TCP
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
runAsNonRoot: true
runAsUser: 65532
capabilities:
drop:
- ALL
env:
- name: LOG_LEVEL
value: "INFO"
- name: GIN_MODE
value: "release"
- name: API_KEY
valueFrom:
secretKeyRef:
name: cardgame-secrets
key: api-key
volumeMounts:
- name: tmp
mountPath: /tmp
livenessProbe:
httpGet:
path: /hello
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
readinessProbe:
httpGet:
path: /hello
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
resources:
limits:
cpu: "1"
memory: "512Mi"
requests:
cpu: "500m"
memory: "256Mi"
volumes:
- name: tmp
emptyDir: {}
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: cardgame-api-netpol
namespace: cardgame
spec:
podSelector:
matchLabels:
app: cardgame-api
policyTypes:
- Ingress
- Egress
ingress:
- from:
- namespaceSelector:
matchLabels:
name: ingress-nginx
ports:
- protocol: TCP
port: 8080
egress:
- to:
- namespaceSelector: {}
ports:
- protocol: TCP
port: 53 # DNS
- protocol: UDP
port: 53 # DNSThe security implementation helps meet common compliance requirements:
- PCI DSS: Secure configuration, access controls, monitoring
- HIPAA: Encryption, audit logs, access management
- SOC 2: Security controls, monitoring, incident response
- GDPR: Data protection, audit trails, secure processing
- Set appropriate environment variables
- Configure trusted proxies for your load balancer
- Enable structured logging with LOG_FORMAT=json
- Set up Prometheus scraping for /metrics endpoint
- Configure health check monitoring on /hello
- Use read-only filesystem in container
- Set resource limits (CPU, memory)
- Enable network policies in Kubernetes
- Configure TLS termination at load balancer
- Set up log aggregation (ELK, Datadog, etc.)
- Concurrency: Handles 10,000+ concurrent games
- Latency: Sub-millisecond response times for most operations
- Memory: ~100KB per active game
- CPU: Efficient with 1 CPU core handling 1000+ RPS
Example Grafana dashboards are available in deployments/grafana/:
- API Overview: Request rates, latencies, error rates
- Business Metrics: Active games, player counts, game outcomes
- System Health: CPU, memory, goroutines, GC metrics
docker build --platform linux/amd64 -t cardgame-api:latest .
docker save cardgame-api:latest > cardgame-api.tar
scp cardgame-api.tar root@glitchjack.com:/tmp/
scp docker-compose.prod.yml root@glitchjack.com:/opt/cardgame-api/
docker load -i /tmp/cardgame-api.tar
# On the production server
cd /opt/cardgame-api
docker-compose -f docker-compose.prod.yml up -d
# View logs
docker-compose -f docker-compose.prod.yml logs -f
# Stop the service
docker-compose -f docker-compose.prod.yml down
docker build --platform linux/amd64 -t blackjack-casino/frontend:latest .
docker save blackjack-casino/frontend:latest > blackjack-frontend.tar
scp blackjack-frontend.tar root@glitchjack.com:/tmp/
scp docker-compose.prod.yml root@glitchjack.com:/opt/cardgame-api/blackjackfrontend-compose.prod.yaml
docker build \
--build-arg VITE_API_BASE_URL=https://your-api.com \
-t blackjack-casino/frontend:latest .
cp .env.example .env
# Start the application
docker-compose up -d
# With Traefik reverse proxy (includes SSL)
docker-compose --profile proxy up -d
3. Direct Docker Run
docker run -d \
--name blackjack-frontend \
-p 3000:8080 \
--restart unless-stopped \
--security-opt no-new-privileges:true \
--memory 512m \
--cpus 1.0 \
blackjack-casino/frontend:latest
Contributions are welcome! Please ensure:
- All tests pass (
make test) - Code coverage remains above 80%
- Code is formatted (
make fmt) - No linting errors (
make lint) - Update documentation for new features
- Add appropriate tests for new functionality
This project is licensed under the terms specified in the LICENSE file.