feat: with monitoring

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2023-08-27 00:07:56 +02:00
parent 569f5272e6
commit e0545c726c
8 changed files with 145 additions and 11 deletions

View File

@@ -1,4 +1,5 @@
mod agent;
mod event;
mod lease;
use std::net::SocketAddr;
@@ -12,6 +13,7 @@ use axum::routing::{get, post};
use axum::{Json, Router};
use churn_domain::{LeaseResp, ServerEnrollReq, ServerMonitorResp};
use clap::{Parser, Subcommand};
use event::{EventService, LogEvent};
use lease::LeaseService;
use serde::{Deserialize, Serialize};
use serde_json::json;
@@ -40,6 +42,7 @@ struct Agent {
struct AppState {
agent: AgentService,
leases: LeaseService,
events: EventService,
}
#[tokio::main]
@@ -67,6 +70,7 @@ async fn main() -> anyhow::Result<()> {
.with_state(AppState {
agent: AgentService::default(),
leases: LeaseService::default(),
events: EventService::default(),
});
tracing::info!("churn server listening on {}", host);
@@ -110,8 +114,20 @@ async fn enroll(
State(state): State<AppState>,
Json(req): Json<ServerEnrollReq>,
) -> Result<Json<Agent>, AppError> {
state
.events
.append(LogEvent::new(&req.agent_name, "attempting to enroll agent"))
.await
.map_err(AppError::Internal)?;
let name = state.agent.enroll(req).await.map_err(AppError::Internal)?;
state
.events
.append(LogEvent::new(&name, "enrolled agent"))
.await
.map_err(AppError::Internal)?;
Ok(Json(Agent { name }))
}
@@ -139,10 +155,29 @@ async fn ping() -> impl IntoResponse {
#[derive(Clone, Deserialize)]
struct LogsQuery {
cursor: Option<String>,
cursor: Option<uuid::Uuid>,
}
async fn logs(Query(cursor): Query<LogsQuery>) -> Result<Json<ServerMonitorResp>, AppError> {
async fn logs(
State(state): State<AppState>,
Query(cursor): Query<LogsQuery>,
) -> Result<Json<ServerMonitorResp>, AppError> {
state
.events
.append(LogEvent::new(
"author",
format!(
"logs called: {}",
cursor
.cursor
.as_ref()
.map(|c| format!("(cursor={c})"))
.unwrap_or("".to_string())
),
))
.await
.map_err(AppError::Internal)?;
match cursor.cursor {
Some(cursor) => {
tracing::trace!("finding logs from cursor: {}", cursor);
@@ -152,8 +187,24 @@ async fn logs(Query(cursor): Query<LogsQuery>) -> Result<Json<ServerMonitorResp>
}
}
let events = match cursor.cursor {
Some(c) => state.events.get_from_cursor(c).await,
None => state.events.get_from_beginning().await,
}
.map_err(AppError::Internal)?;
if events.is_empty() {
return Ok(Json(ServerMonitorResp {
cursor: cursor.cursor.clone(),
logs: Vec::new(),
}));
}
Ok(Json(ServerMonitorResp {
cursor: uuid::Uuid::new_v4().to_string(),
logs: vec!["something".to_string()],
cursor: events.last().map(|e| e.id),
logs: events
.iter()
.map(|e| format!("{}: {}", e.author, e.content))
.collect(),
}))
}