mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-19 15:21:23 +02:00
Proto selection UI/models
This commit is contained in:
@@ -6,7 +6,6 @@ use hyper_rustls::HttpsConnector;
|
||||
use prost_reflect::DescriptorPool;
|
||||
pub use prost_reflect::DynamicMessage;
|
||||
use serde_json::Deserializer;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio_stream::wrappers::ReceiverStream;
|
||||
use tokio_stream::StreamExt;
|
||||
use tonic::body::BoxBody;
|
||||
@@ -15,6 +14,7 @@ use tonic::{IntoRequest, IntoStreamingRequest, Streaming};
|
||||
|
||||
use crate::codec::DynamicCodec;
|
||||
use crate::proto::{fill_pool, method_desc_to_path};
|
||||
use crate::{json_schema, MethodDefinition, ServiceDefinition};
|
||||
|
||||
type Result<T> = std::result::Result<T, String>;
|
||||
|
||||
@@ -145,25 +145,61 @@ impl GrpcConnection {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct GrpcManager {
|
||||
pub struct GrpcHandle {
|
||||
connections: HashMap<String, GrpcConnection>,
|
||||
pub send: mpsc::Sender<String>,
|
||||
pub recv: mpsc::Receiver<String>,
|
||||
pools: HashMap<String, DescriptorPool>,
|
||||
}
|
||||
|
||||
impl Default for GrpcManager {
|
||||
impl Default for GrpcHandle {
|
||||
fn default() -> Self {
|
||||
let (send, recv) = mpsc::channel(100);
|
||||
let connections = HashMap::new();
|
||||
Self {
|
||||
connections,
|
||||
send,
|
||||
recv,
|
||||
}
|
||||
let pools = HashMap::new();
|
||||
Self { connections, pools }
|
||||
}
|
||||
}
|
||||
|
||||
impl GrpcManager {
|
||||
impl GrpcHandle {
|
||||
pub async fn clean_reflect(&mut self, uri: &Uri) -> Result<Vec<ServiceDefinition>> {
|
||||
self.pools.remove(&uri.to_string());
|
||||
self.reflect(uri).await
|
||||
}
|
||||
|
||||
pub async fn reflect(&mut self, uri: &Uri) -> Result<Vec<ServiceDefinition>> {
|
||||
let pool = match self.pools.get(&uri.to_string()) {
|
||||
Some(p) => p.clone(),
|
||||
None => {
|
||||
let (pool, _) = fill_pool(uri).await?;
|
||||
self.pools.insert(uri.to_string(), pool.clone());
|
||||
pool
|
||||
}
|
||||
};
|
||||
|
||||
let result =
|
||||
pool.services()
|
||||
.map(|s| {
|
||||
let mut def = ServiceDefinition {
|
||||
name: s.full_name().to_string(),
|
||||
methods: vec![],
|
||||
};
|
||||
for method in s.methods() {
|
||||
let input_message = method.input();
|
||||
def.methods.push(MethodDefinition {
|
||||
name: method.name().to_string(),
|
||||
server_streaming: method.is_server_streaming(),
|
||||
client_streaming: method.is_client_streaming(),
|
||||
schema: serde_json::to_string_pretty(
|
||||
&json_schema::message_to_json_schema(&pool, input_message),
|
||||
)
|
||||
.unwrap(),
|
||||
})
|
||||
}
|
||||
def
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub async fn server_streaming(
|
||||
&mut self,
|
||||
id: &str,
|
||||
|
||||
Reference in New Issue
Block a user