diff --git a/src/main.rs b/src/main.rs index 369f525..f8a6dbf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ #![forbid(unsafe_code)] +use std::sync::Arc; use std::time::Duration; use axum::http::StatusCode; @@ -43,14 +44,14 @@ async fn main() -> Result<(), Box> { .await .expect("Set KUBECONFIG to a valid kubernetes config."); - let jobs: Api = Api::namespaced(kubernetes_client, "lighthouse"); + // let jobs: Api = Api::namespaced(kubernetes_client, "lighthouse"); // let jobs: Api = Api::default_namespaced(kubernetes_client); - tracing::info!("Using crd: {}", serde_json::to_string(&PipelineRun::crd())?); + // tracing::info!("Using crd: {}", serde_json::to_string(&PipelineRun::crd())?); - let test_run: PipelineRun = serde_json::from_str(EXAMPLE_PIPELINE_RUN)?; + // let test_run: PipelineRun = serde_json::from_str(EXAMPLE_PIPELINE_RUN)?; - let pp = PostParams::default(); - let created_run = jobs.create(&pp, &test_run).await?; + // let pp = PostParams::default(); + // let created_run = jobs.create(&pp, &test_run).await?; let app = Router::new() .route("/hook", post(hook)) @@ -60,7 +61,8 @@ async fn main() -> Result<(), Box> { TraceLayer::new_for_http(), // Add a timeout layer so graceful shutdown can't wait forever. TimeoutLayer::new(Duration::from_secs(600)), - )); + )) + .with_state(AppState { kubernetes_client }); let listener = tokio::net::TcpListener::bind("0.0.0.0:9988").await?; tracing::info!("listening on {}", listener.local_addr().unwrap()); @@ -70,6 +72,11 @@ async fn main() -> Result<(), Box> { Ok(()) } +#[derive(Clone)] +struct AppState { + kubernetes_client: Client, +} + async fn shutdown_signal() { let ctrl_c = async { signal::ctrl_c() diff --git a/src/webhook.rs b/src/webhook.rs index 86f46ef..3b77061 100644 --- a/src/webhook.rs +++ b/src/webhook.rs @@ -1,10 +1,12 @@ use std::future::Future; +use std::sync::Arc; use axum::async_trait; use axum::body::Body; use axum::body::Bytes; use axum::extract::FromRequest; use axum::extract::Request; +use axum::extract::State; use axum::http::HeaderMap; use axum::http::StatusCode; use axum::middleware::Next; @@ -21,11 +23,13 @@ use sha2::Sha256; use tracing::debug; use crate::hook_push::HookPush; +use crate::AppState; type HmacSha256 = Hmac; pub(crate) async fn hook( _headers: HeaderMap, + State(state): State, payload: HookRequest, ) -> (StatusCode, Json) { debug!("REQ: {:?}", payload);