From c6d6702cf838fa8d43a511d455f45a385b74e427 Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Sun, 9 Jul 2017 01:23:54 -0400 Subject: add: Rendering of quotes. --- Cargo.lock | 56 +++++++++++++++++++++++++++++ Cargo.toml | 3 ++ src/data.rs | 27 ++++++++++++++ src/data/templates/quotes.hbs | 84 +++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 5 +++ src/server.rs | 23 ++++++++++-- 6 files changed, 196 insertions(+), 2 deletions(-) create mode 100644 src/data.rs create mode 100644 src/data/templates/quotes.hbs diff --git a/Cargo.lock b/Cargo.lock index 83b1de2..9c4291d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10,6 +10,9 @@ dependencies = [ "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "router 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "rusqlite 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -317,6 +320,11 @@ name = "quick-error" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "quote" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "redox_syscall" version = "0.1.21" @@ -401,6 +409,25 @@ name = "serde" version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "serde_derive" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive_internals 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_derive_internals" +version = "0.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", + "synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "serde_json" version = "1.0.2" @@ -417,6 +444,24 @@ name = "strsim" version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "syn" +version = "0.11.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", + "synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "synom" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "term_size" version = "0.3.0" @@ -522,6 +567,11 @@ name = "unicode-width" version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "unicode-xid" +version = "0.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "unreachable" version = "1.0.0" @@ -638,6 +688,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "3a8b4c6b8165cd1a1cd4b9b120978131389f64bdaf456435caa41e630edba903" "checksum plugin 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "1a6a0dc3910bc8db877ffed8e457763b317cf880df4ae19109b9f77d277cf6e0" "checksum quick-error 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3c36987d4978eb1be2e422b1e0423a557923a5c3e7e6f31d5699e9aafaefa469" +"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" "checksum redox_syscall 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)" = "8f4120cd9183efc5cdbac08bee870325ede495745391b0de336723b430d7a1ad" "checksum regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)" = "4fd4ace6a8cf7860714a2c2280d6c1f7e6a413486c13298bbc86fd3da019402f" "checksum regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1731164734096285ec2a5ec7fea5248ae2f5485b3feeb0115af4fda2183b2d1b" @@ -649,8 +700,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda" "checksum same-file 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d931a44fdaa43b8637009e7632a02adc4f2b2e0733c08caa4cf00e8da4a117a7" "checksum serde 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)" = "6a7c6b751a2e8d5df57a5ff71b5b4fc8aaee9ee28ff1341d640dd130bb5f4f7a" +"checksum serde_derive 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)" = "2f6ca58905ebd3c3b285a8a6d4f3ac92b92c0d7951d5649b1bdd212549c06639" +"checksum serde_derive_internals 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)" = "37aee4e0da52d801acfbc0cc219eb1eda7142112339726e427926a6f6ee65d3a" "checksum serde_json 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "48b04779552e92037212c3615370f6bd57a40ebba7f20e554ff9f55e41a69a7b" "checksum strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b4d15c810519a91cf877e7e36e63fe068815c678181439f2f29e2562147c3694" +"checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" +"checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" "checksum term_size 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2b6b55df3198cc93372e85dd2ed817f0e38ce8cc0f22eb32391bfad9c4bf209" "checksum textwrap 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f86300c3e7416ee233abd7cda890c492007a3980f941f79185c753a701257167" "checksum thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9539db560102d1cef46b8b78ce737ff0bb64e7e18d35b2a5688f7d097d0ff03" @@ -665,6 +720,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum unicode-normalization 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "51ccda9ef9efa3f7ef5d91e8f9b83bbe6955f9bf86aec89d5cce2c874625920f" "checksum unicode-segmentation 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "18127285758f0e2c6cf325bb3f3d138a12fee27de4f23e146cd6a179f26c2cf3" "checksum unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "bf3a113775714a22dcb774d8ea3655c53a32debae63a063acc00a91cc586245f" +"checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" "checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" "checksum unsafe-any 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f30360d7979f5e9c6e6cea48af192ea8fab4afb3cf72597154b8f08935bc9c7f" "checksum url 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "eeb819346883532a271eb626deb43c4a1bb4c4dd47c519bd78137c3e72a4fe27" diff --git a/Cargo.toml b/Cargo.toml index 1da33da..bbed912 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,6 @@ log = "^0.3" env_logger = "^0.3" rusqlite = "^0.12.0" clap = "^2.25.0" +serde = "^1.0.0" +serde_json = "^1.0.0" +serde_derive = "^1.0.0" diff --git a/src/data.rs b/src/data.rs new file mode 100644 index 0000000..ee39613 --- /dev/null +++ b/src/data.rs @@ -0,0 +1,27 @@ +use serde_json::{Value, Map}; +use handlebars_iron::handlebars::to_json; + +#[derive(Serialize, Debug)] +pub struct Quote { + id: u64, + author: String, + date: String, + score: String, + votes: u32, + content: String +} + +pub fn make_data() -> Map { + let mut data = Map::new(); + data.insert("quotes".to_string(), to_json(&vec!( + &Quote { + id: 1, + author: "panda_man".to_owned(), + date: "2017-07-01".to_owned(), + score: format!("{:.2}", 450.0 / 96.0), + votes: 99, + content: " hvor er jantho?".to_owned(), + } + ))); + data +} diff --git a/src/data/templates/quotes.hbs b/src/data/templates/quotes.hbs new file mode 100644 index 0000000..68f0a5b --- /dev/null +++ b/src/data/templates/quotes.hbs @@ -0,0 +1,84 @@ + + + + + + Quotes fra #linux.no på freenode + + + + +
+                     _ _  
+                    | (_)_ __  _   ___  __  _ __   ___  
+              _|_|_ | | | '_ \| | | \ \/ / | '_ \ / _ \ 
+              _|_|_ | | | | | | |_| |)  ( _| | | | (_) | 
+               | |  |_|_|_| |_|\__,_/_/\_(_)_| |_|\___/ 
+             -=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=- GAMMA -=-
+                   Quotes fra #linux.no på freenode
+                  Klikk her for å legge til en quote
+
+              Sortér etter quote-dato | score | timestamp
+
+
+ +{{#each quotes}} + +
+#{{id}}, lagt til av {{author}}
+Dato: {{date}}, Score: + +{{score}} (fra {{votes}}), Vote: + 1 + 2 + 3 + 4 + 5 + , + +

+

+ +{{content}} +
+
+{{/each}} + + +
+
+

linoquotes v.3 © 2004-2017 Kjetil Ørbekk, Erlend Hamberg, Vidar Holen, John H. Anthony. +

Source code at https://git.orbekk.com/linoquotes-gamma.git. +

The quotes on this page are copyright their respective owners and submitters.

+
+ + + + + diff --git a/src/lib.rs b/src/lib.rs index 9ca0a0b..dd4cf29 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,5 +3,10 @@ extern crate log; extern crate iron; #[macro_use] extern crate router; +extern crate handlebars_iron; +extern crate serde_json; +#[macro_use] +extern crate serde_derive; pub mod server; +mod data; diff --git a/src/server.rs b/src/server.rs index 3a15e45..37c5713 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,18 +1,37 @@ use iron::headers::{ContentType}; use iron::modifiers::{Header}; use iron::{Iron, Chain, Request, Response, IronResult, status}; +use handlebars_iron::{HandlebarsEngine, MemorySource, Template}; +use std::collections::BTreeMap; +use data; + +fn renderer() -> HandlebarsEngine { + let mut e = HandlebarsEngine::new(); + + let mut templates = BTreeMap::new(); + templates.insert("quotes".to_string(), + include_str!("data/templates/quotes.hbs").to_string()); + + e.add(Box::new(MemorySource(templates))); + if let Err(r) = e.reload() { + panic!("Error loading templates: {}", r) + } + e +} fn info(_r: &mut Request) -> IronResult { + let data = data::make_data(); Ok(Response::with((status::Ok, Header(ContentType::html()), - "

Info"))) + Template::new("quotes", data)))) } pub fn serve(port: u16) { let router = router!( info: get "/" => info, ); - let chain = Chain::new(router); + let mut chain = Chain::new(router); + chain.link_after(renderer()); let bind_address = format!("{}:{}", "::", port); let _server = Iron::new(chain).http(bind_address.as_str()); info!("Serving on {}", bind_address); -- cgit v1.2.3