summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKjetil Orbekk <kjetil.orbekk@gmail.com>2017-07-12 22:58:45 -0400
committerKjetil Orbekk <kjetil.orbekk@gmail.com>2017-07-12 22:58:45 -0400
commitba1ab61901e657903a34b0b0acecb81c12b5e0d0 (patch)
treebd9052ffb3c507a1cd27254e3134632d32e6daba
parente033861befa702b9110d531c4d17b40305915ffc (diff)
add: Request logging (enabled with RUST_LOG=logger=info)
-rw-r--r--Cargo.lock12
-rw-r--r--Cargo.toml1
-rw-r--r--src/data.rs13
-rw-r--r--src/lib.rs1
-rw-r--r--src/main.rs16
-rw-r--r--src/server.rs8
6 files changed, 39 insertions, 12 deletions
diff --git a/Cargo.lock b/Cargo.lock
index b1263db..616dc9f 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -9,6 +9,7 @@ dependencies = [
"handlebars-iron 0.24.1 (registry+https://github.com/rust-lang/crates.io-index)",
"iron 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
+ "logger 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"params 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"persistent 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"router 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -277,6 +278,16 @@ version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
+name = "logger"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "iron 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
+ "time 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "lru-cache"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -912,6 +923,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum libsqlite3-sys 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "370090ad578ba845a3ad4f383ceb3deba7abd51ab1915ad1f2c982cc6035e31c"
"checksum linked-hash-map 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7860ec297f7008ff7a1e3382d7f7e1dcd69efc94751a2284bafc3d013c2aa939"
"checksum log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "880f77541efa6e5cc74e76910c9884d9859683118839d6a1dc3b11e63512565b"
+"checksum logger 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "92ff59f9a797ff30f711fe6b8489ad424953cee17c206de77d3c5957a9182ba7"
"checksum lru-cache 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4d06ff7ff06f729ce5f4e227876cb88d10bc59cd4ae1e09fbb2bde15c850dc21"
"checksum matches 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "100aabe6b8ff4e4a7e32c1c13523379802df0772b82466207ac25b013f193376"
"checksum memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d8b629fb514376c675b98c1421e80b151d3817ac42d7c667717d282761418d20"
diff --git a/Cargo.toml b/Cargo.toml
index b55533f..9f288a0 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -5,6 +5,7 @@ authors = ["Kjetil Ørbekk <kj@orbekk.com>"]
[dependencies]
iron = "~0.5.0"
+logger = "~0.3.0"
router = "~0.5.0"
handlebars = "~0.26.0"
handlebars-iron = "~0.24.0"
diff --git a/src/data.rs b/src/data.rs
index c12db94..5674f92 100644
--- a/src/data.rs
+++ b/src/data.rs
@@ -68,15 +68,16 @@ pub fn new_quote(c: &Connection, date: &str, author: &str, content: &str) -> Res
Ok(())
}
-pub fn new_vote(c: &Connection, quote_id: i64, score: i32) -> Result<()>{
- c.execute(
- r#"
+pub fn new_vote(c: &Connection, quote_id: i64, score: i32) -> Result<()> {
+ c.execute(
+ r#"
INSERT INTO votes (quote_id, score) VALUES
(?1, ?2)
"#,
- &[&quote_id, &score])?;
- info!("New vote: quote_id({}) score({})", quote_id, score);
- Ok(())
+ &[&quote_id, &score],
+ )?;
+ info!("New vote: quote_id({}) score({})", quote_id, score);
+ Ok(())
}
pub fn approve_quote(c: &Connection, quote_id: i64) -> Result<()> {
diff --git a/src/lib.rs b/src/lib.rs
index 6b3e4fb..6b448d9 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -12,6 +12,7 @@ extern crate rusqlite;
extern crate persistent;
extern crate params;
extern crate chrono;
+extern crate logger;
pub mod server;
pub mod data;
diff --git a/src/main.rs b/src/main.rs
index 9d9ac05..026f610 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -62,11 +62,17 @@ fn main() {
linoquotes_gamma::data::populate_test_db(&state.connection).unwrap();
}
- linoquotes_gamma::data::new_quote(&state.connection, "2017-07-10", "orbekk", "another test")
- .unwrap();
- let qid = state.connection.last_insert_rowid();
- info!("Last inserted quote: {}", qid);
- linoquotes_gamma::data::approve_quote(&state.connection, qid).unwrap();
+ for i in 1..1000 {
+ linoquotes_gamma::data::new_quote(
+ &state.connection,
+ "2017-07-10",
+ "orbekk",
+ &format!("another test {}", i),
+ ).unwrap();
+ let qid = state.connection.last_insert_rowid();
+ info!("Last inserted quote: {}", qid);
+ linoquotes_gamma::data::approve_quote(&state.connection, qid).unwrap();
+ }
linoquotes_gamma::server::serve(state, port);
}
diff --git a/src/server.rs b/src/server.rs
index bd3e8ac..4816c33 100644
--- a/src/server.rs
+++ b/src/server.rs
@@ -12,6 +12,7 @@ use serde_json::Map;
use params;
use error::LinoError;
use chrono;
+use logger::Logger;
#[derive(Debug)]
pub struct State {
@@ -80,6 +81,7 @@ fn quotes(r: &mut Request) -> IronResult<Response> {
None => data::get_quotes(&state.connection, &ordering)?,
}
};
+ let quotes = quotes.into_iter().collect::<Vec<_>>();
result.insert("quotes".to_string(), to_json(&quotes));
Ok(Response::with((
status::Ok,
@@ -95,7 +97,7 @@ fn add_get(_r: &mut Request) -> IronResult<Response> {
Ok(Response::with((
status::Ok,
Header(ContentType::html()),
- Template::new("add", result)
+ Template::new("add", result),
)))
}
@@ -204,9 +206,13 @@ pub fn serve(state: State, port: u16) {
approve: get "/approve.jsp" => approve,
vote: get "/vote" => vote,
);
+ let (logger_before, logger_after) = Logger::new(None);
+
let mut chain = Chain::new(router);
+ chain.link_before(logger_before);
chain.link_after(make_renderer());
chain.link(Write::<State>::both(state));
+ chain.link_after(logger_after);
let bind_address = format!("{}:{}", "::", port);
let _server = Iron::new(chain).http(bind_address.as_str());
info!("Serving on {}", bind_address);