feat: with enroll

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2023-08-26 18:46:56 +02:00
parent 97978df287
commit 8c41e1004c
5 changed files with 102 additions and 16 deletions

View File

@@ -12,4 +12,6 @@ tracing.workspace = true
tracing-subscriber.workspace = true
clap.workspace = true
dotenv.workspace = true
axum.workspace = true
axum.workspace = true
serde.workspace = true
serde_json.workspace = true

View File

@@ -2,10 +2,15 @@ use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::Arc;
use axum::response::IntoResponse;
use anyhow::Error;
use axum::extract::State;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::routing::{get, post};
use axum::{async_trait, Router};
use axum::{async_trait, Json, Router};
use clap::{Parser, Subcommand};
use serde::{Deserialize, Serialize};
use serde_json::json;
use tokio::sync::Mutex;
#[derive(Parser)]
@@ -23,8 +28,17 @@ 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()))
@@ -43,23 +57,40 @@ impl Default for DefaultAgentService {
}
}
#[async_trait]
impl AgentServiceTrait for DefaultAgentService {
async fn enroll(&self, agent: Agent) -> anyhow::Result<String> {
todo!()
}
}
#[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,
}
struct AppState {}
#[derive(Clone)]
struct AppState {
agent: AgentService,
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
@@ -81,7 +112,9 @@ async fn main() -> anyhow::Result<()> {
.route("/ping", post(agent_ping))
.route("/events", post(get_tasks)),
)
.with_state();
.with_state(AppState {
agent: AgentService::default(),
});
tracing::info!("churn server listening on {}", host);
axum::Server::bind(&host)
@@ -95,8 +128,42 @@ async fn main() -> anyhow::Result<()> {
Ok(())
}
async fn enroll() -> impl IntoResponse {
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 enroll(
State(state): State<AppState>,
Json(agent): Json<Agent>,
) -> Result<Json<Agent>, AppError> {
let _ = state
.agent
.enroll(agent.clone())
.await
.map_err(|e| AppError::Internal(e));
Ok(Json(agent))
}
async fn agent_ping() -> impl IntoResponse {