can configure repositories, hello world response
This commit is contained in:
+11
-9
@@ -1,22 +1,24 @@
|
|||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
fn read_config(path: &str) -> anyhow::Result<Config> {
|
pub fn read_config(path: &PathBuf) -> anyhow::Result<Config> {
|
||||||
let toml = std::fs::read(path)
|
let toml = std::fs::read(path)
|
||||||
.with_context(|| format!("Failed to read config file at {path}"))?;
|
.with_context(|| format!("Failed to read config file at {:#?}", path))?;
|
||||||
let config = toml::from_slice(&toml)
|
let config = toml::from_slice(&toml)
|
||||||
.with_context(|| format!("Failed to parse config file at {path}"))?;
|
.with_context(|| format!("Failed to parse config file at {:#?}", path))?;
|
||||||
Ok(config)
|
Ok(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Debug, Default, Deserialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
repositories: Vec<RepoConfig>,
|
pub repositories: Vec<RepoConfig>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct RepoConfig {
|
pub struct RepoConfig {
|
||||||
remote_git_repo: String,
|
pub remote_git_repo: String,
|
||||||
local_git_repo: String,
|
pub local_git_repo: String,
|
||||||
remote_package_repo: String,
|
pub remote_package_repo: String,
|
||||||
}
|
}
|
||||||
|
|||||||
+27
-4
@@ -1,13 +1,36 @@
|
|||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use dirs::config_dir;
|
||||||
|
|
||||||
#[macro_use] extern crate rocket;
|
#[macro_use] extern crate rocket;
|
||||||
|
|
||||||
mod config;
|
mod config;
|
||||||
|
|
||||||
|
use config::read_config;
|
||||||
|
use rocket::{State, http::Status};
|
||||||
|
|
||||||
|
use crate::config::Config;
|
||||||
|
|
||||||
|
const BIN_NAME: &str = "build-joeac";
|
||||||
|
|
||||||
#[launch]
|
#[launch]
|
||||||
fn rocket() -> _ {
|
fn rocket() -> _ {
|
||||||
rocket::build().mount("/", routes![index])
|
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("/")]
|
#[get("/<remote_git_repo..>")]
|
||||||
fn index() -> &'static str {
|
fn index(remote_git_repo: PathBuf, config: &State<Config>) -> (Status, String) {
|
||||||
"Hello, world!"
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user