use std::ops::Deref; use std::path::Path; use async_std::sync::{Arc, Mutex}; use eyre::Result; use tide::prelude::*; mod state; mod templates; use state::State; #[async_std::main] async fn main() -> Result<()> { tide::log::start(); let matches = clap::App::new("gityeet") .arg(clap::Arg::with_name("root") .help("sets the directory from which repositories will be found") .required(true)) .arg(clap::Arg::with_name("port") .help("sets the port number") .required(true)) .get_matches(); let root = matches.value_of("root").expect("root is required"); let port = matches.value_of("port").expect("port is required"); let port: u16 = port.parse().expect("port was not valid"); let state = State::discover(root).await?; // unfortunately, the State has to be both Send and Sync, but git2::Repository is not Sync // and even an Arc> isn't Sync if State isn't Sync, it has to be a Mutex to get Sync // TODO do something smarter for that like maybe just a vec of repo paths let mut app = tide::with_state(Arc::new(Mutex::new(state))); app.at("/").get(index); { let mut repo = app.at("/*repo/"); repo.get(about); repo.at("tree/").get(|_| async { Ok("bruh tree") }); } app.listen(("127.0.0.1", port)).await?; Ok(()) } type Request = tide::Request>>; async fn index(req: Request) -> tide::Result { let state = req.state(); let state = state.lock_arc().await; Ok(templates::Index { state: state.deref() }.into()) } async fn about(req: Request) -> tide::Result { let repo_rel_path = req.param("repo").expect("no repo in repo-based URL?"); let state = req.state(); let state = state.lock_arc().await; let repo = state.data.iter() .find(|repo| state.relative_path(repo.path()) == Path::new(repo_rel_path)); let repo = match repo { Some(x) => x, None => return Ok("bruh that's not a real repo".into()), }; let head = repo.head(); let head = match head { Ok(x) => x, Err(err) => return Ok(format!("bruh that repo has no HEAD: {}", err).into()), }; let head_tree = head.peel_to_tree(); let head_tree = match head_tree { Ok(x) => x, Err(err) => return Ok(format!("bruh that repo's HEAD has no tree: {}", err).into()), }; let tree_readme = head_tree.get_path(Path::new("README.md")); let tree_readme = match tree_readme { Ok(x) => x, Err(err) => return Ok(format!("that repo's HEAD tree has no README.md: {}", err).into()), }; let tree_readme_object = tree_readme.to_object(repo); let tree_readme_object = match tree_readme_object { Ok(x) => x, Err(err) => return Ok(format!("that repo's HEAD tree has no README.md: {}", err).into()), }; let head_readme_blob = tree_readme_object.into_blob(); let head_readme_blob = match head_readme_blob { Ok(x) => x, Err(_) => return Ok("somehow that README wasn't a file".into()), }; let readme = String::from_utf8_lossy(head_readme_blob.content()); Ok(templates::About { path: repo_rel_path, readme }.into()) }