2023-08-25 21:52:28 +02:00
|
|
|
use std::collections::HashMap;
|
2023-08-24 17:22:45 +02:00
|
|
|
use std::net::SocketAddr;
|
2023-08-25 21:52:28 +02:00
|
|
|
use std::sync::Arc;
|
2023-08-24 17:22:45 +02:00
|
|
|
|
2023-08-26 18:46:56 +02:00
|
|
|
use anyhow::Error;
|
|
|
|
use axum::extract::State;
|
|
|
|
use axum::http::StatusCode;
|
|
|
|
use axum::response::{IntoResponse, Response};
|
2023-08-24 17:22:45 +02:00
|
|
|
use axum::routing::{get, post};
|
2023-08-26 18:46:56 +02:00
|
|
|
use axum::{async_trait, Json, Router};
|
2023-08-24 17:22:45 +02:00
|
|
|
use clap::{Parser, Subcommand};
|
2023-08-26 18:46:56 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use serde_json::json;
|
2023-08-25 21:52:28 +02:00
|
|
|
use tokio::sync::Mutex;
|
2023-08-24 17:22:45 +02:00
|
|
|
|
|
|
|
#[derive(Parser)]
|
|
|
|
#[command(author, version, about, long_about = None, subcommand_required = true)]
|
|
|
|
struct Command {
|
|
|
|
#[command(subcommand)]
|
|
|
|
command: Option<Commands>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Subcommand)]
|
|
|
|
enum Commands {
|
|
|
|
Serve {
|
|
|
|
#[arg(env = "SERVICE_HOST", long, default_value = "127.0.0.1:3000")]
|
|
|
|
host: SocketAddr,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-08-26 18:46:56 +02:00
|
|
|
#[derive(Clone)]
|
2023-08-25 21:52:28 +02:00
|
|
|
struct AgentService(Arc<dyn AgentServiceTrait + Send + Sync + 'static>);
|
|
|
|
|
2023-08-26 18:46:56 +02:00
|
|
|
impl std::ops::Deref for AgentService {
|
|
|
|
type Target = Arc<dyn AgentServiceTrait + Send + Sync + 'static>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-25 21:52:28 +02:00
|
|
|
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]
|
2023-08-26 18:46:56 +02:00
|
|
|
trait AgentServiceTrait {
|
|
|
|
async fn enroll(&self, agent: Agent) -> anyhow::Result<String>;
|
2023-08-25 21:52:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
2023-08-26 18:46:56 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-25 21:52:28 +02:00
|
|
|
}
|
|
|
|
|
2023-08-26 18:46:56 +02:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
2023-08-25 21:52:28 +02:00
|
|
|
struct Agent {
|
|
|
|
pub name: String,
|
|
|
|
}
|
|
|
|
|
2023-08-26 18:46:56 +02:00
|
|
|
#[derive(Clone)]
|
|
|
|
struct AppState {
|
|
|
|
agent: AgentService,
|
|
|
|
}
|
2023-08-25 21:52:28 +02:00
|
|
|
|
2023-08-24 17:22:45 +02:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> anyhow::Result<()> {
|
|
|
|
dotenv::dotenv().ok();
|
|
|
|
tracing_subscriber::fmt::init();
|
|
|
|
|
|
|
|
let cli = Command::parse();
|
|
|
|
|
|
|
|
match cli.command {
|
|
|
|
Some(Commands::Serve { host }) => {
|
2023-08-25 21:52:28 +02:00
|
|
|
tracing::info!("Starting churn server");
|
2023-08-24 17:22:45 +02:00
|
|
|
|
2023-08-25 21:52:28 +02:00
|
|
|
let app = Router::new()
|
|
|
|
.route("/ping", get(ping))
|
|
|
|
.nest(
|
|
|
|
"/agent",
|
|
|
|
Router::new()
|
|
|
|
.route("/enroll", post(enroll))
|
|
|
|
.route("/ping", post(agent_ping))
|
|
|
|
.route("/events", post(get_tasks)),
|
|
|
|
)
|
2023-08-26 18:46:56 +02:00
|
|
|
.with_state(AppState {
|
|
|
|
agent: AgentService::default(),
|
|
|
|
});
|
2023-08-24 17:22:45 +02:00
|
|
|
|
2023-08-25 21:52:28 +02:00
|
|
|
tracing::info!("churn server listening on {}", host);
|
2023-08-24 17:22:45 +02:00
|
|
|
axum::Server::bind(&host)
|
|
|
|
.serve(app.into_make_service())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-08-26 18:46:56 +02:00
|
|
|
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))
|
2023-08-24 17:22:45 +02:00
|
|
|
}
|
|
|
|
|
2023-08-25 21:52:28 +02:00
|
|
|
async fn agent_ping() -> impl IntoResponse {
|
2023-08-24 17:22:45 +02:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_tasks() -> impl IntoResponse {
|
|
|
|
todo!()
|
|
|
|
}
|
2023-08-25 21:52:28 +02:00
|
|
|
|
|
|
|
async fn ping() -> impl IntoResponse {
|
|
|
|
"pong!"
|
|
|
|
}
|