# =============================================================================
# Caddyfile - Reverse Proxy with Automatic HTTPS
# =============================================================================
#
# Usage:
#   1. Replace {$DOMAIN} with your actual domain, or set DOMAIN in .env
#   2. Ensure DNS points to your EC2 instance
#   3. Caddy will automatically obtain and renew SSL certificates
#
# For local testing without SSL, use: localhost instead of {$DOMAIN}
#
# =============================================================================

{$DOMAIN:localhost} {
    # Reverse proxy to the app
    reverse_proxy app:3000

    # Compress everything except live streams (chat tokens, activity feed).
    # A compressed event stream makes content-inspecting proxies (TLS
    # inspection, antivirus/DLP gateways) hold the whole answer until it ends,
    # so users see it appear all at once. The streaming fetches always send
    # Accept: text/event-stream; the app also marks those responses
    # Content-Encoding: identity, which Caddy leaves alone.
    @compressible not header Accept *text/event-stream*
    encode @compressible gzip zstd

    # Security headers
    header {
        # Enable HSTS
        Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
        # Prevent MIME type sniffing
        X-Content-Type-Options "nosniff"
        # XSS protection
        X-XSS-Protection "1; mode=block"
        # Referrer policy
        Referrer-Policy "strict-origin-when-cross-origin"
        # Remove server header
        -Server
    }

    # Logging
    log {
        output stdout
        format console
    }
}

# Redirect www to non-www (optional - uncomment if needed)
# www.{$DOMAIN} {
#     redir https://{$DOMAIN}{uri} permanent
# }
