From 94b9733730de48cb10631f1e805f1661c2405d41 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Sun, 28 May 2017 13:49:26 -0400 Subject: [PATCH] Initial server commit --- .gitignore | 5 ++++ Cargo.toml | 9 ++++++ src/main.rs | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ab8f0bd --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +target/ +**/*.rs.bk +Cargo.lock +target/ +**/*.rs.bk diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..8bfc669 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "icicle" +version = "0.1.0" +authors = ["Bradlee Speice "] + +[dependencies] +router = "*" +iron = "*" +urlencoded = "*" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..b193346 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,86 @@ +#[macro_use(router)] +extern crate router; +extern crate iron; +extern crate urlencoded; + +use iron::{status, Response, Request, Iron, IronResult}; +use iron::prelude::*; +use urlencoded::UrlEncodedQuery; +use std::process::Command; +use std::{thread, time}; + +fn respond(ret_status: status::Status, message: &str) -> IronResult { + Ok(Response::with(( + ret_status, + message + ))) +} + +fn index(_: &mut Request) -> IronResult { + respond(status::Ok, "You found the index!") +} + +fn remote_run(remote: &str, actions: &Vec) -> IronResult { + let actions: Vec<&str> = actions[0].split(',').collect(); + //let message = format!("Actions: {:?}", actions); + let mut status = Some(0); + for action in actions { + let output = Command::new("irsend") + .arg("SEND_ONCE") + .arg(remote) + .arg(action) + .output(); + + match output { + Ok(ref out) => status = if !out.status.success() { out.status.code() } else { status }, + Err(_) => status = None + } + + thread::sleep(time::Duration::from_secs(1)); + + } + let message = match status { + Some(code) => if code == 0 { "Success" } else { "Failure" }, + None => "Could not find irsend program" + }; + respond(status::Ok, message) +} + +fn remote(req: &mut Request) -> IronResult { + let url_ref = req.url.clone(); + let remote_name = url_ref.path()[0]; + + let query_ref = match req.url.query() { + Some(_) => req.get_ref::(), + None => return respond( + status::BadRequest, + "Please add `actions` to the query params" + ) + }; + + let actions = match query_ref { + Ok(ref hashmap) => hashmap.get("actions"), + Err(ref e) => return respond( + status::BadRequest, + format!("{:?}", e).as_str() + ) + }; + + match actions { + Some(act) => remote_run(remote_name, act), + None => respond( + status::BadRequest, + format!("No actions specified!").as_str() + ) + } +} + + +fn main() { + let router = router!( + index: get "/" => index, + remote: get "/:remote" => remote + ); + + Iron::new(router).http("localhost:3000").unwrap(); +}