|
| 1 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +// Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk> |
| 3 | + |
| 4 | +//! Live BoJ-server connection module (async). |
| 5 | +//! |
| 6 | +//! Connects to the BoJ-server gateway at localhost:7700 for real |
| 7 | +//! cartridge invocation, health checks, and topology queries. |
| 8 | +//! |
| 9 | +//! This module parallels `boj::commands` but uses async reqwest via the |
| 10 | +//! shared [`http_client`] module instead of blocking calls. Panels that |
| 11 | +//! need non-blocking BoJ access (v0.2.0+) should prefer these commands. |
| 12 | +
|
| 13 | +use crate::http_client::{self, ServiceEndpoint}; |
| 14 | +use serde::{Deserialize, Serialize}; |
| 15 | +use serde_json::json; |
| 16 | + |
| 17 | +// panic-attack:allow insecure-protocol — localhost dev endpoint |
| 18 | +/// Default BoJ-server base URL. |
| 19 | +const DEFAULT_BOJ_URL: &str = "http://localhost:7700"; |
| 20 | + |
| 21 | +/// Build the BoJ endpoint, respecting the `BOJ_URL` environment variable |
| 22 | +/// if set, otherwise falling back to [`DEFAULT_BOJ_URL`]. |
| 23 | +fn boj_endpoint() -> ServiceEndpoint { |
| 24 | + let url = std::env::var("BOJ_URL").unwrap_or_else(|_| DEFAULT_BOJ_URL.to_string()); |
| 25 | + ServiceEndpoint::new(&url) |
| 26 | +} |
| 27 | + |
| 28 | +/// BoJ health check response shape. |
| 29 | +/// |
| 30 | +/// Mirrors the JSON envelope returned by `GET /health` on the BoJ server. |
| 31 | +/// Optional fields are absent when the server is freshly started or running |
| 32 | +/// in minimal mode. |
| 33 | +#[derive(Debug, Deserialize, Serialize)] |
| 34 | +pub struct BojHealth { |
| 35 | + /// Top-level status string (e.g. "ok", "degraded"). |
| 36 | + pub status: String, |
| 37 | + /// Semantic version of the running BoJ server. |
| 38 | + pub version: Option<String>, |
| 39 | + /// Number of loaded cartridges. |
| 40 | + pub cartridges: Option<usize>, |
| 41 | + /// Human-readable uptime (e.g. "2h 14m"). |
| 42 | + pub uptime: Option<String>, |
| 43 | +} |
| 44 | + |
| 45 | +/// Check BoJ-server health (async). |
| 46 | +/// |
| 47 | +/// `GET /health` — returns the full health envelope as a JSON string. |
| 48 | +#[tauri::command] |
| 49 | +pub async fn boj_live_health() -> Result<String, String> { |
| 50 | + let endpoint = boj_endpoint(); |
| 51 | + let health: BojHealth = http_client::get_json(&endpoint, "/health") |
| 52 | + .await |
| 53 | + .map_err(|e| format!("BoJ server unreachable: {}", e))?; |
| 54 | + serde_json::to_string(&health).map_err(|e| format!("Serialization failed: {}", e)) |
| 55 | +} |
| 56 | + |
| 57 | +/// List available cartridges from BoJ-server (async). |
| 58 | +/// |
| 59 | +/// `GET /cartridges` — returns the cartridge array as a JSON string. |
| 60 | +#[tauri::command] |
| 61 | +pub async fn boj_live_cartridges() -> Result<String, String> { |
| 62 | + let endpoint = boj_endpoint(); |
| 63 | + let cartridges: serde_json::Value = http_client::get_json(&endpoint, "/cartridges") |
| 64 | + .await |
| 65 | + .map_err(|e| format!("Failed to list cartridges: {}", e))?; |
| 66 | + serde_json::to_string(&cartridges).map_err(|e| format!("Serialization failed: {}", e)) |
| 67 | +} |
| 68 | + |
| 69 | +/// Invoke a BoJ cartridge tool (async). |
| 70 | +/// |
| 71 | +/// `POST /cartridges/{cartridge}/invoke` — sends `{"tool": ..., "args": ...}` |
| 72 | +/// and returns the invocation result as a JSON string. |
| 73 | +#[tauri::command] |
| 74 | +pub async fn boj_live_invoke( |
| 75 | + cartridge: String, |
| 76 | + tool: String, |
| 77 | + params: String, |
| 78 | +) -> Result<String, String> { |
| 79 | + let endpoint = boj_endpoint(); |
| 80 | + let parsed_params: serde_json::Value = |
| 81 | + serde_json::from_str(¶ms).unwrap_or(json!({})); |
| 82 | + let body = json!({ |
| 83 | + "tool": tool, |
| 84 | + "args": parsed_params, |
| 85 | + }); |
| 86 | + let result: serde_json::Value = http_client::post_json( |
| 87 | + &endpoint, |
| 88 | + &format!("/cartridges/{}/invoke", cartridge), |
| 89 | + &body, |
| 90 | + ) |
| 91 | + .await |
| 92 | + .map_err(|e| format!("Cartridge invoke failed: {}", e))?; |
| 93 | + serde_json::to_string(&result).map_err(|e| format!("Serialization failed: {}", e)) |
| 94 | +} |
| 95 | + |
| 96 | +/// Get BoJ topology / cartridge dependency graph (async). |
| 97 | +/// |
| 98 | +/// `GET /topology` — returns the topology matrix as a JSON string. |
| 99 | +#[tauri::command] |
| 100 | +pub async fn boj_live_topology() -> Result<String, String> { |
| 101 | + let endpoint = boj_endpoint(); |
| 102 | + let topology: serde_json::Value = http_client::get_json(&endpoint, "/topology") |
| 103 | + .await |
| 104 | + .map_err(|e| format!("Failed to get topology: {}", e))?; |
| 105 | + serde_json::to_string(&topology).map_err(|e| format!("Serialization failed: {}", e)) |
| 106 | +} |
| 107 | + |
| 108 | +/// Check if BoJ-server is reachable (async health probe). |
| 109 | +/// |
| 110 | +/// Returns `{"reachable": bool, "endpoint": "..."}` as a JSON string. |
| 111 | +/// Panels use this for connection-dot indicators in the panel bar. |
| 112 | +#[tauri::command] |
| 113 | +pub async fn boj_live_check() -> Result<String, String> { |
| 114 | + let endpoint = boj_endpoint(); |
| 115 | + let reachable = http_client::check_health(&endpoint, "/health").await?; |
| 116 | + let url = std::env::var("BOJ_URL").unwrap_or_else(|_| DEFAULT_BOJ_URL.to_string()); |
| 117 | + Ok(json!({ |
| 118 | + "reachable": reachable, |
| 119 | + "endpoint": url, |
| 120 | + }) |
| 121 | + .to_string()) |
| 122 | +} |
| 123 | + |
| 124 | +#[cfg(test)] |
| 125 | +mod tests { |
| 126 | + use super::*; |
| 127 | + |
| 128 | + /// Verify the endpoint builder respects BOJ_URL and strips trailing slashes. |
| 129 | + #[test] |
| 130 | + fn boj_endpoint_default_and_override() { |
| 131 | + std::env::remove_var("BOJ_URL"); |
| 132 | + let ep = boj_endpoint(); |
| 133 | + assert_eq!(ep.base_url, DEFAULT_BOJ_URL); |
| 134 | + |
| 135 | + std::env::set_var("BOJ_URL", "http://boj.local:9999/v2/"); |
| 136 | + let ep2 = boj_endpoint(); |
| 137 | + assert_eq!(ep2.base_url, "http://boj.local:9999/v2"); |
| 138 | + std::env::remove_var("BOJ_URL"); |
| 139 | + } |
| 140 | +} |
0 commit comments