1mod cli;
2
3mod plugin_extension {
4 tonic::include_proto!("plugin_extension");
5}
6
7pub use plugin_extension::{
8 Reply, ReplyStatus, Request,
9 plugin_extension_client::PluginExtensionClient,
10 plugin_extension_server::{PluginExtension, PluginExtensionServer},
11};
12use std::{fs, path::Path};
13use tonic::codegen::tokio_stream::wrappers::UnixListenerStream;
14use tracing::{debug};
15
16pub async fn run_server<T: PluginExtension + Send + Sync + 'static, P: AsRef<Path>>(
17 service: T,
18 plugin_path: P,
19) -> Result<(), Box<dyn std::error::Error>> {
20 use tokio::net::UnixListener;
21 use tonic::transport::Server;
22
23 debug!(
24 "Starting server with plugin path: {}",
25 plugin_path.as_ref().display()
26 );
27
28 let plugin_config_string = fs::read_to_string(plugin_path)?;
29 let plugin_config: serde_json::Value = serde_yaml::from_str(plugin_config_string.as_str())?;
30 let sock_path_option = plugin_config.get("sock_path");
31 if let Some(sock_path_value) = sock_path_option {
32 if let Some(sock_path) = sock_path_value.as_str() {
33 let _ = fs::remove_file(&sock_path);
35 let path = Path::new(sock_path);
36 if path.exists() {
37 if !path.is_file() {
39 debug!("sock_path exists but is not a file: {}", sock_path);
40 return Err(Box::new(std::io::Error::new(
41 std::io::ErrorKind::InvalidData,
42 "sock_path exists but is not a file",
43 )));
44 }
45 } else {
46 if let Some(parent) = path.parent() {
48 fs::create_dir_all(parent)?;
49 debug!("Created directory for socket path: {}", parent.display());
50 }
51 }
52
53 let uds = UnixListener::bind(sock_path)?;
54 let incoming = UnixListenerStream::new(uds);
55
56 debug!("Starting server on socket: {}", sock_path);
57
58 Server::builder()
59 .add_service(PluginExtensionServer::new(service))
60 .serve_with_incoming(incoming)
61 .await?;
62
63 Ok(())
64 } else {
65 debug!("sock_path is not a string, skipping");
66 Err(Box::new(std::io::Error::new(
67 std::io::ErrorKind::InvalidData,
68 "sock_path is not a string",
69 )))
70 }
71 } else {
72 debug!("no socket path given, skipping");
73 Err(Box::new(std::io::Error::new(
74 std::io::ErrorKind::NotFound,
75 "sock_path not found in plugin config",
76 )))
77 }
78}