diff --git a/lazy-supplements-desktop/Cargo.toml b/lazy-supplements-desktop/Cargo.toml index 6e0c3c7..3611cfa 100644 --- a/lazy-supplements-desktop/Cargo.toml +++ b/lazy-supplements-desktop/Cargo.toml @@ -16,10 +16,15 @@ clap.workspace = true dirs = "6.0.0" lazy-supplements-core = { workspace = true, features = ["desktop"] } libp2p.workspace = true +prost = "0.14.1" serde.workspace = true thiserror.workspace = true tokio.workspace = true +tonic = "0.14.0" uuid.workspace = true [dev-dependencies] lazy-supplements-core = {workspace = true, features = ["test"]} + +[build-dependencies] +tonic-prost-build = "0.14.0" diff --git a/lazy-supplements-desktop/build.rs b/lazy-supplements-desktop/build.rs new file mode 100644 index 0000000..833c16f --- /dev/null +++ b/lazy-supplements-desktop/build.rs @@ -0,0 +1,4 @@ +fn main() -> Result<(), Box> { + tonic_prost_build::compile_protos("proto/lazy_supplements.proto")?; + Ok(()) +} \ No newline at end of file diff --git a/lazy-supplements-desktop/proto/lazy_supplements.proto b/lazy-supplements-desktop/proto/lazy_supplements.proto new file mode 100644 index 0000000..d2788a3 --- /dev/null +++ b/lazy-supplements-desktop/proto/lazy_supplements.proto @@ -0,0 +1,28 @@ +syntax = "proto3"; +package lazy_supplements; + +enum PeerListOrderBy { + CREATED_AT = 0; + UPDATED_AT = 1; + PEER_ID = 2; +} + +service CachedPeerService { + rpc List(CachedPeerListRequest) returns (CachedPeerListResponse); +} + +message CachedPeerListRequest { + uint32 start = 1; + uint32 count = 2; + PeerListOrderBy order_by = 3; + +} + +message CachedPeer { + string peer_id = 1; + repeated string multi_addresss = 2; +} + +message CachedPeerListResponse { + repeated CachedPeer peers = 1; +} diff --git a/lazy-supplements-desktop/src/cli/args/config.rs b/lazy-supplements-desktop/src/cli/args/config.rs index 8dcf791..26c87f1 100644 --- a/lazy-supplements-desktop/src/cli/args/config.rs +++ b/lazy-supplements-desktop/src/cli/args/config.rs @@ -3,8 +3,6 @@ use std::{net::IpAddr, path::PathBuf}; use clap::Args; use lazy_supplements_core::config::{BaseConfig, ConfigError}; use crate::config::{PartialP2pConfig, PartialStorageConfig}; -#[cfg(unix)] -use crate::config::PartialUnixConfig; use serde::{Deserialize, Serialize}; diff --git a/lazy-supplements-desktop/src/config/mod.rs b/lazy-supplements-desktop/src/config/mod.rs index f01812e..2254173 100644 --- a/lazy-supplements-desktop/src/config/mod.rs +++ b/lazy-supplements-desktop/src/config/mod.rs @@ -1,8 +1,4 @@ -#[cfg(unix)] -pub mod unix; - -#[cfg(windows)] -pub mod windows; +pub mod rpc; use clap::Args; pub use lazy_supplements_core::config::*; @@ -10,7 +6,7 @@ pub use lazy_supplements_core::config::*; use lazy_supplements_core::utils::{emptiable::Emptiable, mergeable::Mergeable}; use serde::{Deserialize, Serialize}; #[cfg(unix)] -pub use unix::*; +pub use rpc::*; #[cfg(windows)] pub use windows::*; @@ -21,9 +17,8 @@ pub struct DesktopBaseConfig { p2p: PartialP2pConfig, #[command(flatten)] storage: PartialStorageConfig, - #[cfg(unix)] #[command(flatten)] - unix: PartialUnixConfig, + rpc: PartialRpcConfig, } impl BaseConfig for DesktopBaseConfig { @@ -31,7 +26,7 @@ impl BaseConfig for DesktopBaseConfig { Self { p2p : PartialP2pConfig::empty().with_new_secret(), storage: PartialStorageConfig::empty(), - unix: PartialUnixConfig::empty(), + rpc: PartialRpcConfig::empty().with_unused_port(), } } } @@ -46,8 +41,8 @@ impl Into for &DesktopBaseConfig { self.storage.clone() } } -impl Into for &DesktopBaseConfig { - fn into(self) -> PartialUnixConfig { - self.unix.clone() +impl Into for &DesktopBaseConfig { + fn into(self) -> PartialRpcConfig { + self.rpc.clone() } } \ No newline at end of file diff --git a/lazy-supplements-desktop/src/config/rpc.rs b/lazy-supplements-desktop/src/config/rpc.rs new file mode 100644 index 0000000..ce90a6d --- /dev/null +++ b/lazy-supplements-desktop/src/config/rpc.rs @@ -0,0 +1,60 @@ +use std::{net::{IpAddr, Ipv4Addr, SocketAddr, TcpListener}, path::PathBuf}; +use clap::Args; +use lazy_supplements_core::{config::PartialConfig, utils::{emptiable::Emptiable, mergeable::Mergeable}}; +use libp2p::mdns::Config; +use serde::{Deserialize, Serialize}; + +use crate::config::error::ConfigError; + + +pub struct RpcConfig { + pub listen_address: IpAddr, + pub port: u16, +} + +impl TryFrom for RpcConfig { + type Error = ConfigError; + fn try_from(config: PartialRpcConfig) -> Result { + Ok(Self{ + listen_address: config.listen_address.ok_or(ConfigError::MissingConfig("listen_address".to_string()))?, + port: config.port.ok_or(ConfigError::MissingConfig("port".to_string()))?, + }) + } +} + +#[derive(Args, Clone, Debug, Deserialize, Emptiable, Mergeable, Serialize)] +pub struct PartialRpcConfig { + pub listen_address: Option, + pub port: Option, +} +impl PartialRpcConfig { + pub fn with_unused_port(mut self) -> Self { + let listneer = if let Some(x) = self.listen_address { + TcpListener::bind(SocketAddr::new(x,0)).unwrap() + } else { + TcpListener::bind("127.0.0.1:0").unwrap() + }; + self.port = Some(listneer.local_addr().unwrap().port()); + self + } +} + +impl Default for PartialRpcConfig { + fn default() -> Self { + Self{ + listen_address: Some(IpAddr::V4(Ipv4Addr::LOCALHOST)), + port: None, + } + } +} + +impl From for PartialRpcConfig { + fn from(source: RpcConfig) -> Self { + Self { + listen_address: Some(source.listen_address), + port: Some(source.port), + } + } +} + + diff --git a/lazy-supplements-desktop/src/config/unix.rs b/lazy-supplements-desktop/src/config/unix.rs deleted file mode 100644 index bb922a8..0000000 --- a/lazy-supplements-desktop/src/config/unix.rs +++ /dev/null @@ -1,42 +0,0 @@ -use std::path::PathBuf; -use clap::Args; -use lazy_supplements_core::{config::PartialConfig, utils::{emptiable::Emptiable, mergeable::Mergeable}}; -use libp2p::mdns::Config; -use serde::{Deserialize, Serialize}; - -use crate::config::error::ConfigError; - - -pub struct UnixConfig { - pub socket_path: PathBuf, -} - -impl TryFrom for UnixConfig { - type Error = ConfigError; - fn try_from(config: PartialUnixConfig) -> Result { - Ok(Self{ - socket_path: config.socket_path.ok_or(ConfigError::MissingConfig("socket_path".to_string()))? - }) - } -} - -#[derive(Args, Clone, Debug, Deserialize, Emptiable, Mergeable, Serialize)] -pub struct PartialUnixConfig { - pub socket_path: Option, -} - -impl Default for PartialUnixConfig { - fn default() -> Self { - todo!() - } -} - -impl From for PartialUnixConfig { - fn from(source: UnixConfig) -> Self { - Self { - socket_path: Some(source.socket_path) - } - } -} - - diff --git a/lazy-supplements-desktop/src/config/windows.rs b/lazy-supplements-desktop/src/config/windows.rs deleted file mode 100644 index d9a6589..0000000 --- a/lazy-supplements-desktop/src/config/windows.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub struct WindowsConfig { - pub pipe_name: String -}