summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorKjetil Orbekk <kjetil.orbekk@gmail.com>2017-05-21 22:50:34 -0400
committerKjetil Orbekk <kjetil.orbekk@gmail.com>2017-05-21 22:50:34 -0400
commit4252fded06dc4c84142e0ade06c88a034ef2872c (patch)
treebd4c128b4b5da1f6283d39c2766073222a0ba481 /src/main.rs
Simple webapp with iron and horrorshow.
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..0893825
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,59 @@
+extern crate iron;
+#[macro_use]
+extern crate log;
+#[macro_use]
+extern crate router;
+extern crate env_logger;
+#[macro_use]
+extern crate horrorshow;
+
+use iron::status;
+use iron::modifiers::Header;
+use iron::headers::ContentType;
+use iron::{Iron, Request, IronResult, Response};
+use router::Router;
+
+use horrorshow::prelude::*;
+use horrorshow::Raw;
+
+use std::process::Command;
+
+fn render_message(message: &str) -> String {
+ (html!{
+ : Raw("<!DOCTYPE html>");
+ html {
+ head { title: "Title"; }
+ body {
+ p {
+ : message
+ }
+ }
+ }
+ })
+ .into_string()
+ .unwrap()
+}
+
+fn hello(r: &mut Request) -> IronResult<Response> {
+ let name = r.extensions
+ .get::<Router>()
+ .unwrap()
+ .find("name")
+ .unwrap_or("World");
+ let output = Command::new("date").output().unwrap().stdout;
+ let date = String::from_utf8_lossy(&output);
+
+ Ok(Response::with((status::Ok,
+ Header(ContentType::html()),
+ render_message(&format!("Hello, {}. The time is {}", name, date)))))
+}
+
+fn main() {
+ env_logger::init().unwrap();
+ info!("Some debug info");
+ let router = router!(
+ root: get "/" => hello,
+ name: get "/:name" => hello);
+ let _server = Iron::new(router).http(":::8080").unwrap();
+ println!("On 8080");
+}