| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
use serde_json::Value; |
| 13 |
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader}; |
| 14 |
|
| 15 |
use super::ServeConfig; |
| 16 |
use super::protocol::{JsonRpcResponse, codes}; |
| 17 |
use crate::error::Result; |
| 18 |
use crate::tool::ToolRegistry; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
pub async fn serve(registry: ToolRegistry, config: ServeConfig) -> Result<()> { |
| 23 |
let mut lines = BufReader::new(tokio::io::stdin()).lines(); |
| 24 |
let mut stdout = tokio::io::stdout(); |
| 25 |
|
| 26 |
while let Some(line) = lines.next_line().await? { |
| 27 |
let line = line.trim(); |
| 28 |
if line.is_empty() { |
| 29 |
continue; |
| 30 |
} |
| 31 |
let response = match serde_json::from_str(line) { |
| 32 |
Ok(req) => { |
| 33 |
|
| 34 |
|
| 35 |
super::dispatch( |
| 36 |
®istry, |
| 37 |
config.resources.as_ref(), |
| 38 |
config.projection, |
| 39 |
config.grants.as_ref(), |
| 40 |
None, |
| 41 |
req, |
| 42 |
) |
| 43 |
.await |
| 44 |
} |
| 45 |
Err(e) => Some(JsonRpcResponse::err( |
| 46 |
Value::Null, |
| 47 |
codes::PARSE_ERROR, |
| 48 |
e.to_string(), |
| 49 |
)), |
| 50 |
}; |
| 51 |
|
| 52 |
if let Some(response) = response { |
| 53 |
let mut buf = serde_json::to_vec(&response)?; |
| 54 |
buf.push(b'\n'); |
| 55 |
stdout.write_all(&buf).await?; |
| 56 |
stdout.flush().await?; |
| 57 |
} |
| 58 |
} |
| 59 |
Ok(()) |
| 60 |
} |
| 61 |
|