# CONDUWUIT
we2.ee, we2.ee:8448 {
    log {
        output stdout
        format console
        level INFO
    }

    # About redirect
    handle /about {
        redir https://sij.law/we2ee/ permanent
    }

    # Privacy Policy redirect
    handle /privacy {
        redir https://sij.law/we2ee-privacy/ permanent
    }

    # Terms redirect
    handle /terms {
        redir https://sij.law/we2ee-terms/ permanent
    }

    # Anysync redirect
    handle /anysync {
        redir https://sij.law/anysync/ permanent
    }


    # Block leaving the mandatory announcements room
    handle /_matrix/client/v3/rooms/!H8Eys4SOa7C5CxBY3O:we2.ee/leave {
        header Content-Type "application/json"
        respond 403 {
            body `{"errcode":"M_FORBIDDEN","error":"Leaving the announcements room is not permitted"}`
        }
    }

    # Handle Matrix-style room redirects
    @matrix_local_room {
        path_regexp ^/@@([^:]+)$
    }
    redir @matrix_local_room https://matrix.to/#/%23{re.1}:we2.ee permanent

    @matrix_remote_room {
        path_regexp ^/@@([^:]+):([^/]+)$
    }
    redir @matrix_remote_room https://matrix.to/#/%23{re.1}:{re.2} permanent

    # Handle Matrix-style user redirects
    @matrix_local_user {
        path_regexp ^/@([^:]+)$
    }
    redir @matrix_local_user https://matrix.to/#/@{re.1}:we2.ee permanent

    @matrix_remote_user {
        path_regexp ^/@([^:]+):([^/]+)$
    }
    redir @matrix_remote_user https://matrix.to/#/@{re.1}:{re.2} permanent

    # Maubot (separate bot runner)
    handle /_matrix/maubot/* {
        reverse_proxy localhost:29316
    }

    # Handle Conduwuit homeserver
    handle /_matrix/* {
        reverse_proxy localhost:8008
    }

    # Handle federation
    handle /.well-known/matrix/server {
        header Access-Control-Allow-Origin "*"
        header Content-Type "application/json"
        respond `{"m.server": "we2.ee:8448"}`
    }

    # Handle client discovery
    handle /.well-known/matrix/client {
        header Access-Control-Allow-Origin "*"
        header Content-Type "application/json"
        respond `{
            "m.homeserver": {"base_url": "https://we2.ee/"},
            "org.matrix.msc3575.proxy": {"url": "https://we2.ee/"}
        }`
    }

    # Handle MSC1929 Admin Contact Information
    handle /.well-known/matrix/support {
        header Access-Control-Allow-Origin "*"
        header Content-Type "application/json"
        respond `{
            "contacts": [
                {
                    "matrix_id": "@sij:we2.ee",
                    "email_address": "sij@sij.law",
                    "role": "m.role.admin"
                }
            ],
            "support_page": "https://we2.ee/about"
        }`
    }

    # Handle Element webUI
    handle {
        reverse_proxy localhost:8637
    }

    tls {
        dns cloudflare {env.CLOUDFLARE_API_TOKEN}
    }
}

# Anytype Sync
sync.we2.ee {
    # Tree nodes
    handle_path /tree/* {
        reverse_proxy 127.0.0.1:1001 127.0.0.1:1002 127.0.0.1:1003
    }

    # Coordinator
    handle_path /coordinator/* {
        reverse_proxy 127.0.0.1:1004
    }

    # File node
    handle_path /file/* {
        reverse_proxy 127.0.0.1:1005
    }

    # Consensus node
    handle_path /consensus/* {
        reverse_proxy 127.0.0.1:1006
    }
}

# PrivateBin
txt.we2.ee {
    reverse_proxy localhost:8987
    tls {
        dns cloudflare {env.CLOUDFLARE_API_TOKEN}
    }
}

# Cinny
cinny.we2.ee {
    reverse_proxy localhost:3004
    tls {
        dns cloudflare {env.CLOUDFLARE_API_TOKEN}
    }
}