From 2570644a90490c031febb08870545880b3f5b7d0 Mon Sep 17 00:00:00 2001 From: Niko Reunanen Date: Sat, 25 Jan 2025 01:53:50 +0200 Subject: [PATCH] Add git2 to access git --- Cargo.toml | 1 + src/bin/main.rs | 38 +++++++++++++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b079c3a..a96416c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" [dependencies] axum = { version = "0.8.1" } bollard = { version = "0.18.1", registry = "cratesio" } +git2 = { version = "0.20.0" } tokio = { version = "1.43.0", features = ["full"] } tower-http = { version = "0.6.2", features = ["trace"] } tracing = { version = "0.1.41" } diff --git a/src/bin/main.rs b/src/bin/main.rs index 7100848..e424ef1 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -1,10 +1,32 @@ use std::process::ExitCode; -use axum::Router; +use axum::{routing::get, Router}; use bollard::Docker; +use git2::Repository; use tower_http::trace::TraceLayer; use tracing::level_filters::LevelFilter; +#[tokio::main] +async fn download_repository(url: &str, name: &str) { + let folder = format!("/tmp/{name}"); + + match tokio::fs::try_exists(&folder).await { + Ok(true) => { + let repo = Repository::open(folder).unwrap(); + repo.find_remote("origin") + .unwrap() + .fetch(&["main"], None, None) + .unwrap(); + } + Ok(false) => { + Repository::clone(url, folder).unwrap(); + } + Err(error) => { + tracing::error!("{error}"); + } + }; +} + #[tokio::main] async fn main() -> ExitCode { let docker = match Docker::connect_with_socket_defaults() { @@ -33,7 +55,7 @@ async fn main() -> ExitCode { return ExitCode::FAILURE; } - tracing::info!("Launching microdeploy on {bind}"); + tracing::info!("launching microdeploy on {bind}"); let listener = match tokio::net::TcpListener::bind(&bind).await { Ok(listener) => listener, @@ -43,7 +65,17 @@ async fn main() -> ExitCode { } }; - let app = Router::new().layer(TraceLayer::new_for_http()); + let app = Router::new() + .route( + "/api/v1/image/build", + get({ + download_repository("URL", "microdeploy"); + + "todo" + }), + ) + .route("/api/v1/image/serve", get("todo")) + .layer(TraceLayer::new_for_http()); match axum::serve(listener, app).await { Ok(()) => ExitCode::SUCCESS,