Add tonic
This commit is contained in:
parent
9c960a9d59
commit
25ea7ed652
8 changed files with 104 additions and 59 deletions
|
@ -16,10 +16,15 @@ clap.workspace = true
|
||||||
dirs = "6.0.0"
|
dirs = "6.0.0"
|
||||||
lazy-supplements-core = { workspace = true, features = ["desktop"] }
|
lazy-supplements-core = { workspace = true, features = ["desktop"] }
|
||||||
libp2p.workspace = true
|
libp2p.workspace = true
|
||||||
|
prost = "0.14.1"
|
||||||
serde.workspace = true
|
serde.workspace = true
|
||||||
thiserror.workspace = true
|
thiserror.workspace = true
|
||||||
tokio.workspace = true
|
tokio.workspace = true
|
||||||
|
tonic = "0.14.0"
|
||||||
uuid.workspace = true
|
uuid.workspace = true
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
lazy-supplements-core = {workspace = true, features = ["test"]}
|
lazy-supplements-core = {workspace = true, features = ["test"]}
|
||||||
|
|
||||||
|
[build-dependencies]
|
||||||
|
tonic-prost-build = "0.14.0"
|
||||||
|
|
4
lazy-supplements-desktop/build.rs
Normal file
4
lazy-supplements-desktop/build.rs
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
tonic_prost_build::compile_protos("proto/lazy_supplements.proto")?;
|
||||||
|
Ok(())
|
||||||
|
}
|
28
lazy-supplements-desktop/proto/lazy_supplements.proto
Normal file
28
lazy-supplements-desktop/proto/lazy_supplements.proto
Normal file
|
@ -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;
|
||||||
|
}
|
|
@ -3,8 +3,6 @@ use std::{net::IpAddr, path::PathBuf};
|
||||||
use clap::Args;
|
use clap::Args;
|
||||||
use lazy_supplements_core::config::{BaseConfig, ConfigError};
|
use lazy_supplements_core::config::{BaseConfig, ConfigError};
|
||||||
use crate::config::{PartialP2pConfig, PartialStorageConfig};
|
use crate::config::{PartialP2pConfig, PartialStorageConfig};
|
||||||
#[cfg(unix)]
|
|
||||||
use crate::config::PartialUnixConfig;
|
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,4 @@
|
||||||
#[cfg(unix)]
|
pub mod rpc;
|
||||||
pub mod unix;
|
|
||||||
|
|
||||||
#[cfg(windows)]
|
|
||||||
pub mod windows;
|
|
||||||
|
|
||||||
use clap::Args;
|
use clap::Args;
|
||||||
pub use lazy_supplements_core::config::*;
|
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 lazy_supplements_core::utils::{emptiable::Emptiable, mergeable::Mergeable};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
pub use unix::*;
|
pub use rpc::*;
|
||||||
|
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
pub use windows::*;
|
pub use windows::*;
|
||||||
|
@ -21,9 +17,8 @@ pub struct DesktopBaseConfig {
|
||||||
p2p: PartialP2pConfig,
|
p2p: PartialP2pConfig,
|
||||||
#[command(flatten)]
|
#[command(flatten)]
|
||||||
storage: PartialStorageConfig,
|
storage: PartialStorageConfig,
|
||||||
#[cfg(unix)]
|
|
||||||
#[command(flatten)]
|
#[command(flatten)]
|
||||||
unix: PartialUnixConfig,
|
rpc: PartialRpcConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BaseConfig for DesktopBaseConfig {
|
impl BaseConfig for DesktopBaseConfig {
|
||||||
|
@ -31,7 +26,7 @@ impl BaseConfig for DesktopBaseConfig {
|
||||||
Self {
|
Self {
|
||||||
p2p : PartialP2pConfig::empty().with_new_secret(),
|
p2p : PartialP2pConfig::empty().with_new_secret(),
|
||||||
storage: PartialStorageConfig::empty(),
|
storage: PartialStorageConfig::empty(),
|
||||||
unix: PartialUnixConfig::empty(),
|
rpc: PartialRpcConfig::empty().with_unused_port(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,8 +41,8 @@ impl Into<PartialStorageConfig> for &DesktopBaseConfig {
|
||||||
self.storage.clone()
|
self.storage.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl Into<PartialUnixConfig> for &DesktopBaseConfig {
|
impl Into<PartialRpcConfig> for &DesktopBaseConfig {
|
||||||
fn into(self) -> PartialUnixConfig {
|
fn into(self) -> PartialRpcConfig {
|
||||||
self.unix.clone()
|
self.rpc.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
60
lazy-supplements-desktop/src/config/rpc.rs
Normal file
60
lazy-supplements-desktop/src/config/rpc.rs
Normal file
|
@ -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<PartialRpcConfig> for RpcConfig {
|
||||||
|
type Error = ConfigError;
|
||||||
|
fn try_from(config: PartialRpcConfig) -> Result<Self, Self::Error> {
|
||||||
|
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<IpAddr>,
|
||||||
|
pub port: Option<u16>,
|
||||||
|
}
|
||||||
|
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<RpcConfig> for PartialRpcConfig {
|
||||||
|
fn from(source: RpcConfig) -> Self {
|
||||||
|
Self {
|
||||||
|
listen_address: Some(source.listen_address),
|
||||||
|
port: Some(source.port),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -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<PartialUnixConfig> for UnixConfig {
|
|
||||||
type Error = ConfigError;
|
|
||||||
fn try_from(config: PartialUnixConfig) -> Result<Self, Self::Error> {
|
|
||||||
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<PathBuf>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for PartialUnixConfig {
|
|
||||||
fn default() -> Self {
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<UnixConfig> for PartialUnixConfig {
|
|
||||||
fn from(source: UnixConfig) -> Self {
|
|
||||||
Self {
|
|
||||||
socket_path: Some(source.socket_path)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
pub struct WindowsConfig {
|
|
||||||
pub pipe_name: String
|
|
||||||
}
|
|
Loading…
Add table
Reference in a new issue