churn/crates/churn-server/src/main.rs

113 lines
2.4 KiB
Rust
Raw Normal View History

use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::Arc;
use axum::response::IntoResponse;
use axum::routing::{get, post};
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)]
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,
},
}
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();
tracing_subscriber::fmt::init();
let cli = Command::parse();
match cli.command {
Some(Commands::Serve { host }) => {
tracing::info!("Starting churn server");
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!("churn server listening on {}", host);
axum::Server::bind(&host)
.serve(app.into_make_service())
.await
.unwrap();
}
None => {}
}
Ok(())
}
async fn enroll() -> impl IntoResponse {
todo!()
}
async fn agent_ping() -> impl IntoResponse {
todo!()
}
async fn get_tasks() -> impl IntoResponse {
todo!()
}
async fn ping() -> impl IntoResponse {
"pong!"
}