replaces git repo config with routes config

This commit is contained in:
2026-06-08 09:06:08 +01:00
parent 1277a0733f
commit 158044c320
6 changed files with 143 additions and 109 deletions
+20 -32
View File
@@ -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("/<remote_git_repo..>")]
fn index(remote_git_repo: PathBuf, config: &State<Config>) -> (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("/<req_path..>")]
fn index(req_path: PathBuf, routes: &State<Vec<String>>) -> (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!();
}