initial commit

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2019-01-10 16:08:35 +08:00
commit 72c88f45a6
4 changed files with 1396 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target
**/*.rs.bk

1274
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

25
Cargo.toml Normal file
View File

@ -0,0 +1,25 @@
[package]
name = "reveal-sync"
version = "0.1.0"
authors = ["Sean Cross <sean@xobs.io>"]
edition = "2018"
#[dependencies]
#clap = "2.25"
#error-type = "0.1"
#futures = "0.1"
#futures-cpupool = "0.1"
#git2 = "0.8"
#http = "0.1"
#hyper = "0.12"
##hyper-staticfile = "0.3"
#serde_json = "1.0"
#websocket = "0.22"
[dependencies]
git2 = "0.8"
iron = "0.6"
mount = "0.4"
serde_json = "1.0"
staticfile = "0.5"
websocket = "0.22"

95
src/main.rs Normal file
View File

@ -0,0 +1,95 @@
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<String>,
}
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<Response> {
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();
}