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
|
|
|
|
|
|
|
use axum::response::IntoResponse;
|
|
|
|
use axum::routing::{get, post};
|
2023-08-25 21:52:28 +02:00
|
|
|
use axum::{async_trait, Router};
|
2023-08-24 17:22:45 +02:00
|
|
|
use clap::{Parser, Subcommand};
|
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-25 21:52:28 +02:00
|
|
|
struct AgentService(Arc<dyn AgentServiceTrait + Send + Sync + 'static>);
|
|
|
|
|
|
|
|
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]
|
|
|
|
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>;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Agent {
|
|
|
|
pub name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct AppState {}
|
|
|
|
|
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)),
|
|
|
|
)
|
|
|
|
.with_state();
|
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(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn enroll() -> impl IntoResponse {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
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!"
|
|
|
|
}
|