build route

This commit is contained in:
beckerinj
2022-11-13 23:29:12 -05:00
parent a7abca2038
commit 809732e7f3
3 changed files with 42 additions and 3 deletions
+40
View File
@@ -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<Build>,
) -> anyhow::Result<Json<Vec<Log>>> {
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<String>,
config: &PeripheryConfig,
) -> anyhow::Result<Option<DockerToken>> {
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),
}
}
+1 -3
View File
@@ -42,9 +42,7 @@ pub fn router() -> Router {
.route(
"/remove",
post(|Json(container): Json<Container>| 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(
+1
View File
@@ -14,4 +14,5 @@ pub fn router() -> Router {
.nest("/network", network::router())
.nest("/stats", stats::router())
.nest("/git", git::router())
.nest("/build", build::router())
}