{
    # Global options - works for both environments
    email {$LETSENCRYPT_EMAIL}
}

import /etc/caddy/addons/cors.conf

(configuration) {
    tls internal

    @authelia path /authenticate /authenticate/*

    handle @authelia {
        reverse_proxy authelia:9091
    }

    handle {
        forward_auth authelia:9091 {
            uri /api/authz/forward-auth
            copy_headers Remote-User Remote-Groups Remote-Name Remote-Email
        }
    }
}

# N8N
{$N8N_HOSTNAME} {
    import configuration
    # For domains, Caddy will automatically use Let's Encrypt
    # For localhost/port addresses, HTTPS won't be enabled
    reverse_proxy n8n:5678
}

# OPENCLAW
{$OPENCLAW_HOSTNAME} {
    import configuration
    reverse_proxy openclaw-gateway:18789
}

# Open WebUI
{$WEBUI_HOSTNAME} {
    import configuration
    reverse_proxy open-webui:8080
}

# Flowise
{$FLOWISE_HOSTNAME} {
    import configuration
    reverse_proxy flowise:3001
}

# Langfuse
{$LANGFUSE_HOSTNAME} {
    import configuration
    reverse_proxy langfuse-web:3000
}

# Supabase
{$SUPABASE_HOSTNAME} {
    import configuration
    @supa_api path /rest/v1/* /auth/v1/* /realtime/v1/* /functions/v1/* /mcp /api/mcp

    handle @supa_api {
        reverse_proxy supabase-kong:8000
    }

    handle_path /storage/v1/* {
        import cors *
        reverse_proxy storage:5000 {
            header_up X-Forwarded-Prefix /{http.request.orig_uri.path.0}/{http.request.orig_uri.path.1}
        }
    }

    handle_path /goapi/* {
        reverse_proxy kong:8000
    }

    handle {
        reverse_proxy studio:3000
    }
}

# Neo4j
{$NEO4J_HOSTNAME} {
    import configuration
    reverse_proxy neo4j:7474
}

# SearXNG
{DISABLED_SEARXNG} {
    import configuration
    encode zstd gzip

    @api {
        path /config
        path /healthz
        path /stats/errors
        path /stats/checker
    }
    @search {
        path /search
    }
    @imageproxy {
        path /image_proxy
    }
    @static {
        path /static/*
    }

    header {
        # CSP (https://content-security-policy.com)
        Content-Security-Policy "upgrade-insecure-requests; default-src 'none'; script-src 'self'; style-src 'self' 'unsafe-inline'; form-action 'self' https://github.com/searxng/searxng/issues/new; font-src 'self'; frame-ancestors 'self'; base-uri 'self'; connect-src 'self' https://overpass-api.de; img-src * data:; frame-src https://www.youtube-nocookie.com https://player.vimeo.com https://www.dailymotion.com https://www.deezer.com https://www.mixcloud.com https://w.soundcloud.com https://embed.spotify.com;"
        # Disable some browser features
        Permissions-Policy "accelerometer=(),camera=(),geolocation=(),gyroscope=(),magnetometer=(),microphone=(),payment=(),usb=()"
        # Set referrer policy
        Referrer-Policy "no-referrer"
        # Force clients to use HTTPS
        Strict-Transport-Security "max-age=31536000"
        # Prevent MIME type sniffing from the declared Content-Type
        X-Content-Type-Options "nosniff"
        # X-Robots-Tag (comment to allow site indexing)
        X-Robots-Tag "noindex, noarchive, nofollow"
        # Remove "Server" header
        -Server
    }

    header @api {
        Access-Control-Allow-Methods "GET, OPTIONS"
        Access-Control-Allow-Origin "*"
    }

    route {
        # Cache policy
        header Cache-Control "max-age=0, no-store"
        header @search Cache-Control "max-age=5, private"
        header @imageproxy Cache-Control "max-age=604800, public"
        header @static Cache-Control "max-age=31536000, public, immutable"
    }

    # SearXNG (uWSGI)
    reverse_proxy searxng:8080 {
        header_up X-Forwarded-Port {http.request.port}
        header_up X-Real-IP {http.request.remote.host}
        # https://github.com/searx/searx-docker/issues/24
        header_up Connection "close"
    }
}
