From 158044c320ef328ffad0a150d50b40e103d3f685 Mon Sep 17 00:00:00 2001 From: Joe Carstairs Date: Mon, 8 Jun 2026 09:06:08 +0100 Subject: replaces git repo config with routes config --- src/main.rs | 52 ++++++++++++++++++++-------------------------------- 1 file changed, 20 insertions(+), 32 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 42cfaae..5dd5db5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,47 +4,35 @@ use dirs::config_dir; #[macro_use] extern crate rocket; -mod build; -mod config; +mod routes; -use config::read_config; +use anyhow::Result; +use routes::read_routes; use rocket::{State, http::Status}; -use crate::{build::{BuildError, pull_or_clone_and_build_and_push}, config::Config}; - const BIN_NAME: &str = "build-joeac"; #[launch] fn rocket() -> _ { - let config_path: PathBuf = config_dir().unwrap() - .join(BIN_NAME).join(BIN_NAME).with_added_extension("toml"); - let config = read_config(&config_path).inspect_err(|err| - eprintln!("Failed to read config. Error: {}\nProceeding with defaults", err)) - .unwrap_or(Config::default()); - - rocket::build().manage(config).mount("/", routes![index]) + let routes_path: PathBuf = config_dir().unwrap().join(BIN_NAME).join("routes"); + let routes = read_routes(&routes_path).expect("Failed to read routes config"); + rocket::build().manage(routes).mount("/", routes![index]) } -#[get("/")] -fn index(remote_git_repo: PathBuf, config: &State) -> (Status, String) { - match remote_git_repo.to_str() { - None => (Status::BadRequest, format!("Failed to convert request path to unicode: {:?}", remote_git_repo)), - Some(remote_git_repo) => match config.repositories.iter().find(|repo| repo.remote_git_repo.eq(remote_git_repo)) { - None => (Status::NotFound, format!("Remote git repository not configured: {remote_git_repo}")), - Some(repo) => match pull_or_clone_and_build_and_push(repo) { - Err(BuildError::FailedToOpenOrClone(open_err, clone_err)) => ( - Status::InternalServerError, - format!( - "Failed to open or clone git repository. Error opening: {:#?}\n\nError cloning: {:#?}", - open_err, - clone_err) - ), - Err(BuildError::FailedToPull(err)) => ( - Status::InternalServerError, - format!("Failed to pull git repository. Error: {:#?}", err), - ), - Ok(()) => (Status::Ok, String::from("Pulled, built and pushed image")), - } +#[get("/")] +fn index(req_path: PathBuf, routes: &State>) -> (Status, String) { + match req_path.to_str().map(String::from) { + None => (Status::BadRequest, format!("Failed to convert request path to unicode: {:?}", req_path)), + Some(req_path) => match routes.iter().find(|&route| req_path.eq(route)) { + None => (Status::NotFound, format!("Route not configured: {req_path}")), + Some(route) => match execute_route(route) { + Ok(_) => todo!(), + Err(_) => todo!(), + }, } } } + +fn execute_route(route: &str) -> Result<()> { + todo!(); +} -- cgit v1.2.3