@@ -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