Move server command implemention to caretta-desktop

This commit is contained in:
fluo10 2025-08-24 10:19:06 +09:00
parent 0fce680e1e
commit 6741c2e0a7
4 changed files with 21 additions and 9 deletions

View file

@ -5,9 +5,11 @@ mod config;
mod device; mod device;
mod logs; mod logs;
mod peer; mod peer;
mod serve;
pub use args::*; pub use args::*;
pub use config::*; pub use config::*;
pub use device::*; pub use device::*;
pub use logs::*; pub use logs::*;
pub use peer::*; pub use peer::*;
pub use serve::*;

View file

@ -1,19 +1,28 @@
use caretta_example_core::server::Server; use std::marker::PhantomData;
use clap::Args; use clap::Args;
use caretta::{config::Config, data::migration::DataMigrator, global::{CONFIG, DATABASE_CONNECTIONS}, server::ServerTrait, utils::runnable::Runnable}; use caretta_core::{config::Config, data::migration::DataMigrator, global::{CONFIG, DATABASE_CONNECTIONS}, server::ServerTrait, utils::runnable::Runnable};
use libp2p::{noise, ping, swarm::{NetworkBehaviour, SwarmEvent}, tcp, yamux, Swarm}; use libp2p::{noise, ping, swarm::{NetworkBehaviour, SwarmEvent}, tcp, yamux, Swarm};
use super::ConfigArgs; use super::ConfigArgs;
#[derive(Args, Debug)] #[derive(Args, Debug)]
pub struct ServerCommandArgs { pub struct ServeCommandArgs<T>
where
T: ServerTrait
{
#[arg(skip)]
server: PhantomData<T>,
#[command(flatten)] #[command(flatten)]
config: ConfigArgs, config: ConfigArgs,
} }
impl Runnable for ServerCommandArgs { impl<T> Runnable for ServeCommandArgs<T>
where
T: ServerTrait
{
async fn run(self, app_name: &'static str) { async fn run(self, app_name: &'static str) {
let config = CONFIG.get_or_init::<Config>(self.config.into_config(app_name).await).await; let config = CONFIG.get_or_init::<Config>(self.config.into_config(app_name).await).await;
let _ = DATABASE_CONNECTIONS.get_or_init_unchecked(&config, DataMigrator).await; let _ = DATABASE_CONNECTIONS.get_or_init_unchecked(&config, DataMigrator).await;
Server::serve_all(config).await.unwrap(); T::serve_all(config).await.unwrap();
} }
} }

View file

@ -7,6 +7,8 @@ use caretta::{
use libp2p::{futures::StreamExt, noise, swarm::SwarmEvent, tcp, yamux}; use libp2p::{futures::StreamExt, noise, swarm::SwarmEvent, tcp, yamux};
use tokio::net::UnixListener; use tokio::net::UnixListener;
use tokio_stream::wrappers::UnixListenerStream; use tokio_stream::wrappers::UnixListenerStream;
#[derive(Debug)]
pub struct Server{} pub struct Server{}
impl ServerTrait for Server { impl ServerTrait for Server {

View file

@ -1,7 +1,6 @@
mod server; use caretta_example_core::server::Server;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use caretta::{cli::*, utils::runnable::Runnable}; use caretta::{cli::*, utils::runnable::Runnable};
pub use server::*;
#[derive(Debug, Parser, Runnable)] #[derive(Debug, Parser, Runnable)]
@ -17,5 +16,5 @@ pub enum CliCommand {
Device(DeviceCommandArgs), Device(DeviceCommandArgs),
Logs(LogsCommandArgs), Logs(LogsCommandArgs),
Peer(PeerCommandArgs), Peer(PeerCommandArgs),
Server(ServerCommandArgs), Serve(ServeCommandArgs<Server>),
} }