Repositories

grarr

(mirrored on github)

Wim Looman <wim@nemo157.com>
13eb67 Add initial gh-pages support
Wim Looman committed at 2016-03-11 12:44:44

Modified src/handler/mod.rs

@@ -19,6 +19,7 @@ mod settings;
mod about;
mod tree;
mod blob;
mod pages;
pub use self::avatar::Avatars;
pub use self::review::Review;
@@ -29,6 +30,7 @@ pub use self::repository::Repository;
pub use self::repositories::Repositories;
pub use self::tree::Tree;
pub use self::blob::Blob;
pub use self::pages::Pages;
pub use self::register::Register;
pub use self::statics::Static;

Added src/handler/pages.rs

@@ -0,0 +1,57 @@
use super::base::*;
use tree_entry;
use git2;
#[derive(Clone)]
pub struct Pages;
impl Handler for Pages {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
{
let mut context = itry!(req.extensions.get_mut::<RepositoryContext>().ok_or(Error::MissingExtension), status::InternalServerError);
context.reference = Some("gh-pages".to_owned());
}
let context = itry!(req.extensions.get::<RepositoryContext>().ok_or(Error::MissingExtension), status::InternalServerError);
let router = itry!(req.extensions.get::<Router>().ok_or(Error::MissingExtension), status::InternalServerError);
let path = router.find("path").unwrap_or("");
let tree_entry::TreeEntryContext {
entry,
entry_path: mut path,
..
} = try!(tree_entry::get_tree_entry(&context, path));
let mut new_entry = None;
if path.ends_with('/') {
if let Some(git2::ObjectType::Tree) = entry.kind() {
if let Some(tree_entry) = entry.as_tree().unwrap().get_name("index.html") {
new_entry = Some(itry!(tree_entry.to_object(&context.repository), status::InternalServerError));
path = path + "index.html";
}
}
}
let entry = new_entry.unwrap_or(entry);
match entry.kind() {
Some(git2::ObjectType::Blob) => {
Ok(Response::with((status::Ok, utils::mime(&*path), entry.as_blob().unwrap().content())))
},
_ => {
Err(IronError::new(Error::from("Not found"), status::NotFound))
},
}
}
}
impl Route for Pages {
fn method() -> Method {
Method::Get
}
fn routes() -> Vec<Cow<'static, str>> {
vec![
"/pages/".into(),
"/pages/*path".into(),
]
}
}

Modified src/handler/utils.rs

@@ -51,6 +51,7 @@ impl fmt::Debug for File {
pub fn mime(path: &str) -> Mime {
match Path::new(path).extension().and_then(|s| s.to_str()) {
Some("css") => mime!(Text/Css),
Some("html") => mime!(Text/Html),
Some("js") => mime!(Text/Javascript),
None | Some(_) => mime!(Application/("octet-stream")),
}

Modified src/main.rs

@@ -88,6 +88,7 @@ fn main() {
.register(handler::About)
.register(inject_repository_context(&config.repos.root, handler::Tree))
.register(inject_repository_context(&config.repos.root, handler::Blob))
.register(inject_repository_context(&config.repos.root, handler::Pages))
.register(statics![
prefix: "./static/";
"./static/js/highlight.js",

Modified src/repository_context.rs

@@ -18,7 +18,7 @@ use referenced_commit::ReferencedCommit;
pub struct RepositoryContext {
pub path: String,
pub repository: git2::Repository,
reference: Option<String>,
pub reference: Option<String>,
}
impl Key for RepositoryContext {