feat: with monitor

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2023-08-26 22:32:38 +02:00
parent 10eae9b36c
commit 569f5272e6
14 changed files with 526 additions and 95 deletions

View File

@@ -1,7 +1,19 @@
mod agent;
use std::net::SocketAddr;
use axum::{response::IntoResponse, routing::get, Router};
use agent::AgentService;
use anyhow::Error;
use axum::{
extract::State,
http::StatusCode,
response::{IntoResponse, Response},
routing::{get, post},
Json, Router,
};
use churn_domain::AgentEnrollReq;
use clap::{Parser, Subcommand};
use serde_json::json;
#[derive(Parser)]
#[command(author, version, about, long_about = None, subcommand_required = true)]
@@ -30,6 +42,14 @@ enum Commands {
},
}
#[derive(Clone)]
#[derive(Default)]
struct AppState {
agent: AgentService,
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
dotenv::dotenv().ok();
@@ -47,7 +67,10 @@ async fn handle_command(cmd: Command) -> anyhow::Result<()> {
Some(Commands::Daemon { host }) => {
tracing::info!("Starting churn server");
let app = Router::new().route("/ping", get(ping));
let app = Router::new()
.route("/enroll", post(enroll))
.route("/ping", get(ping))
.with_state(AppState::default());
tracing::info!("churn server listening on {}", host);
axum::Server::bind(&host)
@@ -58,14 +81,52 @@ async fn handle_command(cmd: Command) -> anyhow::Result<()> {
Ok(())
}
Some(Commands::Connect {
host,
token,
agent_name,
host: _,
token: _,
agent_name: _,
}) => todo!(),
None => todo!(),
}
}
enum AppError {
Internal(Error),
}
impl IntoResponse for AppError {
fn into_response(self) -> Response {
let (status, error_message) = match self {
AppError::Internal(e) => {
tracing::error!("failed with error: {}", e);
(
StatusCode::INTERNAL_SERVER_ERROR,
"failed with internal error",
)
}
};
let body = Json(json!({
"error": error_message,
}));
(status, body).into_response()
}
}
async fn ping() -> impl IntoResponse {
"pong!"
}
async fn enroll(
State(state): State<AppState>,
Json(req): Json<AgentEnrollReq>,
) -> Result<(), AppError> {
state
.agent
.enroll(&req.agent_name, &req.server, &req.lease)
.await
.map_err(AppError::Internal)?;
Ok(())
}