Add tonic

This commit is contained in:
fluo10 2025-08-01 09:25:20 +09:00
parent 9c960a9d59
commit 25ea7ed652
8 changed files with 104 additions and 59 deletions

View file

@ -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"

View file

@ -0,0 +1,4 @@
fn main() -> Result<(), Box<dyn std::error::Error>> {
tonic_prost_build::compile_protos("proto/lazy_supplements.proto")?;
Ok(())
}

View 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;
}

View file

@ -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};

View file

@ -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<PartialStorageConfig> for &DesktopBaseConfig {
self.storage.clone()
}
}
impl Into<PartialUnixConfig> for &DesktopBaseConfig {
fn into(self) -> PartialUnixConfig {
self.unix.clone()
impl Into<PartialRpcConfig> for &DesktopBaseConfig {
fn into(self) -> PartialRpcConfig {
self.rpc.clone()
}
}

View 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),
}
}
}

View file

@ -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)
}
}
}

View file

@ -1,3 +0,0 @@
pub struct WindowsConfig {
pub pipe_name: String
}