summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 215f9e69fdc899bb8b5d12d735c90b899e071ab3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use std::path::PathBuf;

use dirs::config_dir;

#[macro_use] extern crate rocket;

mod config;

use config::read_config;
use rocket::{State, http::Status};

use crate::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])
}

#[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) => (Status::Ok, format!("Matched! {:?}", repo))
        }
    }
}