# neopig SERP - Direct vault serving (fastest)
#
# Caddy serves media files directly from vault, bypassing Python.
# Only API/search goes through FastAPI.
#
# Run: caddy run --config Caddyfile.direct

{
    order rewrite before file_server
}

:8080 {
    log {
        output stdout
        format console
    }

    # Direct media serving from vault
    # URL: /media/abc123def... -> vault/ab/abc123def...
    handle /media/* {
        # Strip /media/ prefix
        uri strip_prefix /media

        # Map hash to vault subdirectory structure
        # First 2 chars of hash = subdirectory
        map {path} {vault_subdir} {
            ~^/([a-f0-9]{2}) "${1}"
            default ""
        }

        # Rewrite to vault path
        @hasSubdir expression {vault_subdir} != ""
        rewrite @hasSubdir /vault/{vault_subdir}{path}

        # Immutable cache headers (1 year)
        header Cache-Control "public, max-age=31536000, immutable"
        header X-Served-By "caddy-direct"

        # Serve files
        file_server {
            root .
            hide .git
        }
    }

    # API requests -> FastAPI
    handle /api/* {
        header Cache-Control "no-cache, no-store"
        reverse_proxy localhost:8000
    }

    # UI -> FastAPI
    handle {
        reverse_proxy localhost:8000
    }
}
