@@ -6,6 +6,8 @@ edition = "2021"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
churn-domain.workspace = true
|
||||
|
||||
anyhow.workspace = true
|
||||
tokio.workspace = true
|
||||
tracing.workspace = true
|
||||
@@ -14,4 +16,5 @@ clap.workspace = true
|
||||
dotenv.workspace = true
|
||||
axum.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
serde_json.workspace = true
|
||||
uuid.workspace = true
|
64
crates/churn-server/src/agent.rs
Normal file
64
crates/churn-server/src/agent.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::async_trait;
|
||||
|
||||
use churn_domain::ServerEnrollReq;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use crate::Agent;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AgentService(Arc<dyn AgentServiceTrait + Send + Sync + 'static>);
|
||||
|
||||
impl std::ops::Deref for AgentService {
|
||||
type Target = Arc<dyn AgentServiceTrait + Send + Sync + 'static>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for AgentService {
|
||||
fn default() -> Self {
|
||||
Self(Arc::new(DefaultAgentService::default()))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct DefaultAgentService {
|
||||
agents: Arc<Mutex<HashMap<String, Agent>>>,
|
||||
}
|
||||
|
||||
#[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;
|
||||
|
||||
let mut agents = self.agents.lock().await;
|
||||
|
||||
match agents.insert(
|
||||
agent_name.clone(),
|
||||
Agent {
|
||||
name: agent_name.clone(),
|
||||
},
|
||||
) {
|
||||
Some(_) => {
|
||||
tracing::debug!("agents store already contained agent, replaced existing");
|
||||
|
||||
Ok(agent_name)
|
||||
}
|
||||
None => {
|
||||
tracing::debug!("agents store didn't contain agent, inserted");
|
||||
|
||||
Ok(agent_name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
57
crates/churn-server/src/lease.rs
Normal file
57
crates/churn-server/src/lease.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
use axum::{async_trait};
|
||||
|
||||
|
||||
|
||||
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct LeaseService(Arc<dyn LeaseServiceTrait + Send + Sync + 'static>);
|
||||
|
||||
impl std::ops::Deref for LeaseService {
|
||||
type Target = Arc<dyn LeaseServiceTrait + Send + Sync + 'static>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for LeaseService {
|
||||
fn default() -> Self {
|
||||
Self(Arc::new(DefaultLeaseService::default()))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct DefaultLeaseService {
|
||||
leases: Arc<Mutex<Vec<String>>>,
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[async_trait]
|
||||
pub trait LeaseServiceTrait {
|
||||
async fn create_lease(&self) -> anyhow::Result<String>;
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl LeaseServiceTrait for DefaultLeaseService {
|
||||
async fn create_lease(&self) -> anyhow::Result<String> {
|
||||
let mut leases = self.leases.lock().await;
|
||||
|
||||
let lease = uuid::Uuid::new_v4().to_string();
|
||||
|
||||
leases.push(lease.clone());
|
||||
|
||||
Ok(lease)
|
||||
}
|
||||
}
|
@@ -1,17 +1,20 @@
|
||||
use std::collections::HashMap;
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::Arc;
|
||||
mod agent;
|
||||
mod lease;
|
||||
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use agent::AgentService;
|
||||
use anyhow::Error;
|
||||
use axum::extract::State;
|
||||
use axum::extract::{Query, State};
|
||||
use axum::http::StatusCode;
|
||||
use axum::response::{IntoResponse, Response};
|
||||
use axum::routing::{get, post};
|
||||
use axum::{async_trait, Json, Router};
|
||||
use axum::{Json, Router};
|
||||
use churn_domain::{LeaseResp, ServerEnrollReq, ServerMonitorResp};
|
||||
use clap::{Parser, Subcommand};
|
||||
use lease::LeaseService;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(author, version, about, long_about = None, subcommand_required = true)]
|
||||
@@ -28,60 +31,6 @@ enum Commands {
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct AgentService(Arc<dyn AgentServiceTrait + Send + Sync + 'static>);
|
||||
|
||||
impl std::ops::Deref for AgentService {
|
||||
type Target = Arc<dyn AgentServiceTrait + Send + Sync + 'static>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for AgentService {
|
||||
fn default() -> Self {
|
||||
Self(Arc::new(DefaultAgentService::default()))
|
||||
}
|
||||
}
|
||||
|
||||
struct DefaultAgentService {
|
||||
agents: Arc<Mutex<HashMap<String, Agent>>>,
|
||||
}
|
||||
|
||||
impl Default for DefaultAgentService {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
agents: Arc::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
trait AgentServiceTrait {
|
||||
async fn enroll(&self, agent: Agent) -> anyhow::Result<String>;
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl AgentServiceTrait for DefaultAgentService {
|
||||
async fn enroll(&self, agent: Agent) -> anyhow::Result<String> {
|
||||
let mut agents = self.agents.lock().await;
|
||||
|
||||
match agents.insert(agent.name.clone(), agent.clone()) {
|
||||
Some(_) => {
|
||||
tracing::debug!("agents store already contained agent, replaced existing");
|
||||
|
||||
Ok(agent.name)
|
||||
}
|
||||
None => {
|
||||
tracing::debug!("agents store didn't contain agent, inserted");
|
||||
|
||||
Ok(agent.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
struct Agent {
|
||||
pub name: String,
|
||||
@@ -90,6 +39,7 @@ struct Agent {
|
||||
#[derive(Clone)]
|
||||
struct AppState {
|
||||
agent: AgentService,
|
||||
leases: LeaseService,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
@@ -105,15 +55,18 @@ async fn main() -> anyhow::Result<()> {
|
||||
|
||||
let app = Router::new()
|
||||
.route("/ping", get(ping))
|
||||
.route("/logs", get(logs))
|
||||
.nest(
|
||||
"/agent",
|
||||
Router::new()
|
||||
.route("/enroll", post(enroll))
|
||||
.route("/ping", post(agent_ping))
|
||||
.route("/events", post(get_tasks)),
|
||||
.route("/events", post(get_tasks))
|
||||
.route("/lease", post(agent_lease)),
|
||||
)
|
||||
.with_state(AppState {
|
||||
agent: AgentService::default(),
|
||||
leases: LeaseService::default(),
|
||||
});
|
||||
|
||||
tracing::info!("churn server listening on {}", host);
|
||||
@@ -155,15 +108,21 @@ impl IntoResponse for AppError {
|
||||
|
||||
async fn enroll(
|
||||
State(state): State<AppState>,
|
||||
Json(agent): Json<Agent>,
|
||||
Json(req): Json<ServerEnrollReq>,
|
||||
) -> Result<Json<Agent>, AppError> {
|
||||
let _ = state
|
||||
.agent
|
||||
.enroll(agent.clone())
|
||||
.await
|
||||
.map_err(|e| AppError::Internal(e));
|
||||
let name = state.agent.enroll(req).await.map_err(AppError::Internal)?;
|
||||
|
||||
Ok(Json(agent))
|
||||
Ok(Json(Agent { name }))
|
||||
}
|
||||
|
||||
async fn agent_lease(State(state): State<AppState>) -> Result<Json<LeaseResp>, AppError> {
|
||||
let lease = state
|
||||
.leases
|
||||
.create_lease()
|
||||
.await
|
||||
.map_err(AppError::Internal)?;
|
||||
|
||||
Ok(Json(LeaseResp { token: lease }))
|
||||
}
|
||||
|
||||
async fn agent_ping() -> impl IntoResponse {
|
||||
@@ -177,3 +136,24 @@ async fn get_tasks() -> impl IntoResponse {
|
||||
async fn ping() -> impl IntoResponse {
|
||||
"pong!"
|
||||
}
|
||||
|
||||
#[derive(Clone, Deserialize)]
|
||||
struct LogsQuery {
|
||||
cursor: Option<String>,
|
||||
}
|
||||
|
||||
async fn logs(Query(cursor): Query<LogsQuery>) -> Result<Json<ServerMonitorResp>, AppError> {
|
||||
match cursor.cursor {
|
||||
Some(cursor) => {
|
||||
tracing::trace!("finding logs from cursor: {}", cursor);
|
||||
}
|
||||
None => {
|
||||
tracing::trace!("finding logs from beginning");
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Json(ServerMonitorResp {
|
||||
cursor: uuid::Uuid::new_v4().to_string(),
|
||||
logs: vec!["something".to_string()],
|
||||
}))
|
||||
}
|
||||
|
Reference in New Issue
Block a user