feat: with enroll
Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
parent
97978df287
commit
8c41e1004c
15
Cargo.lock
generated
15
Cargo.lock
generated
@ -103,6 +103,7 @@ checksum = "f8175979259124331c1d7bf6586ee7e0da434155e4b2d48ec2c8386281d8df39"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"async-trait",
|
"async-trait",
|
||||||
"axum-core",
|
"axum-core",
|
||||||
|
"axum-macros",
|
||||||
"bitflags 1.3.2",
|
"bitflags 1.3.2",
|
||||||
"bytes",
|
"bytes",
|
||||||
"futures-util",
|
"futures-util",
|
||||||
@ -144,6 +145,18 @@ dependencies = [
|
|||||||
"tower-service",
|
"tower-service",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "axum-macros"
|
||||||
|
version = "0.3.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cdca6a10ecad987bda04e95606ef85a5417dcaac1a78455242d72e031e2b6b62"
|
||||||
|
dependencies = [
|
||||||
|
"heck",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn 2.0.29",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "backtrace"
|
name = "backtrace"
|
||||||
version = "0.3.69"
|
version = "0.3.69"
|
||||||
@ -251,6 +264,8 @@ dependencies = [
|
|||||||
"axum",
|
"axum",
|
||||||
"clap",
|
"clap",
|
||||||
"dotenv",
|
"dotenv",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
|
@ -13,7 +13,9 @@ tracing = { version = "0.1", features = ["log"] }
|
|||||||
tracing-subscriber = { version = "0.3.17" }
|
tracing-subscriber = { version = "0.3.17" }
|
||||||
clap = { version = "4.3.4", features = ["derive", "env"] }
|
clap = { version = "4.3.4", features = ["derive", "env"] }
|
||||||
dotenv = { version = "0.15.0" }
|
dotenv = { version = "0.15.0" }
|
||||||
axum = { version = "0.6.18" }
|
axum = { version = "0.6.18", features = ["macros"] }
|
||||||
async-trait = "*"
|
async-trait = "*"
|
||||||
|
serde = {version = "1", features = ["derive"]}
|
||||||
|
serde_json = "1"
|
||||||
|
|
||||||
reqwest = {version = "0.11.20", features = ["json"]}
|
reqwest = {version = "0.11.20", features = ["json"]}
|
||||||
|
@ -13,3 +13,5 @@ tracing-subscriber.workspace = true
|
|||||||
clap.workspace = true
|
clap.workspace = true
|
||||||
dotenv.workspace = true
|
dotenv.workspace = true
|
||||||
axum.workspace = true
|
axum.workspace = true
|
||||||
|
serde.workspace = true
|
||||||
|
serde_json.workspace = true
|
@ -2,10 +2,15 @@ use std::collections::HashMap;
|
|||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use axum::response::IntoResponse;
|
use anyhow::Error;
|
||||||
|
use axum::extract::State;
|
||||||
|
use axum::http::StatusCode;
|
||||||
|
use axum::response::{IntoResponse, Response};
|
||||||
use axum::routing::{get, post};
|
use axum::routing::{get, post};
|
||||||
use axum::{async_trait, Router};
|
use axum::{async_trait, Json, Router};
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use serde_json::json;
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
@ -23,8 +28,17 @@ enum Commands {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
struct AgentService(Arc<dyn AgentServiceTrait + Send + Sync + 'static>);
|
struct AgentService(Arc<dyn AgentServiceTrait + Send + Sync + 'static>);
|
||||||
|
|
||||||
|
impl std::ops::Deref for AgentService {
|
||||||
|
type Target = Arc<dyn AgentServiceTrait + Send + Sync + 'static>;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Default for AgentService {
|
impl Default for AgentService {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self(Arc::new(DefaultAgentService::default()))
|
Self(Arc::new(DefaultAgentService::default()))
|
||||||
@ -43,23 +57,40 @@ impl Default for DefaultAgentService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
impl AgentServiceTrait for DefaultAgentService {
|
|
||||||
async fn enroll(&self, agent: Agent) -> anyhow::Result<String> {
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
trait AgentServiceTrait {
|
trait AgentServiceTrait {
|
||||||
async fn enroll(&self, agent: Agent) -> anyhow::Result<String>;
|
async fn enroll(&self, agent: Agent) -> anyhow::Result<String>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||||
struct Agent {
|
struct Agent {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct AppState {}
|
#[derive(Clone)]
|
||||||
|
struct AppState {
|
||||||
|
agent: AgentService,
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> anyhow::Result<()> {
|
async fn main() -> anyhow::Result<()> {
|
||||||
@ -81,7 +112,9 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
.route("/ping", post(agent_ping))
|
.route("/ping", post(agent_ping))
|
||||||
.route("/events", post(get_tasks)),
|
.route("/events", post(get_tasks)),
|
||||||
)
|
)
|
||||||
.with_state();
|
.with_state(AppState {
|
||||||
|
agent: AgentService::default(),
|
||||||
|
});
|
||||||
|
|
||||||
tracing::info!("churn server listening on {}", host);
|
tracing::info!("churn server listening on {}", host);
|
||||||
axum::Server::bind(&host)
|
axum::Server::bind(&host)
|
||||||
@ -95,8 +128,42 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn enroll() -> impl IntoResponse {
|
enum AppError {
|
||||||
todo!()
|
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))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn agent_ping() -> impl IntoResponse {
|
async fn agent_ping() -> impl IntoResponse {
|
||||||
|
@ -59,7 +59,7 @@ async fn build_container(
|
|||||||
.iter()
|
.iter()
|
||||||
.map(|s| s.to_string())
|
.map(|s| s.to_string())
|
||||||
.collect::<Vec<_>>(),
|
.collect::<Vec<_>>(),
|
||||||
architecture: dagger_rust::build::BuildArchitecture::Arm64,
|
architecture: dagger_rust::build::BuildArchitecture::Amd64,
|
||||||
}],
|
}],
|
||||||
&bin_name,
|
&bin_name,
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user