diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/src/main.rs b/src/main.rs index b4a8f38..f96ebdc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ // CSRF protection // https://github.com/heartsucker/iron-csrf +#[macro_use] extern crate iron; #[macro_use] extern crate log; @@ -9,6 +10,7 @@ extern crate env_logger; extern crate systemhttp; extern crate iron_sessionstorage; extern crate staticfile; +extern crate regex; use iron_sessionstorage::traits::*; use iron_sessionstorage::SessionStorage; @@ -19,8 +21,10 @@ use iron::headers::ContentType; use iron::{Iron, Request, IronResult, Response, Chain}; use router::Router; use systemhttp::systemd::unit; +use systemhttp::systemd::journal; use systemhttp::render; use staticfile::Static; +use regex::Regex; struct Aaa(String); @@ -36,8 +40,7 @@ impl iron_sessionstorage::Value for Aaa { } } - -fn hello(r: &mut Request) -> IronResult<Response> { +fn overview(r: &mut Request) -> IronResult<Response> { let mut _value = match try!(r.session().get::<Aaa>()) { Some(aaa) => aaa, None => Aaa("".to_owned()), @@ -73,13 +76,31 @@ fn hello(r: &mut Request) -> IronResult<Response> { render::system_status(&units_by_section)))) } -fn main() { +fn journal(r: &mut Request) -> IronResult<Response> { + let unit = iexpect!(r.extensions + .get::<Router>() + .unwrap() + .find("unit"), status::BadRequest); + info!("got here"); + let re = Regex::new(r"[-_\w\d]*").unwrap(); + if !re.is_match(unit) { + return Ok(Response::with( + (status::BadRequest, + format!("Unit ({}) does not match {}", + unit, re)))); + } + info!("got here"); + Ok(Response::with((status::Ok, + itry!(journal::get_log(unit, 100))))) +} +fn main() { env_logger::init().unwrap(); let secret = b"secret2".to_vec(); let router = router!( - root: get "/" => hello, - name: get "/:name" => hello, + root: get "/" => overview, + name: get "/:name" => overview, + journal: get "/journal/:unit" => journal, css: get "/static/main.css" => Static::new(""), ); let mut chain = Chain::new(router); |