From 809732e7f3de1bf5f0220c841afd682f2f8d73a7 Mon Sep 17 00:00:00 2001 From: beckerinj Date: Sun, 13 Nov 2022 23:29:12 -0500 Subject: [PATCH] build route --- periphery/src/api/build.rs | 40 ++++++++++++++++++++++++++++++++++ periphery/src/api/container.rs | 4 +--- periphery/src/api/mod.rs | 1 + 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/periphery/src/api/build.rs b/periphery/src/api/build.rs index 8b1378917..5b6c953a1 100644 --- a/periphery/src/api/build.rs +++ b/periphery/src/api/build.rs @@ -1 +1,41 @@ +use anyhow::anyhow; +use axum::{routing::post, Extension, Json, Router}; +use helpers::{docker, handle_anyhow_error}; +use types::{Build, DockerToken, Log, PeripheryConfig}; +use crate::PeripheryConfigExtension; + +pub fn router() -> Router { + Router::new().route( + "/", + post(|config, build| async move { + build_image(config, build) + .await + .map_err(handle_anyhow_error) + }), + ) +} + +async fn build_image( + Extension(config): PeripheryConfigExtension, + Json(build): Json, +) -> anyhow::Result>> { + let docker_token = get_docker_token(&build.docker_account, &config)?; + let logs = docker::build(&build, &config.repo_dir, docker_token).await?; + Ok(Json(logs)) +} + +fn get_docker_token( + docker_account: &Option, + config: &PeripheryConfig, +) -> anyhow::Result> { + match docker_account { + Some(account) => match config.docker_accounts.get(account) { + Some(token) => Ok(Some(token.to_owned())), + None => Err(anyhow!( + "did not find token in config for docker account {account} " + )), + }, + None => Ok(None), + } +} diff --git a/periphery/src/api/container.rs b/periphery/src/api/container.rs index 204be1285..0ab9c4585 100644 --- a/periphery/src/api/container.rs +++ b/periphery/src/api/container.rs @@ -42,9 +42,7 @@ pub fn router() -> Router { .route( "/remove", post(|Json(container): Json| async move { - Json( - docker::stop_and_remove_container(&to_monitor_name(&container.name)).await, - ) + Json(docker::stop_and_remove_container(&to_monitor_name(&container.name)).await) }), ) .route( diff --git a/periphery/src/api/mod.rs b/periphery/src/api/mod.rs index 59bb9336d..641661f2c 100644 --- a/periphery/src/api/mod.rs +++ b/periphery/src/api/mod.rs @@ -14,4 +14,5 @@ pub fn router() -> Router { .nest("/network", network::router()) .nest("/stats", stats::router()) .nest("/git", git::router()) + .nest("/build", build::router()) }