2026-01-24 22:59:20 +00:00
|
|
|
use crate::api::rpc::RpcHandler;
|
|
|
|
|
use crate::config::Config;
|
2026-01-27 23:06:18 +00:00
|
|
|
use crate::state::StateManager;
|
2026-01-24 22:59:20 +00:00
|
|
|
use anyhow::Result;
|
2026-01-27 22:37:08 +00:00
|
|
|
use futures_util::{SinkExt, StreamExt};
|
2026-01-24 22:59:20 +00:00
|
|
|
use hyper::{Method, Request, Response, StatusCode};
|
2026-01-27 22:47:51 +00:00
|
|
|
use hyper_ws_listener::WsStream;
|
2026-01-24 22:59:20 +00:00
|
|
|
use std::sync::Arc;
|
2026-02-01 13:24:03 +00:00
|
|
|
use tokio::sync::broadcast;
|
2026-01-27 22:47:51 +00:00
|
|
|
use tokio_tungstenite::tungstenite::Message;
|
|
|
|
|
use tracing::{debug, info};
|
2026-01-24 22:59:20 +00:00
|
|
|
|
2026-02-14 16:44:20 +00:00
|
|
|
const CORS_ANY: &str = "*";
|
|
|
|
|
|
2026-01-24 22:59:20 +00:00
|
|
|
pub struct ApiHandler {
|
Update archipelago: API, auth, container, parmanode, performance, security
- API handler, RPC, and server updates
- Auth and coding rules
- Container data manager, dev orchestrator, health monitor, podman client
- Parmanode script runner
- Performance resource manager
- Security container policies and secrets manager
- Add build scripts and documentation
2026-01-27 22:27:17 +00:00
|
|
|
_config: Config,
|
2026-01-24 22:59:20 +00:00
|
|
|
rpc_handler: Arc<RpcHandler>,
|
2026-01-27 23:06:18 +00:00
|
|
|
state_manager: Arc<StateManager>,
|
2026-01-24 22:59:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ApiHandler {
|
2026-01-27 23:06:18 +00:00
|
|
|
pub async fn new(config: Config, state_manager: Arc<StateManager>) -> Result<Self> {
|
2026-01-24 22:59:20 +00:00
|
|
|
let rpc_handler = Arc::new(RpcHandler::new(config.clone()).await?);
|
|
|
|
|
|
|
|
|
|
Ok(Self {
|
Update archipelago: API, auth, container, parmanode, performance, security
- API handler, RPC, and server updates
- Auth and coding rules
- Container data manager, dev orchestrator, health monitor, podman client
- Parmanode script runner
- Performance resource manager
- Security container policies and secrets manager
- Add build scripts and documentation
2026-01-27 22:27:17 +00:00
|
|
|
_config: config,
|
2026-01-24 22:59:20 +00:00
|
|
|
rpc_handler,
|
2026-01-27 23:06:18 +00:00
|
|
|
state_manager,
|
2026-01-24 22:59:20 +00:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-24 23:20:54 +00:00
|
|
|
pub async fn handle_request(
|
|
|
|
|
&self,
|
Update archipelago: API, auth, container, parmanode, performance, security
- API handler, RPC, and server updates
- Auth and coding rules
- Container data manager, dev orchestrator, health monitor, podman client
- Parmanode script runner
- Performance resource manager
- Security container policies and secrets manager
- Add build scripts and documentation
2026-01-27 22:27:17 +00:00
|
|
|
req: Request<hyper::Body>,
|
|
|
|
|
) -> Result<Response<hyper::Body>> {
|
2026-01-24 23:20:54 +00:00
|
|
|
let path = req.uri().path().to_string();
|
|
|
|
|
let method = req.method().clone();
|
2026-01-27 22:37:08 +00:00
|
|
|
|
|
|
|
|
// WebSocket upgrade must be handled before consuming the body
|
|
|
|
|
if method == Method::GET && path == "/ws/db" {
|
2026-01-27 23:06:18 +00:00
|
|
|
return Self::handle_websocket(req, self.state_manager.clone()).await;
|
2026-01-27 22:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Convert body to bytes for non-WS routes
|
2026-01-24 22:59:20 +00:00
|
|
|
let (parts, body) = req.into_parts();
|
Update archipelago: API, auth, container, parmanode, performance, security
- API handler, RPC, and server updates
- Auth and coding rules
- Container data manager, dev orchestrator, health monitor, podman client
- Parmanode script runner
- Performance resource manager
- Security container policies and secrets manager
- Add build scripts and documentation
2026-01-27 22:27:17 +00:00
|
|
|
let body_bytes = hyper::body::to_bytes(body).await
|
|
|
|
|
.map_err(|e| anyhow::anyhow!("Failed to read body: {}", e))?;
|
|
|
|
|
let req_with_bytes = Request::from_parts(parts, hyper::Body::from(body_bytes));
|
2026-01-24 22:59:20 +00:00
|
|
|
|
|
|
|
|
debug!("{} {}", method, path);
|
|
|
|
|
|
2026-01-24 23:09:46 +00:00
|
|
|
match (method, path.as_str()) {
|
2026-01-27 22:37:08 +00:00
|
|
|
(Method::POST, "/rpc/v1") => self.rpc_handler.handle(req_with_bytes).await,
|
|
|
|
|
(Method::GET, "/health") => Ok(Response::builder()
|
|
|
|
|
.status(StatusCode::OK)
|
|
|
|
|
.body(hyper::Body::from("OK"))
|
|
|
|
|
.unwrap()),
|
2026-02-14 16:44:20 +00:00
|
|
|
(Method::GET, path) if path.starts_with("/api/container/logs") => {
|
|
|
|
|
Self::handle_container_logs_http(self.rpc_handler.clone(), path).await
|
|
|
|
|
}
|
|
|
|
|
(Method::GET, path) if path.starts_with("/proxy/lnd/") => {
|
|
|
|
|
Self::handle_lnd_proxy(path).await
|
|
|
|
|
}
|
2026-01-27 22:37:08 +00:00
|
|
|
_ => Ok(Response::builder()
|
|
|
|
|
.status(StatusCode::NOT_FOUND)
|
|
|
|
|
.body(hyper::Body::from("Not Found"))
|
|
|
|
|
.unwrap()),
|
2026-01-24 22:59:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-01-27 22:37:08 +00:00
|
|
|
|
2026-02-14 16:44:20 +00:00
|
|
|
async fn handle_container_logs_http(
|
|
|
|
|
rpc: Arc<RpcHandler>,
|
|
|
|
|
path: &str,
|
|
|
|
|
) -> Result<Response<hyper::Body>> {
|
|
|
|
|
let query = path
|
|
|
|
|
.strip_prefix("/api/container/logs")
|
|
|
|
|
.and_then(|s| s.strip_prefix('?'))
|
|
|
|
|
.unwrap_or("");
|
|
|
|
|
let params: std::collections::HashMap<String, String> =
|
|
|
|
|
query
|
|
|
|
|
.split('&')
|
|
|
|
|
.filter_map(|p| {
|
|
|
|
|
let mut it = p.splitn(2, '=');
|
|
|
|
|
let k = it.next()?.to_string();
|
|
|
|
|
let v = it.next()?.to_string();
|
|
|
|
|
Some((k, v))
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
let app_id = params.get("app_id").map(|s| s.as_str()).unwrap_or("lnd");
|
|
|
|
|
let lines = params
|
|
|
|
|
.get("lines")
|
|
|
|
|
.and_then(|s| s.parse::<u32>().ok())
|
|
|
|
|
.unwrap_or(200);
|
|
|
|
|
|
|
|
|
|
match rpc.get_container_logs_value(app_id, lines).await {
|
|
|
|
|
Ok(value) => {
|
|
|
|
|
let body = serde_json::json!({ "result": value });
|
|
|
|
|
let body_bytes = serde_json::to_vec(&body).unwrap_or_default();
|
|
|
|
|
Ok(Response::builder()
|
|
|
|
|
.status(StatusCode::OK)
|
|
|
|
|
.header("Content-Type", "application/json")
|
|
|
|
|
.header("Access-Control-Allow-Origin", CORS_ANY)
|
|
|
|
|
.body(hyper::Body::from(body_bytes))
|
|
|
|
|
.unwrap())
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
let body = serde_json::json!({ "error": e.to_string() });
|
|
|
|
|
let body_bytes = serde_json::to_vec(&body).unwrap_or_default();
|
|
|
|
|
Ok(Response::builder()
|
|
|
|
|
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
|
|
|
|
.header("Content-Type", "application/json")
|
|
|
|
|
.header("Access-Control-Allow-Origin", CORS_ANY)
|
|
|
|
|
.body(hyper::Body::from(body_bytes))
|
|
|
|
|
.unwrap())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn handle_lnd_proxy(path: &str) -> Result<Response<hyper::Body>> {
|
|
|
|
|
let suffix = path.strip_prefix("/proxy/lnd").unwrap_or("/");
|
|
|
|
|
let url = format!("http://127.0.0.1:8080{}", suffix);
|
|
|
|
|
match reqwest::get(&url).await {
|
|
|
|
|
Ok(resp) => {
|
|
|
|
|
let status = resp.status().as_u16();
|
|
|
|
|
let headers = resp.headers().clone();
|
|
|
|
|
let body = resp.bytes().await.unwrap_or_default();
|
|
|
|
|
let mut builder = Response::builder().status(status);
|
|
|
|
|
if let Some(ct) = headers.get("content-type") {
|
|
|
|
|
if let Ok(s) = ct.to_str() {
|
|
|
|
|
builder = builder.header("Content-Type", s);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
builder
|
|
|
|
|
.header("Access-Control-Allow-Origin", CORS_ANY)
|
|
|
|
|
.body(hyper::Body::from(body))
|
|
|
|
|
.map_err(|e| anyhow::anyhow!("response build: {}", e))
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
let body = serde_json::json!({ "error": e.to_string() });
|
|
|
|
|
let body_bytes = serde_json::to_vec(&body).unwrap_or_default();
|
|
|
|
|
Ok(Response::builder()
|
|
|
|
|
.status(StatusCode::BAD_GATEWAY)
|
|
|
|
|
.header("Content-Type", "application/json")
|
|
|
|
|
.header("Access-Control-Allow-Origin", CORS_ANY)
|
|
|
|
|
.body(hyper::Body::from(body_bytes))
|
|
|
|
|
.unwrap())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-27 22:37:08 +00:00
|
|
|
async fn handle_websocket(
|
|
|
|
|
req: Request<hyper::Body>,
|
2026-01-27 23:06:18 +00:00
|
|
|
state_manager: Arc<StateManager>,
|
2026-01-27 22:37:08 +00:00
|
|
|
) -> Result<Response<hyper::Body>> {
|
2026-01-27 22:47:51 +00:00
|
|
|
let (response, ws_fut_opt) = hyper_ws_listener::create_ws(req)
|
2026-01-27 22:37:08 +00:00
|
|
|
.map_err(|e| anyhow::anyhow!("WebSocket upgrade failed: {}", e))?;
|
|
|
|
|
|
2026-01-27 22:47:51 +00:00
|
|
|
// Spawn a task to hold the connection open if upgrade future exists
|
|
|
|
|
if let Some(ws_fut) = ws_fut_opt {
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
|
let ws_stream: WsStream = match ws_fut.await {
|
|
|
|
|
Ok(Ok(s)) => s,
|
|
|
|
|
Ok(Err(e)) => {
|
|
|
|
|
debug!("WebSocket handshake failed (hyper): {}", e);
|
|
|
|
|
return;
|
2026-01-27 22:37:08 +00:00
|
|
|
}
|
|
|
|
|
Err(e) => {
|
2026-01-27 22:47:51 +00:00
|
|
|
debug!("WebSocket task join failed: {}", e);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
info!("WebSocket /ws/db connected");
|
|
|
|
|
|
|
|
|
|
let (mut tx, mut rx) = ws_stream.split();
|
|
|
|
|
|
2026-01-27 23:06:18 +00:00
|
|
|
// Send initial data dump
|
|
|
|
|
let initial_msg = state_manager.get_initial_message().await;
|
|
|
|
|
if let Ok(json_msg) = serde_json::to_string(&initial_msg) {
|
|
|
|
|
if let Err(e) = tx.send(Message::Text(json_msg)).await {
|
|
|
|
|
debug!("Failed to send initial data: {}", e);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
debug!("Sent initial data dump at revision {}", initial_msg.rev);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-01 13:24:03 +00:00
|
|
|
// Subscribe to state updates
|
|
|
|
|
let mut state_rx = state_manager.subscribe();
|
|
|
|
|
|
2026-01-27 22:55:20 +00:00
|
|
|
// Send periodic pings to keep connection alive
|
|
|
|
|
let ping_interval = tokio::time::interval(tokio::time::Duration::from_secs(30));
|
|
|
|
|
tokio::pin!(ping_interval);
|
|
|
|
|
|
2026-02-01 13:24:03 +00:00
|
|
|
// Keep connection open and forward state updates to client
|
2026-01-27 22:55:20 +00:00
|
|
|
loop {
|
|
|
|
|
tokio::select! {
|
|
|
|
|
_ = ping_interval.tick() => {
|
|
|
|
|
if tx.send(Message::Ping(vec![])).await.is_err() {
|
|
|
|
|
debug!("Failed to send ping, connection likely closed");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2026-01-27 22:47:51 +00:00
|
|
|
}
|
2026-02-01 13:24:03 +00:00
|
|
|
// Forward state updates from broadcast channel to WebSocket
|
|
|
|
|
update = state_rx.recv() => {
|
|
|
|
|
match update {
|
|
|
|
|
Ok(msg) => {
|
|
|
|
|
if let Ok(json_msg) = serde_json::to_string(&msg) {
|
|
|
|
|
if let Err(e) = tx.send(Message::Text(json_msg)).await {
|
|
|
|
|
debug!("Failed to send state update: {}", e);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
debug!("Sent state update at revision {}", msg.rev);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Err(broadcast::error::RecvError::Lagged(skipped)) => {
|
|
|
|
|
debug!("Client lagged behind, skipped {} messages", skipped);
|
|
|
|
|
// Continue receiving - the client will get the next update
|
|
|
|
|
}
|
|
|
|
|
Err(broadcast::error::RecvError::Closed) => {
|
|
|
|
|
debug!("Broadcast channel closed");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-27 22:55:20 +00:00
|
|
|
msg = rx.next() => {
|
|
|
|
|
match msg {
|
|
|
|
|
Some(Ok(Message::Close(_))) => break,
|
|
|
|
|
Some(Ok(Message::Pong(_))) => {
|
|
|
|
|
debug!("Received pong");
|
|
|
|
|
}
|
|
|
|
|
Some(Ok(Message::Ping(data))) => {
|
|
|
|
|
let _ = tx.send(Message::Pong(data)).await;
|
|
|
|
|
}
|
|
|
|
|
Some(Ok(_)) => {}
|
|
|
|
|
Some(Err(e)) => {
|
|
|
|
|
debug!("WebSocket stream error: {}", e);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
None => break,
|
|
|
|
|
}
|
2026-01-27 22:47:51 +00:00
|
|
|
}
|
2026-01-27 22:37:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-01-27 22:47:51 +00:00
|
|
|
info!("WebSocket /ws/db disconnected");
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-01-27 22:37:08 +00:00
|
|
|
|
|
|
|
|
Ok(response)
|
|
|
|
|
}
|
2026-01-24 22:59:20 +00:00
|
|
|
}
|