2023-08-26 22:32:38 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use axum::async_trait;
|
|
|
|
|
2023-08-27 19:42:33 +02:00
|
|
|
use churn_capnp::CapnpPackExt;
|
|
|
|
use churn_domain::{Agent, ServerEnrollReq};
|
2023-08-26 22:32:38 +02:00
|
|
|
|
2023-08-27 19:42:33 +02:00
|
|
|
use crate::db::Db;
|
2023-08-26 22:32:38 +02:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct AgentService(Arc<dyn AgentServiceTrait + Send + Sync + 'static>);
|
|
|
|
|
2023-08-27 19:42:33 +02:00
|
|
|
impl AgentService {
|
|
|
|
pub fn new(db: Db) -> Self {
|
|
|
|
Self(Arc::new(DefaultAgentService::new(db)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-26 22:32:38 +02:00
|
|
|
impl std::ops::Deref for AgentService {
|
|
|
|
type Target = Arc<dyn AgentServiceTrait + Send + Sync + 'static>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct DefaultAgentService {
|
2023-08-27 19:42:33 +02:00
|
|
|
agents: Db,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DefaultAgentService {
|
|
|
|
pub fn new(db: Db) -> Self {
|
|
|
|
Self { agents: db }
|
|
|
|
}
|
2023-08-26 22:32:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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;
|
|
|
|
|
2023-08-27 19:42:33 +02:00
|
|
|
self.agents
|
|
|
|
.insert(
|
|
|
|
"agents",
|
|
|
|
&agent_name,
|
|
|
|
&Agent {
|
|
|
|
name: agent_name.clone(),
|
|
|
|
}
|
|
|
|
.serialize_capnp(),
|
|
|
|
)
|
|
|
|
.await?;
|
2023-08-26 22:32:38 +02:00
|
|
|
|
2023-08-27 19:42:33 +02:00
|
|
|
Ok(agent_name)
|
2023-08-26 22:32:38 +02:00
|
|
|
}
|
|
|
|
}
|