From 6e6e27b848b668b0718ede19f1ebff68d1d375b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Felipe=20Gon=C3=A7alves?= Date: Tue, 23 Apr 2024 17:10:23 -0300 Subject: [PATCH] feat(http): introduce `HttpState` --- ctl/src/discovery/mod.rs | 2 +- ctl/src/http/deployer/mod.rs | 10 +++++----- ctl/src/http/mod.rs | 14 ++++++++------ ctl/src/main.rs | 10 +++++----- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/ctl/src/discovery/mod.rs b/ctl/src/discovery/mod.rs index e76957b..15ce6a8 100644 --- a/ctl/src/discovery/mod.rs +++ b/ctl/src/discovery/mod.rs @@ -77,7 +77,7 @@ impl Discovery { } } -#[derive(Clone, Debug)] +#[derive(Clone)] pub struct DiscoveryHandle(mpsc::Sender); impl DiscoveryHandle { diff --git a/ctl/src/http/deployer/mod.rs b/ctl/src/http/deployer/mod.rs index f05dd9a..20fbf3c 100644 --- a/ctl/src/http/deployer/mod.rs +++ b/ctl/src/http/deployer/mod.rs @@ -1,21 +1,21 @@ use axum::{extract::State, Json}; use proto::ctl::deployer::{DeployId, DeployReq, DeployRes, RevisionId}; -use crate::discovery::DiscoveryHandle; +use crate::http::HttpState; pub async fn deploy( - State(discovery_handle): State, + State(state): State, Json(payload): Json, ) -> Json { let revision_id = RevisionId::now_v7(); let mut deploys_id: Vec = Vec::new(); - for _i in 0..payload.service_spec.concurrency { - deploys_id.push(discovery_handle.schedule_deploy(revision_id).await); + for _ in 0..payload.service_spec.concurrency { + deploys_id.push(state.discovery.schedule_deploy(revision_id).await); } tokio::spawn(async move { - let _workers = discovery_handle.query_worker().await; + let _workers = state.discovery.query_worker().await; // TODO: Select worker // TODO: Start deployment on runner }); diff --git a/ctl/src/http/mod.rs b/ctl/src/http/mod.rs index 2320e8c..a0179c0 100644 --- a/ctl/src/http/mod.rs +++ b/ctl/src/http/mod.rs @@ -6,14 +6,16 @@ use crate::discovery::DiscoveryHandle; pub mod deployer; pub mod worker; -pub async fn run_server(discovery_handle: DiscoveryHandle) { +#[derive(Clone)] +pub struct HttpState { + pub discovery: DiscoveryHandle, +} + +pub async fn run_server(state: HttpState) { let app = Router::new() .route("/worker/metrics", post(worker::push_metrics)) - .route( - "/deploy", - post(deployer::deploy), - ) - .with_state(discovery_handle); + .route("/deploy", post(deployer::deploy)) + .with_state(state); let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); info!("HTTP listening at port 3000"); diff --git a/ctl/src/main.rs b/ctl/src/main.rs index 1174e4c..3c3a88e 100644 --- a/ctl/src/main.rs +++ b/ctl/src/main.rs @@ -1,6 +1,6 @@ use tracing::info; -use crate::discovery::Discovery; +use crate::{discovery::Discovery, http::HttpState}; mod discovery; mod http; @@ -8,7 +8,6 @@ mod http; #[tokio::main] async fn main() { setup::tracing(); - info!("started controller"); let (discovery, discovery_handle) = Discovery::new(); @@ -18,10 +17,11 @@ async fn main() { }); let http_handle = tokio::spawn({ - let discovery_handle = discovery_handle.clone(); - + let state = HttpState { + discovery: discovery_handle.clone(), + }; async move { - http::run_server(discovery_handle).await; + http::run_server(state).await; } });