This commit is contained in:
Michael Zhang 2020-04-21 21:32:51 -05:00
parent 7f8370b67e
commit 83173ac684
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
17 changed files with 58 additions and 66 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
/target /target
/repos /repos
/config.toml

4
Cargo.lock generated
View file

@ -270,6 +270,10 @@ dependencies = [
name = "fedhub-hooks" name = "fedhub-hooks"
version = "0.1.0" version = "0.1.0"
[[package]]
name = "fedhub-shell"
version = "0.1.0"
[[package]] [[package]]
name = "fnv" name = "fnv"
version = "1.0.6" version = "1.0.6"

View file

@ -8,6 +8,8 @@ license = "MIT/Apache-2.0"
[workspace] [workspace]
members = [ members = [
"async-git", "async-git",
"async-zlib",
"fedhub-shell",
"fedhub-hooks", "fedhub-hooks",
] ]

View file

View file

@ -5,7 +5,7 @@ use std::task::{Context, Poll};
use anyhow::Error; use anyhow::Error;
use async_zlib::ZlibDecoder; use async_zlib::ZlibDecoder;
use tokio::{io::AsyncReadExt, fs::File}; use tokio::{fs::File, io::AsyncReadExt};
use typenum::U19; use typenum::U19;
use crate::plumbing::Commit; use crate::plumbing::Commit;

3
config.sample.toml Normal file
View file

@ -0,0 +1,3 @@
addr = "127.0.0.1:3000"
repo_root = "./repos"
redis_url = "redis://127.0.0.1"

View file

@ -1,3 +1,3 @@
addr = "127.0.0.1:3000" addr = "127.0.0.1:5000"
repo_root = "./repos" repo_root = "./repos"
redis_url = "redis://127.0.0.1" redis_url = "redis://127.0.0.1"

View file

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

9
fedhub-shell/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "fedhub-shell"
version = "0.1.0"
authors = ["Michael Zhang <iptq@protonmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

3
fedhub-shell/src/main.rs Normal file
View file

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

View file

@ -1 +0,0 @@
nightly

View file

@ -12,7 +12,7 @@ use std::sync::Arc;
use anyhow::Result; use anyhow::Result;
use comrak::{markdown_to_html, ComrakOptions}; use comrak::{markdown_to_html, ComrakOptions};
use git2::{ObjectType, Oid, Repository}; use git2::{ObjectType, Oid, Repository};
use hyper::{Body, Request, Response, StatusCode}; use hyper::{header, Body, Request, Response, StatusCode};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use packer::Packer; use packer::Packer;
use parking_lot::RwLock; use parking_lot::RwLock;
@ -129,7 +129,7 @@ impl Fedhub {
let directories = self.get_dir_list()?; let directories = self.get_dir_list()?;
let mut ctx = self.context(); let mut ctx = self.context();
ctx.insert("repos", &directories); 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<Response<Body>> { pub fn render_repo_index(&self, path: PathBuf, repo: &Repository) -> Result<Response<Body>> {
@ -151,7 +151,7 @@ impl Fedhub {
})); }));
} }
ctx.insert("branches", &branches); 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( pub fn render_repo_tree(
@ -209,9 +209,13 @@ impl Fedhub {
entries.push(Entry { entries.push(Entry {
name: entry.name().unwrap().to_string(), name: entry.name().unwrap().to_string(),
is_directory, 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| { entries.sort_by(|left, right| {
let mut left_string = (!left.is_directory as u8).to_string(); let mut left_string = (!left.is_directory as u8).to_string();
left_string += &left.name; left_string += &left.name;
@ -219,6 +223,7 @@ impl Fedhub {
right_string += &right.name; right_string += &right.name;
left_string.cmp(&right_string) left_string.cmp(&right_string)
}); });
ctx.insert("entries", &entries); ctx.insert("entries", &entries);
if let Some(readme) = readme { if let Some(readme) = readme {
ctx.insert( 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( pub fn render_repo_blob(
@ -259,7 +264,7 @@ impl Fedhub {
"lines": contents, "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<Body>) -> Result<Response<Body>> { pub fn handle(self, req: Request<Body>) -> Result<Response<Body>> {
@ -352,3 +357,12 @@ impl Fedhub {
.body("not found".into())?) .body("not found".into())?)
} }
} }
fn render_response(template: impl AsRef<str>, ctx: &TeraContext) -> Result<Response<Body>> {
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())?)
}

View file

@ -1,5 +1,7 @@
{% extends "layout.html" %} {% extends "layout.html" %}
{% block title %}Home{% endblock %}
{% block content %} {% block content %}
{% for repo in repos %} {% for repo in repos %}
<li><a href="/{{ repo }}">{{ repo }}</a></li> <li><a href="/{{ repo }}">{{ repo }}</a></li>

View file

@ -1,7 +1,9 @@
<html> <!DOCTYPE html>
<html lang="en">
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="fedhub is a work-in-progress federated git forge.">
<title>{% block title %}{% endblock %}</title> <title>{% block title %}{% endblock %}</title>

View file

@ -1,5 +1,7 @@
{% extends "layout.html" %} {% extends "layout.html" %}
{% block title %}{{ repo_name }}: {{ tree_name }}: {{ blob.name }}{% endblock %}
{% block content %} {% block content %}
<h1>{{ repo_name }}: {{ tree_name }}: {{ blob.name }}</h1> <h1>{{ repo_name }}: {{ tree_name }}: {{ blob.name }}</h1>

View file

@ -1,12 +1,16 @@
{% extends "layout.html" %} {% extends "layout.html" %}
{% block title %}{{ repo_name }}{% endblock %}
{% block content %} {% block content %}
<h1>{{ repo_name }}</h1> <h1>{{ repo_name }}</h1>
<h3>branches:</h3> <h3>branches:</h3>
<ul> <ul>
{% for branch in branches %} {% for branch in branches %}
<li>
<a href="{{ branch.url | safe }}">{{ branch.name }}</a> <a href="{{ branch.url | safe }}">{{ branch.name }}</a>
</li>
{% endfor %} {% endfor %}
</ul> </ul>
{% endblock %} {% endblock %}

View file

@ -1,5 +1,7 @@
{% extends "layout.html" %} {% extends "layout.html" %}
{% block title %}{{ repo_name }}: {{ tree_name }}{% if filepath %}: {{ filepath }}{% endif %}{% endblock %}
{% block content %} {% block content %}
<h1>{{ repo_name }}: {{ tree_name }}{% if filepath %}: {{ filepath }}{% endif %}</h1> <h1>{{ repo_name }}: {{ tree_name }}{% if filepath %}: {{ filepath }}{% endif %}</h1>