Skip to content

armgit / xerj

Public Imported from GitHub
0

AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.

9.0 KB
Raw Blame
//! Public router — Xerj Console exposes a *complete* HTTP surface that any
//! container can mount as a peer to other axum routers.  In the v1.0
//! bundled binary, `xerj-server` brings up this router alongside the
//! engine's ES-compat / native routers via `Router::merge` so they
//! share one TCP listener.  When Xerj Console is extracted post-v1.0 it ships
//! with this same router as the binary's main entry point.
//!
//! Surface owned by this crate:
//!
//! ```text
//! /_xerj-console                 redirect → /_xerj-console/
//! /_xerj-console/                bundled SPA index.html
//! /_xerj-console/setup           bundled setup.html (magic-link claim)
//! /_xerj-console/login           bundled login.html (returning user)
//! /_xerj-console/{path*}         bundled SPA assets (CSS, JS, images, fonts)
//! /_xerj-console/api/v1/...      API endpoints (auth, prefs, dashboards, …)
//! ```
//!
//! ## Why one router, not two?
//!
//! Mounting the SPA assets and the API as one Router ensures Xerj Console
//! owns the full `/_xerj-console/*` namespace exclusively — no risk that the
//! engine's ES-compat router accidentally serves a 404 page over an
//! asset request, or vice versa.  The engine layer enforces its own
//! `Authorization: Bearer` policy on its routes only; this router
//! enforces its session-cookie policy on its routes only.

use axum::{
    routing::{delete, get, patch, post},
    Router,
};

use crate::{
    auth, cluster, dashboards, data_sources, graph, knowledge, prefs, spa, state::ConsoleState,
    views,
};

/// Build the full Xerj Console router. Mount at the root of an axum Router
/// (it owns the `/_xerj-console/*` prefix internally):
///
/// ```ignore
/// let merged = engine_router.merge(xerj_console_api::xerj_console_router(state));
/// axum::serve(listener, merged).await?;
/// ```
pub fn xerj_console_router(state: ConsoleState) -> Router {
    Router::new()
        // ── Bundled SPA assets (index, setup, login, css, js, fonts) ───────
        .route("/_xerj-console", get(spa::xerj_console_redirect))
        .route("/_xerj-console/", get(spa::xerj_console_index))
        .route("/_xerj-console/*rest", get(spa::xerj_console_asset))
        // ── API: cluster awareness ─────────────────────────────────────────
        .route("/_xerj-console/api/v1/cluster/info", get(cluster::info))
        // ── API: auth bootstrap (unauthenticated) ──────────────────────────
        .route(
            "/_xerj-console/api/v1/auth/magic/redeem",
            post(auth::magic::redeem),
        )
        // ── API: invite issue (owner/admin passkey session required) ───────
        // A bearer API token is 403. See `PasskeySession`.
        .route(
            "/_xerj-console/api/v1/auth/magic/issue",
            post(auth::magic::issue),
        )
        .route(
            "/_xerj-console/api/v1/auth/passkey/begin",
            post(auth::passkey::begin),
        )
        .route(
            "/_xerj-console/api/v1/auth/passkey/finish",
            post(auth::passkey::finish),
        )
        .route(
            "/_xerj-console/api/v1/auth/login/begin",
            post(auth::login::begin),
        )
        .route(
            "/_xerj-console/api/v1/auth/login/finish",
            post(auth::login::finish),
        )
        // ── API: session-protected ─────────────────────────────────────────
        // `PasskeySession` (bearer token → 403): logout, passkey delete,
        // api-token create and revoke. List routes and every data route
        // below accept a bearer token. See `auth::sessions::PasskeySession`.
        .route(
            "/_xerj-console/api/v1/auth/logout",
            post(auth::login::logout),
        )
        .route("/_xerj-console/api/v1/me", get(auth::me::me))
        .route(
            "/_xerj-console/api/v1/auth/passkeys",
            get(auth::me::list_passkeys),
        )
        .route(
            "/_xerj-console/api/v1/auth/passkeys/:id",
            delete(auth::me::delete_passkey),
        )
        .route(
            "/_xerj-console/api/v1/auth/api-tokens",
            get(auth::tokens::list).post(auth::tokens::create),
        )
        .route(
            "/_xerj-console/api/v1/auth/api-tokens/:id",
            delete(auth::tokens::revoke),
        )
        .route(
            "/_xerj-console/api/v1/prefs",
            get(prefs::get).put(prefs::put),
        )
        .route(
            "/_xerj-console/api/v1/dashboards",
            get(dashboards::list).post(dashboards::create),
        )
        // Static `_bulk` sits beside the `:id` param route below; matchit
        // gives the static segment priority, so a POST to `_bulk` never
        // lands on the `:id` handler.
        .route(
            "/_xerj-console/api/v1/dashboards/_bulk",
            post(dashboards::bulk),
        )
        .route(
            "/_xerj-console/api/v1/dashboards/:id",
            get(dashboards::get_one)
                .put(dashboards::replace)
                .delete(dashboards::delete),
        )
        .route(
            "/_xerj-console/api/v1/dashboards/:id",
            patch(dashboards::patch),
        )
        .route(
            "/_xerj-console/api/v1/views",
            get(views::list).post(views::create),
        )
        .route(
            "/_xerj-console/api/v1/views/:id",
            get(views::get_one).delete(views::delete),
        )
        .route(
            "/_xerj-console/api/v1/data-sources/connections",
            get(data_sources::list_connections),
        )
        .route(
            "/_xerj-console/api/v1/data-sources/connections/:id/indices",
            get(data_sources::list_indices),
        )
        .route(
            "/_xerj-console/api/v1/data-sources/connections/:id/indices/:name/fields",
            get(data_sources::list_fields),
        )
        .route(
            "/_xerj-console/api/v1/data-sources/connections/:id/indices/:name/search",
            post(data_sources::search),
        )
        // ── API: the knowledge surface (session-authorized) ─
        // One read that answers what a person asks the moment indexing
        // finishes: how large, which data, what can I do now. See
        // src/knowledge.rs.
        .route("/_xerj-console/api/v1/knowledge", get(knowledge::knowledge))
        // ── API: Second-Brain graph reads (session-authorized, issue #936) ─
        // The data plane's /_graph/* routes stay credential-only; these are
        // the console's own role-gated read paths over the same engine
        // traversals. See src/graph.rs for the authorization rule.
        .route("/_xerj-console/api/v1/graph/brains", get(graph::brains))
        .route("/_xerj-console/api/v1/graph/:brain/ego", get(graph::ego))
        .route(
            "/_xerj-console/api/v1/graph/:brain/overview",
            get(graph::overview),
        )
        .route(
            "/_xerj-console/api/v1/graph/:brain/edges/_search",
            post(graph::edges_search),
        )
        .route(
            "/_xerj-console/api/v1/graph/:brain/nodes/_search",
            post(graph::nodes_search),
        )
        .with_state(state)
}

/// Routes this crate currently exposes. Used by tests so an accidental
/// drop is caught at CI.
#[doc(hidden)]
pub fn known_routes() -> &'static [&'static str] {
    &[
        "/_xerj-console",
        "/_xerj-console/",
        "/_xerj-console/*rest",
        "/_xerj-console/api/v1/cluster/info",
        "/_xerj-console/api/v1/auth/magic/redeem",
        "/_xerj-console/api/v1/auth/magic/issue",
        "/_xerj-console/api/v1/auth/passkey/begin",
        "/_xerj-console/api/v1/auth/passkey/finish",
        "/_xerj-console/api/v1/auth/login/begin",
        "/_xerj-console/api/v1/auth/login/finish",
        "/_xerj-console/api/v1/auth/logout",
        "/_xerj-console/api/v1/me",
        "/_xerj-console/api/v1/auth/passkeys",
        "/_xerj-console/api/v1/auth/passkeys/:id",
        "/_xerj-console/api/v1/auth/api-tokens",
        "/_xerj-console/api/v1/auth/api-tokens/:id",
        "/_xerj-console/api/v1/prefs",
        "/_xerj-console/api/v1/dashboards",
        "/_xerj-console/api/v1/dashboards/_bulk",
        "/_xerj-console/api/v1/dashboards/:id",
        "/_xerj-console/api/v1/views",
        "/_xerj-console/api/v1/views/:id",
        "/_xerj-console/api/v1/data-sources/connections",
        "/_xerj-console/api/v1/data-sources/connections/:id/indices",
        "/_xerj-console/api/v1/data-sources/connections/:id/indices/:name/fields",
        "/_xerj-console/api/v1/data-sources/connections/:id/indices/:name/search",
        "/_xerj-console/api/v1/knowledge",
        "/_xerj-console/api/v1/graph/brains",
        "/_xerj-console/api/v1/graph/:brain/ego",
        "/_xerj-console/api/v1/graph/:brain/overview",
        "/_xerj-console/api/v1/graph/:brain/edges/_search",
        "/_xerj-console/api/v1/graph/:brain/nodes/_search",
    ]
}