summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorKjetil Orbekk <kjetil.orbekk@gmail.com>2017-05-25 06:47:48 -0400
committerKjetil Orbekk <kjetil.orbekk@gmail.com>2017-05-25 06:47:48 -0400
commit8d414d7b8fdcad0466f413419d37edc1ebf922d0 (patch)
treefe3b3717bba6ae9fc05a2e8ff78c7a0aeaa3e691 /src/main.rs
parent652f6ad4da9918410780ce9f3187f3d3d1f9278c (diff)
journal: Get unit log
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs31
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);