2026-01-24 22:59:20 +00:00
|
|
|
use crate::api::rpc::RpcHandler;
|
|
|
|
|
use crate::config::Config;
|
|
|
|
|
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:37:08 +00:00
|
|
|
use hyper_tungstenite::tungstenite::Message;
|
2026-01-24 22:59:20 +00:00
|
|
|
use std::sync::Arc;
|
2026-01-27 22:37:08 +00:00
|
|
|
use tracing::{debug, error, info};
|
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>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ApiHandler {
|
|
|
|
|
pub async fn new(config: Config) -> Result<Self> {
|
|
|
|
|
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-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" {
|
|
|
|
|
return Self::handle_websocket(req).await;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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()),
|
|
|
|
|
_ => 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
|
|
|
|
|
|
|
|
async fn handle_websocket(
|
|
|
|
|
req: Request<hyper::Body>,
|
|
|
|
|
) -> Result<Response<hyper::Body>> {
|
|
|
|
|
if !hyper_tungstenite::is_upgrade_request(&req) {
|
|
|
|
|
return Ok(Response::builder()
|
|
|
|
|
.status(StatusCode::BAD_REQUEST)
|
|
|
|
|
.body(hyper::Body::from("Expected WebSocket upgrade"))?);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let (response, ws_fut) = hyper_tungstenite::upgrade(req, None)
|
|
|
|
|
.map_err(|e| anyhow::anyhow!("WebSocket upgrade failed: {}", e))?;
|
|
|
|
|
|
|
|
|
|
// Spawn a task to hold the connection open and avoid EPIPE when the UI connects
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
|
let mut ws_stream = match ws_fut.await {
|
|
|
|
|
Ok(s) => s,
|
|
|
|
|
Err(e) => {
|
|
|
|
|
error!("WebSocket handshake failed: {}", e);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
info!("WebSocket /ws/db connected");
|
|
|
|
|
// Keep connection open; UI may send/receive JSON patches. For now just accept and ignore.
|
|
|
|
|
while let Some(msg) = ws_stream.next().await {
|
|
|
|
|
match msg {
|
|
|
|
|
Ok(Message::Close(_)) => break,
|
|
|
|
|
Ok(Message::Ping(data)) => {
|
|
|
|
|
let _ = ws_stream.send(Message::Pong(data)).await;
|
|
|
|
|
}
|
|
|
|
|
Ok(_) => {}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
debug!("WebSocket stream error: {}", e);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Ok(response)
|
|
|
|
|
}
|
2026-01-24 22:59:20 +00:00
|
|
|
}
|