Update Runnable and fix errors

This commit is contained in:
fluo10 2025-08-21 07:40:33 +09:00
parent be896aaae3
commit 5ee1ffca63
7 changed files with 29 additions and 13 deletions

View file

@ -8,3 +8,5 @@ repository.workspace = true
[dependencies]
caretta.path = "../.."
libp2p.workspace = true
tokio.workspace = true

View file

@ -0,0 +1 @@
pub const APP_NAME: &str = "caretta_demo";

View file

@ -1,2 +1,3 @@
pub mod global;
pub mod rpc;
pub mod server;
pub mod server;

View file

@ -1,17 +1,20 @@
use caretta::{config::P2pConfig, server::ServerTrait};
pub struct Server{};
use libp2p::{futures::StreamExt, noise, swarm::SwarmEvent, tcp, yamux};
pub struct Server{}
impl ServerTrait for Server {
async fn serve_p2p(config: P2pConfig) -> Result<(), caretta::error::Error> {
let mut swarm = libp2p::SwarmBuilder::with_existing_identity(self.secret)
async fn serve_p2p<T>(config: &T) -> Result<(), caretta::error::Error>
where
T: AsRef<P2pConfig>
{
let mut swarm = libp2p::SwarmBuilder::with_existing_identity(config.as_ref().secret.clone())
.with_tokio()
.with_tcp(
tcp::Config::default(),
noise::Config::new,
yamux::Config::default,
)?
.with_behaviour(|keypair| p2p::Behaviour::try_from(keypair).unwrap())?
.with_behaviour(|keypair| caretta::p2p::Behaviour::try_from(keypair).unwrap())?
.build();
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
loop{
@ -28,4 +31,9 @@ impl ServerTrait for Server {
});
}
}
async fn serve_rpc<T>(config: &T) -> Result<(), caretta::error::Error>
where T: AsRef<caretta::config::RpcConfig> {
todo!()
}
}

View file

@ -1,20 +1,21 @@
mod server;
use clap::{Parser, Subcommand};
use caretta::cli::*;
use caretta::{cli::*, utils::runnable::Runnable};
pub use server::*;
#[derive(Debug, Parser)]
#[derive(Debug, Parser, Runnable)]
pub struct Cli {
#[command(subcommand)]
#[runnable]
command: CliCommand
}
#[derive(Debug, Subcommand)]
#[derive(Debug, Subcommand, Runnable)]
pub enum CliCommand {
Config(ConfigCommandArgs),
Device(DeviceCommandArgs),
Logs(LogsCommandArgs),
Peer(PeerSubcommand),
Peer(PeerCommandArgs),
Server(ServerCommandArgs),
}

View file

@ -1,5 +1,5 @@
use clap::Args;
use caretta::{error::Error, global::CONFIG, utils::runnable::Runnable};
use caretta::{config::Config, error::Error, global::CONFIG, utils::runnable::Runnable};
use libp2p::{noise, ping, swarm::{NetworkBehaviour, SwarmEvent}, tcp, yamux, Swarm};
use super::ConfigArgs;
@ -10,7 +10,8 @@ pub struct ServerCommandArgs {
config: ConfigArgs,
}
impl Runnable for ServerCommandArgs {
async fn run(self) {
let config = CONFIG.get_or_init(self.config.try_into()?).await;
async fn run(self, app_name: &'static str) {
let config = CONFIG.get_or_init::<Config>(self.config.into_config_unchecked(app_name).await).await;
}
}

View file

@ -1,3 +1,5 @@
use clap::Parser;
use crate::cli::Cli;
mod cli;