use askama::Template; use async_std::sync::{Arc, Mutex}; use eyre::Result; use tide::prelude::*; mod state; use state::State; use std::ops::Deref; #[derive(Template)] #[template(path = "index.html")] struct IndexTemplate<'a> { state: &'a State, } #[async_std::main] async fn main() -> Result<()> { 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 let mut app = tide::with_state(Arc::new(Mutex::new(state))); app.at("/").get(index); app.at("/*repo").get(|_| async { Ok("bruh") }); app.at("/*repo/tree").get(|_| async { Ok("bruh tree") }); app.listen(("127.0.0.1", port)).await?; Ok(()) } async fn index(req: tide::Request>>) -> tide::Result { let state = req.state(); let state = state.lock_arc().await; for repo in &state.data { println!("path: {:?}", repo.path()); let head = repo.head(); if let Ok(head) = head { println!("head name: {:?}", head.name()); println!("head shorthand: {:?}", head.shorthand()); } } Ok(IndexTemplate { state: state.deref() }.into()) }