Skip to main content

linkrs_server/
web.rs

1//! Web Management Module
2//!
3//! Provides Web management interface API for Linkrs frontend:
4//! - Query history management
5//! - Query favorites management
6//! - Extended Schema management
7//! - Data browsing
8//! - Graph data queries
9
10pub mod error;
11pub mod handlers;
12pub mod middleware;
13pub mod models;
14pub mod services;
15pub mod storage;
16
17use axum::{middleware as axum_middleware, Router};
18use std::sync::Arc;
19
20use crate::http::state::AppState;
21use crate::storage::{
22    StorageClient, StorageOperationContextOps, StorageSchemaContextOps, StorageSyncContextOps,
23};
24
25use self::storage::SqliteStorage;
26
27/// Web module state
28#[derive(Clone)]
29pub struct WebState<
30    S: StorageClient
31        + StorageSchemaContextOps
32        + StorageSyncContextOps
33        + StorageOperationContextOps
34        + Clone
35        + Send
36        + Sync
37        + 'static,
38> {
39    /// Metadata storage
40    pub metadata_storage: Arc<SqliteStorage>,
41    /// Core application state
42    pub core_state: AppState<S>,
43}
44
45impl<
46        S: StorageClient
47            + StorageSchemaContextOps
48            + StorageSyncContextOps
49            + StorageOperationContextOps
50            + Clone
51            + Send
52            + Sync
53            + 'static,
54    > WebState<S>
55{
56    pub async fn new(storage_path: &str, core_state: AppState<S>) -> Result<Self, error::WebError> {
57        let metadata_storage = Arc::new(SqliteStorage::new(storage_path).await?);
58
59        Ok(Self {
60            metadata_storage,
61            core_state,
62        })
63    }
64}
65
66/// Create Web management router
67pub fn create_router<
68    S: StorageClient
69        + StorageSchemaContextOps
70        + StorageSyncContextOps
71        + StorageOperationContextOps
72        + Clone
73        + Send
74        + Sync
75        + 'static,
76>(
77    web_state: WebState<S>,
78) -> Router {
79    // Build routes with shared state
80    let queries_routes = handlers::metadata::create_routes();
81    let schema_routes = handlers::schema_ext::create_routes();
82    let data_routes = handlers::data_browser::create_routes();
83    let graph_routes = handlers::graph_data::create_routes();
84
85    Router::new()
86        .nest("/v1/queries", queries_routes)
87        .nest("/v1/schema", schema_routes)
88        .nest("/v1/data", data_routes)
89        .nest("/v1/graph", graph_routes)
90        .layer(axum_middleware::from_fn_with_state(
91            web_state.clone(),
92            middleware::web_auth_middleware,
93        ))
94        .with_state(web_state)
95}