feat: add initial churn

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2023-08-25 21:52:28 +02:00
parent f61d0bbf12
commit 97978df287
7 changed files with 258 additions and 21 deletions

View File

@@ -1,5 +1,6 @@
use std::net::SocketAddr;
use axum::{response::IntoResponse, routing::get, Router};
use clap::{Parser, Subcommand};
#[derive(Parser)]
@@ -44,8 +45,16 @@ async fn main() -> anyhow::Result<()> {
async fn handle_command(cmd: Command) -> anyhow::Result<()> {
match cmd.command {
Some(Commands::Daemon { host }) => {
tracing::info!("starting agent server on {}", host);
tokio::time::sleep(std::time::Duration::from_secs(60)).await;
tracing::info!("Starting churn server");
let app = Router::new().route("/ping", get(ping));
tracing::info!("churn server listening on {}", host);
axum::Server::bind(&host)
.serve(app.into_make_service())
.await
.unwrap();
Ok(())
}
Some(Commands::Connect {
@@ -56,3 +65,7 @@ async fn handle_command(cmd: Command) -> anyhow::Result<()> {
None => todo!(),
}
}
async fn ping() -> impl IntoResponse {
"pong!"
}

View File

@@ -1,9 +1,12 @@
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::Arc;
use axum::response::IntoResponse;
use axum::routing::{get, post};
use axum::Router;
use axum::{async_trait, Router};
use clap::{Parser, Subcommand};
use tokio::sync::Mutex;
#[derive(Parser)]
#[command(author, version, about, long_about = None, subcommand_required = true)]
@@ -20,6 +23,44 @@ enum Commands {
},
}
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 {}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
dotenv::dotenv().ok();
@@ -29,17 +70,20 @@ async fn main() -> anyhow::Result<()> {
match cli.command {
Some(Commands::Serve { host }) => {
tracing::info!("Starting service");
tracing::info!("Starting churn server");
let app = Router::new().nest(
"/agent",
Router::new()
.route("/enroll", post(enroll))
.route("/ping", post(ping))
.route("/events", post(get_tasks)),
);
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();
tracing::info!("listening on {}", host);
tracing::info!("churn server listening on {}", host);
axum::Server::bind(&host)
.serve(app.into_make_service())
.await
@@ -55,10 +99,14 @@ async fn enroll() -> impl IntoResponse {
todo!()
}
async fn ping() -> impl IntoResponse {
async fn agent_ping() -> impl IntoResponse {
todo!()
}
async fn get_tasks() -> impl IntoResponse {
todo!()
}
async fn ping() -> impl IntoResponse {
"pong!"
}

View File

@@ -11,3 +11,4 @@ tracing-subscriber.workspace = true
clap.workspace = true
dotenv.workspace = true
axum.workspace = true
reqwest.workspace = true

View File

@@ -51,9 +51,11 @@ async fn handle_command(cmd: Command) -> anyhow::Result<()> {
} => todo!(),
Commands::Health { server, agent } => {
tracing::info!("connecting to server: {}", server);
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
reqwest::get(format!("{server}/ping")).await?;
tracing::info!("connected to server successfully");
tracing::info!("connecting to agent: {}", agent);
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
reqwest::get(format!("{agent}/ping")).await?;
tracing::info!("connected to agent successfully");
Ok(())
}
}

View File

@@ -17,18 +17,24 @@ async fn main() -> eyre::Result<()> {
.with_exec(vec!["churn-server", "serve", "--host", "0.0.0.0:3000"])
.with_exposed_port(3000);
cli.with_service_binding("churn-agent", agent.id().await?)
let churning = cli
.with_service_binding("churn-agent", agent.id().await?)
.with_service_binding("churn-server", server.id().await?)
.with_exec(vec![
"churn",
"health",
"--server",
"churn-server:3000",
"http://churn-server:3000",
"--agent",
"churn-agent:3000",
])
.exit_code()
.await?;
"http://churn-agent:3000",
]);
let stdout = churning.stdout().await?;
println!("{stdout}");
let stderr = churning.stderr().await?;
println!("{stderr}");
churning.exit_code().await?;
Ok(())
}