use crate::api::rpc::RpcHandler; use crate::config::Config; use anyhow::Result; use hyper::{Method, Request, Response, StatusCode}; use std::sync::Arc; use tracing::debug; pub struct ApiHandler { _config: Config, rpc_handler: Arc, // Add other handlers here (websocket, static files, etc.) } impl ApiHandler { pub async fn new(config: Config) -> Result { let rpc_handler = Arc::new(RpcHandler::new(config.clone()).await?); Ok(Self { _config: config, rpc_handler, }) } pub async fn handle_request( &self, req: Request, ) -> Result> { // Extract path and method before consuming req let path = req.uri().path().to_string(); let method = req.method().clone(); // Convert body to bytes let (parts, body) = req.into_parts(); let body_bytes = hyper::body::to_bytes(body).await .map_err(|e| anyhow::anyhow!("Failed to read body: {}", e))?; // Reconstruct request with body as Bytes for RPC handler let req_with_bytes = Request::from_parts(parts, hyper::Body::from(body_bytes)); debug!("{} {}", method, path); // Route requests match (method, path.as_str()) { (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()) } } } }