Skip to content

Commit

Permalink
feat(http): introduce HttpState
Browse files Browse the repository at this point in the history
  • Loading branch information
lffg committed Apr 23, 2024
1 parent c75eb16 commit 6e6e27b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion ctl/src/discovery/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl Discovery {
}
}

#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct DiscoveryHandle(mpsc::Sender<Msg>);

impl DiscoveryHandle {
Expand Down
10 changes: 5 additions & 5 deletions ctl/src/http/deployer/mod.rs
Original file line number Diff line number Diff line change
@@ -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<DiscoveryHandle>,
State(state): State<HttpState>,
Json(payload): Json<DeployReq>,
) -> Json<DeployRes> {
let revision_id = RevisionId::now_v7();

let mut deploys_id: Vec<DeployId> = 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
});
Expand Down
14 changes: 8 additions & 6 deletions ctl/src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
10 changes: 5 additions & 5 deletions ctl/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use tracing::info;

use crate::discovery::Discovery;
use crate::{discovery::Discovery, http::HttpState};

mod discovery;
mod http;

#[tokio::main]
async fn main() {
setup::tracing();

info!("started controller");

let (discovery, discovery_handle) = Discovery::new();
Expand All @@ -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;
}
});

Expand Down

0 comments on commit 6e6e27b

Please sign in to comment.