mygit

[UNMAINTAINED] A cgit/webgit alternative, written in Rust
Log | Files | Refs | README | LICENSE

commit 7973f6e1e3ee1bc688d6c4bb262dff21509969c2
parent e22915c559b449c4ffb309ac230262e44ac67ebf
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Tue,  9 Mar 2021 22:17:01 -0800

Add basaic index/repo pages

Diffstat:
M.gitignore | 1+
MCargo.lock | 43+++++++++++++++++++++++++++----------------
MCargo.toml | 13+++++++------
MREADME.md | 6+++---
Msrc/main.rs | 48++++++++++++++++++++++++++++++++++++++++++++----
Mtemplates/index.html | 3++-
Mtemplates/repo.html | 4+++-
7 files changed, 87 insertions(+), 31 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1 +1,2 @@ target/ +repos/ diff --git a/Cargo.lock b/Cargo.lock @@ -119,6 +119,16 @@ dependencies = [ ] [[package]] +name = "askama_tide" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12d2cbee67c51e2d2a33fa1988b2416688ffb1428025806a0ca0337d7df4e5da" +dependencies = [ + "askama", + "tide", +] + +[[package]] name = "async-attributes" version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1107,6 +1117,23 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" [[package]] +name = "mygit" +version = "0.1.0" +dependencies = [ + "askama", + "askama_tide", + "async-std", + "git2", + "once_cell", + "pico-args", + "pulldown-cmark", + "serde", + "syntect", + "tide", + "toml", +] + +[[package]] name = "nb-connect" version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1619,22 +1646,6 @@ dependencies = [ ] [[package]] -name = "starsgit" -version = "0.1.0" -dependencies = [ - "askama", - "async-std", - "git2", - "once_cell", - "pico-args", - "pulldown-cmark", - "serde", - "syntect", - "tide", - "toml", -] - -[[package]] name = "static_assertions" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/Cargo.toml b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "starsgit" +name = "mygit" version = "0.1.0" authors = ["alex wennerberg <alex@alexwennerberg.com>"] edition = "2018" @@ -7,13 +7,14 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +askama = {version = "0.10", features = ["with-tide"]} +askama_tide = "0.13" +async-std = { version = "1.8.0", features = ["attributes"] } git2 = {version="0.13", default-features = false} +once_cell = "1.7.2" pico-args = "0.4" -askama = "0.10" pulldown-cmark = "0.8" -tide = "0.16" -syntect = {version="4.5", default-features = false, features=["html", "regex-onig"]} -async-std = { version = "1.8.0", features = ["attributes"] } serde = { version = "1.0", features = ["derive"] } -once_cell = "1.7.2" +syntect = {version="4.5", default-features = false, features=["html", "regex-onig"]} +tide = "0.16" toml = "0.5.8" diff --git a/README.md b/README.md @@ -1,7 +1,7 @@ -# Mygit -- the world's smallest Git host +# mygit -- the world's smallest Git host -https://public-inbox.org/README.html +A small, self-hosted git forge, with a web viewer for repositories and and a mailing list archive ## Why Rust? -There is IMO no compelling engineering reason to write this in Rust instead of Go. I'm doing so primarily as an exercise and as an opportunity to improve some of the Rust ecosystem. +There is IMO no compelling engineering reason to write this in Rust instead of a language like Go. I'm doing so primarily as an exercise and as an opportunity to improve some of the Rust ecosystem. diff --git a/src/main.rs b/src/main.rs @@ -1,14 +1,18 @@ use tide::Request; +use askama::Template; use tide::prelude::*; +use std::path::Path; use std::time::Instant; use once_cell::sync::OnceCell; use std::fs; use serde::{Serialize, Deserialize}; use pico_args; +use git2::Repository; #[derive(Deserialize, Debug)] pub struct Config { - port: i32 // should be u8 + port: i32, // should be u8 + repo_directory: String } static CONFIG: OnceCell<Config> = OnceCell::new(); @@ -20,9 +24,41 @@ impl Config { } -async fn index(req: Request<()>) -> tide::Result<String> { - let res = "Hello world!".to_owned(); - Ok(res) +#[derive(Template)] +#[template(path = "index.html")] // using the template in this path, relative +struct IndexTemplate { + repos: Vec<Repository> +} + +async fn index(req: Request<()>) -> tide::Result { + let repos = &Config::global().repo_directory; + let paths = fs::read_dir(repos)?; + let mut index_template = IndexTemplate{ + repos: vec![] + }; // TODO replace with map/collect + for path in paths { + let repo = Repository::open(path?.path())?; + index_template.repos.push(repo); + } + Ok(index_template.into()) +} + +#[derive(Template)] +#[template(path = "repo.html")] // using the template in this path, relative +struct RepoHomeTemplate<'a> { + repo: Repository, + readme_text: &'a str +} + +async fn repo_home(req: Request<()>) -> tide::Result { + let repo_path = Path::new(&Config::global().repo_directory).join(req.param("repo_name")?); + // TODO CLEAN PATH! VERY IMPORTANT! DONT FORGET! + let repo = Repository::open(repo_path)?; + let tmpl = RepoHomeTemplate { + repo: repo, + readme_text: "Hello world" + }; + Ok(tmpl.into()) } const HELP: &str = "\ @@ -34,6 +70,7 @@ OPTIONS: -c Path to config file "; + #[async_std::main] async fn main() -> Result<(), std::io::Error> { let mut pargs = pico_args::Arguments::from_env(); @@ -52,6 +89,9 @@ async fn main() -> Result<(), std::io::Error> { tide::log::start(); let mut app = tide::new(); app.at("/").get(index); + app.at("/:repo_name").get(repo_home); + app.at("/:repo_name/log").get(repo_log); + // app.at("/:repo_name/:commit/tree").get(repo_log); app.listen("127.0.0.1:8081").await?; Ok(()) } diff --git a/templates/index.html b/templates/index.html @@ -3,6 +3,7 @@ {% block content %} <h1>Repositories</h1> {% for repo in repos %} - {{ repo.workdir().unwrap().file_name().unwrap().to_str().unwrap() }} + {% let name = repo.workdir().unwrap().file_name().unwrap().to_str().unwrap() %} + <a href="{{name}}">{{ name }}</a> -- description -- {% endfor %} {% endblock %} diff --git a/templates/repo.html b/templates/repo.html @@ -1,5 +1,7 @@ {% extends "base.html" %} {% block content %} - <h1>Repositories</h1> + {% let name = repo.workdir().unwrap().file_name().unwrap().to_str().unwrap() %} + <h1>{{name}}</h1> + Readme text goes here {% endblock %}