aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 795cf41088693b58af1e2dfbd1423d28fec1f270 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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<RwLock<State>> 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<Arc<Mutex<State>>>) -> 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())
}