diff --git a/.gitignore b/.gitignore index 41c1eff..8f82bfe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target /repos +/config.toml diff --git a/Cargo.lock b/Cargo.lock index 5cbe577..94eee97 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -270,6 +270,10 @@ dependencies = [ name = "fedhub-hooks" version = "0.1.0" +[[package]] +name = "fedhub-shell" +version = "0.1.0" + [[package]] name = "fnv" version = "1.0.6" diff --git a/Cargo.toml b/Cargo.toml index 53bd44b..8c1ea74 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,8 @@ license = "MIT/Apache-2.0" [workspace] members = [ "async-git", + "async-zlib", + "fedhub-shell", "fedhub-hooks", ] diff --git a/Justfile b/Justfile deleted file mode 100644 index e69de29..0000000 diff --git a/async-git/src/plumbing/object.rs b/async-git/src/plumbing/object.rs index 566c27c..d2906b4 100644 --- a/async-git/src/plumbing/object.rs +++ b/async-git/src/plumbing/object.rs @@ -5,7 +5,7 @@ use std::task::{Context, Poll}; use anyhow::Error; use async_zlib::ZlibDecoder; -use tokio::{io::AsyncReadExt, fs::File}; +use tokio::{fs::File, io::AsyncReadExt}; use typenum::U19; use crate::plumbing::Commit; diff --git a/config.sample.toml b/config.sample.toml new file mode 100644 index 0000000..888772b --- /dev/null +++ b/config.sample.toml @@ -0,0 +1,3 @@ +addr = "127.0.0.1:3000" +repo_root = "./repos" +redis_url = "redis://127.0.0.1" diff --git a/config.toml b/config.toml index 888772b..c773a26 100644 --- a/config.toml +++ b/config.toml @@ -1,3 +1,3 @@ -addr = "127.0.0.1:3000" +addr = "127.0.0.1:5000" repo_root = "./repos" redis_url = "redis://127.0.0.1" diff --git a/dev-server.old.rs b/dev-server.old.rs deleted file mode 100644 index 8897975..0000000 --- a/dev-server.old.rs +++ /dev/null @@ -1,55 +0,0 @@ -use std::convert::Infallible; -use std::sync::mpsc::{self}; -use std::time::Duration; -use std::thread; -use std::path::PathBuf; - -use anyhow::Result; -use hyper::{ - service::{make_service_fn, service_fn}, - Body, Response, Server, -}; -use notify::{RecommendedWatcher, RecursiveMode, Watcher}; - -#[tokio::main] -async fn main() -> Result<()> { - let (watch_tx, watch_rx) = mpsc::channel(); - let mut watcher = RecommendedWatcher::new(watch_tx, Duration::from_secs(3))?; - - let path = PathBuf::from("."); - watcher.watch(&path, RecursiveMode::Recursive)?; - - thread::spawn(move || { - loop { - match watch_rx.recv() { - Ok(evt) => { - println!("event: {:?}", evt); - } - Err(err) => eprintln!("watch error: {:?}", err), - } - } - }); - - let make_svc = make_service_fn(move |_conn| { - let fedhub = fedhub.clone(); - let main = move |req| { - let fedhub = fedhub.clone(); - future::ready(match fedhub.handle(req) { - Ok(res) => Ok::<_, Infallible>(res), - Err(err) => { - eprintln!("Error: {:?}", err); - Ok(Response::new(Body::from(format!("Error: {:?}", err)))) - } - }) - }; - - future::ok::<_, Infallible>(service_fn(main)) - }); - - let server = Server::bind(&addr).serve(make_svc); - if let Err(e) = server.await { - eprintln!("server error: {}", e); - } - - Ok(()) -} diff --git a/fedhub-shell/Cargo.toml b/fedhub-shell/Cargo.toml new file mode 100644 index 0000000..8b14711 --- /dev/null +++ b/fedhub-shell/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "fedhub-shell" +version = "0.1.0" +authors = ["Michael Zhang "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/fedhub-shell/src/main.rs b/fedhub-shell/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/fedhub-shell/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/rust-toolchain b/rust-toolchain deleted file mode 100644 index bf867e0..0000000 --- a/rust-toolchain +++ /dev/null @@ -1 +0,0 @@ -nightly diff --git a/src/lib.rs b/src/lib.rs index 72fc235..9bdd735 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,7 +12,7 @@ use std::sync::Arc; use anyhow::Result; use comrak::{markdown_to_html, ComrakOptions}; use git2::{ObjectType, Oid, Repository}; -use hyper::{Body, Request, Response, StatusCode}; +use hyper::{header, Body, Request, Response, StatusCode}; use lazy_static::lazy_static; use packer::Packer; use parking_lot::RwLock; @@ -129,7 +129,7 @@ impl Fedhub { let directories = self.get_dir_list()?; let mut ctx = self.context(); ctx.insert("repos", &directories); - Ok(Response::new(TERA.render("index.html", &ctx)?.into())) + render_response("index.html", &ctx) } pub fn render_repo_index(&self, path: PathBuf, repo: &Repository) -> Result> { @@ -151,7 +151,7 @@ impl Fedhub { })); } ctx.insert("branches", &branches); - return Ok(Response::new(TERA.render("repo_index.html", &ctx)?.into())); + render_response("repo_index.html", &ctx) } pub fn render_repo_tree( @@ -209,9 +209,13 @@ impl Fedhub { entries.push(Entry { name: entry.name().unwrap().to_string(), is_directory, - url + url, }); } + + // sort all the entries by directories first, then by filename + // i'm appending a string version of the inverse of the boolean + // this way directories all start with 0 which gets sorted first entries.sort_by(|left, right| { let mut left_string = (!left.is_directory as u8).to_string(); left_string += &left.name; @@ -219,6 +223,7 @@ impl Fedhub { right_string += &right.name; left_string.cmp(&right_string) }); + ctx.insert("entries", &entries); if let Some(readme) = readme { ctx.insert( @@ -229,7 +234,7 @@ impl Fedhub { }), ); } - return Ok(Response::new(TERA.render("repo_tree.html", &ctx)?.into())); + render_response("repo_tree.html", &ctx) } pub fn render_repo_blob( @@ -259,7 +264,7 @@ impl Fedhub { "lines": contents, }), ); - return Ok(Response::new(TERA.render("repo_blob.html", &ctx)?.into())); + render_response("repo_blob.html", &ctx) } pub fn handle(self, req: Request) -> Result> { @@ -352,3 +357,12 @@ impl Fedhub { .body("not found".into())?) } } + +fn render_response(template: impl AsRef, ctx: &TeraContext) -> Result> { + let template = template.as_ref(); + let body = TERA.render(template, ctx)?; + Ok(Response::builder() + .status(StatusCode::OK) + .header(header::CONTENT_TYPE, "text/html; charset-utf8") + .body(body.into())?) +} diff --git a/templates/index.html b/templates/index.html index 72ec53a..7438a3c 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,5 +1,7 @@ {% extends "layout.html" %} +{% block title %}Home{% endblock %} + {% block content %} {% for repo in repos %}
  • {{ repo }}
  • diff --git a/templates/layout.html b/templates/layout.html index 8ba92b6..5885d71 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -1,7 +1,9 @@ - + + + {% block title %}{% endblock %} diff --git a/templates/repo_blob.html b/templates/repo_blob.html index 14a36c0..55b5246 100644 --- a/templates/repo_blob.html +++ b/templates/repo_blob.html @@ -1,5 +1,7 @@ {% extends "layout.html" %} +{% block title %}{{ repo_name }}: {{ tree_name }}: {{ blob.name }}{% endblock %} + {% block content %}

    {{ repo_name }}: {{ tree_name }}: {{ blob.name }}

    diff --git a/templates/repo_index.html b/templates/repo_index.html index 5b04bbf..5c74da0 100644 --- a/templates/repo_index.html +++ b/templates/repo_index.html @@ -1,12 +1,16 @@ {% extends "layout.html" %} +{% block title %}{{ repo_name }}{% endblock %} + {% block content %}

    {{ repo_name }}

    branches:

    {% endblock %} diff --git a/templates/repo_tree.html b/templates/repo_tree.html index ed5da46..7230064 100644 --- a/templates/repo_tree.html +++ b/templates/repo_tree.html @@ -1,5 +1,7 @@ {% extends "layout.html" %} +{% block title %}{{ repo_name }}: {{ tree_name }}{% if filepath %}: {{ filepath }}{% endif %}{% endblock %} + {% block content %}

    {{ repo_name }}: {{ tree_name }}{% if filepath %}: {{ filepath }}{% endif %}