aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 0710d40c65f9d6adf522e82ce04a40cb458ff100 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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<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 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<Arc<Mutex<State>>>;

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())
}