extern crate git2; extern crate iron; extern crate mount; extern crate serde_json; extern crate staticfile; // This example serves the docs from target/doc/staticfile at /doc/ // // Run `cargo doc && cargo run --example doc_server`, then // point your browser to http://127.0.0.1:3000/doc/ use std::path::{Path, PathBuf}; use iron::{Iron, IronResult, status}; use iron::prelude::{Chain, Request, Response}; use mount::Mount; use staticfile::Static; use git2::Repository; struct WebHookConfig { secret: String, repo_root: PathBuf, repo_prefixes: Vec, } fn redeploy_repo(url: &str, path: &Path) -> Result<(), ()> { eprintln!("Cloning {}", url); let repo = match Repository::clone(url, path) { Ok(repo) => repo, Err(e) => panic!("failed to clone: {}", e), }; Ok(()) } fn webhook(req: &mut Request, config: &WebHookConfig) -> IronResult { eprintln!("Webhook Request: {:?}", req); let v: serde_json::Value = serde_json::from_reader(&mut req.body).unwrap(); if let Some(found_secret) = v.get("secret") { if found_secret != &config.secret { return Ok(Response::with(status::Unauthorized)); } } else { return Ok(Response::with(status::Unauthorized)); } if let Some(repository) = v.get("repository") { if let Some(html_url) = repository.get("html_url") { if let Some(html_url_str) = html_url.as_str() { if html_url_str.starts_with("https://git.xobs.io/xobs") { redeploy_repo(html_url_str, &Path::new("C:\\Code\\talkserved")); } else { eprintln!("HTML URL isn't a string"); } } else { eprintln!("URL doesn't start with match"); } } else { eprintln!("No HTML URL found"); } } else { eprintln!("No repository key found"); } // eprintln!("Value: {:?}", v); Ok(Response::with(status::Ok)) } fn main() { let mut mount = Mount::new(); let hostaddr = "0.0.0.0:9119".to_owned(); let config = WebHookConfig { secret: "1234".to_owned(), repo_root: Path::new("D:\\Code\\talkserved").to_path_buf(), repo_prefixes: vec!["https://git.xobs.io/xobs".to_owned()], }; // Serve the shared JS/CSS at / mount.mount("/", Static::new(config.repo_root.clone())); // Listen for calls to "/webhook" and process accordingly mount.mount("/webhook", Chain::new(move |req: &mut Request| { webhook(req, &config) })); println!("Doc server running on http://{}", hostaddr); Iron::new(mount).http(hostaddr).unwrap(); }