2023-08-26 22:32:38 +02:00
|
|
|
mod agent;
|
|
|
|
|
2023-08-24 17:22:45 +02:00
|
|
|
use std::net::SocketAddr;
|
|
|
|
|
2023-08-26 22:32:38 +02:00
|
|
|
use agent::AgentService;
|
|
|
|
use anyhow::Error;
|
|
|
|
use axum::{
|
|
|
|
extract::State,
|
|
|
|
http::StatusCode,
|
|
|
|
response::{IntoResponse, Response},
|
|
|
|
routing::{get, post},
|
|
|
|
Json, Router,
|
|
|
|
};
|
|
|
|
use churn_domain::AgentEnrollReq;
|
2023-08-24 17:22:45 +02:00
|
|
|
use clap::{Parser, Subcommand};
|
2023-08-26 22:32:38 +02:00
|
|
|
use serde_json::json;
|
2024-04-06 21:30:49 +02:00
|
|
|
use tokio::net::TcpListener;
|
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 {
|
|
|
|
Daemon {
|
|
|
|
#[arg(env = "CHURN_ADDR", long)]
|
|
|
|
host: SocketAddr,
|
|
|
|
},
|
|
|
|
|
|
|
|
Connect {
|
|
|
|
/// agent name is the hostname which other agents or servers can resolve and connect via. It should be unique
|
|
|
|
#[arg(env = "CHURN_AGENT_NAME", long)]
|
|
|
|
agent_name: String,
|
|
|
|
|
|
|
|
#[arg(env = "CHURN_ADDR", long)]
|
|
|
|
host: SocketAddr,
|
|
|
|
|
|
|
|
#[arg(env = "CHURN_TOKEN", long)]
|
|
|
|
token: String,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-08-27 19:57:22 +02:00
|
|
|
#[derive(Clone, Default)]
|
2023-08-26 22:32:38 +02:00
|
|
|
struct AppState {
|
|
|
|
agent: AgentService,
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
handle_command(cli).await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn handle_command(cmd: Command) -> anyhow::Result<()> {
|
|
|
|
match cmd.command {
|
|
|
|
Some(Commands::Daemon { host }) => {
|
2023-08-25 21:52:28 +02:00
|
|
|
tracing::info!("Starting churn server");
|
|
|
|
|
2023-08-26 22:32:38 +02:00
|
|
|
let app = Router::new()
|
|
|
|
.route("/enroll", post(enroll))
|
|
|
|
.route("/ping", get(ping))
|
|
|
|
.with_state(AppState::default());
|
2023-08-25 21:52:28 +02:00
|
|
|
|
|
|
|
tracing::info!("churn server listening on {}", host);
|
2024-04-06 21:30:49 +02:00
|
|
|
let listener = TcpListener::bind(&host).await?;
|
|
|
|
axum::serve(listener, app.into_make_service())
|
2023-08-25 21:52:28 +02:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2023-08-24 17:22:45 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
Some(Commands::Connect {
|
2023-08-26 22:32:38 +02:00
|
|
|
host: _,
|
|
|
|
token: _,
|
|
|
|
agent_name: _,
|
2023-08-24 17:22:45 +02:00
|
|
|
}) => todo!(),
|
|
|
|
None => todo!(),
|
|
|
|
}
|
|
|
|
}
|
2023-08-25 21:52:28 +02:00
|
|
|
|
2023-08-26 22:32:38 +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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-25 21:52:28 +02:00
|
|
|
async fn ping() -> impl IntoResponse {
|
|
|
|
"pong!"
|
|
|
|
}
|
2023-08-26 22:32:38 +02:00
|
|
|
|
|
|
|
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(())
|
|
|
|
}
|