churn/crates/churn-server/src/agent.rs

61 lines
1.2 KiB
Rust
Raw Normal View History

use std::sync::Arc;
use axum::async_trait;
use churn_capnp::CapnpPackExt;
use churn_domain::{Agent, ServerEnrollReq};
use crate::db::Db;
#[derive(Clone)]
pub struct AgentService(Arc<dyn AgentServiceTrait + Send + Sync + 'static>);
impl AgentService {
pub fn new(db: Db) -> Self {
Self(Arc::new(DefaultAgentService::new(db)))
}
}
impl std::ops::Deref for AgentService {
type Target = Arc<dyn AgentServiceTrait + Send + Sync + 'static>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
struct DefaultAgentService {
agents: Db,
}
impl DefaultAgentService {
pub fn new(db: Db) -> Self {
Self { agents: db }
}
}
#[async_trait]
pub trait AgentServiceTrait {
async fn enroll(&self, req: ServerEnrollReq) -> anyhow::Result<String>;
}
#[async_trait]
impl AgentServiceTrait for DefaultAgentService {
async fn enroll(&self, req: ServerEnrollReq) -> anyhow::Result<String> {
let agent_name = req.agent_name;
self.agents
.insert(
"agents",
&agent_name,
&Agent {
name: agent_name.clone(),
}
.serialize_capnp(),
)
.await?;
Ok(agent_name)
}
}