vibe_graph_api/
lib.rs

1//! REST + WebSocket API service for Vibe-Graph.
2//!
3//! This crate provides a clean API layer that can be consumed by any frontend.
4//! It separates data serving from visualization concerns.
5//!
6//! ## Endpoints
7//!
8//! - `GET /api/health` - Health check with node/edge counts
9//! - `GET /api/graph` - Full SourceCodeGraph JSON
10//! - `GET /api/graph/nodes` - Nodes only
11//! - `GET /api/graph/edges` - Edges only
12//! - `GET /api/graph/metadata` - Graph metadata
13//! - `GET /api/git/changes` - Current git change snapshot
14//! - `GET /api/ws` - WebSocket for real-time updates
15
16mod routes;
17mod types;
18mod ws;
19
20pub use routes::create_api_router;
21pub use types::{ApiState, WsClientMessage, WsServerMessage};
22
23use std::sync::Arc;
24use tokio::sync::{broadcast, RwLock};
25use vibe_graph_core::{GitChangeSnapshot, SourceCodeGraph};
26
27/// Create a new API state with the given graph.
28pub fn create_api_state(graph: SourceCodeGraph) -> Arc<ApiState> {
29    let (tx, _) = broadcast::channel(100);
30    Arc::new(ApiState {
31        graph: Arc::new(RwLock::new(graph)),
32        git_changes: Arc::new(RwLock::new(GitChangeSnapshot::default())),
33        tx,
34    })
35}
36
37/// Create a new API state with the given graph and git changes.
38pub fn create_api_state_with_changes(
39    graph: SourceCodeGraph,
40    git_changes: GitChangeSnapshot,
41) -> Arc<ApiState> {
42    let (tx, _) = broadcast::channel(100);
43    Arc::new(ApiState {
44        graph: Arc::new(RwLock::new(graph)),
45        git_changes: Arc::new(RwLock::new(git_changes)),
46        tx,
47    })
48}