summaryrefslogtreecommitdiff
path: root/src
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 /src
parente033861befa702b9110d531c4d17b40305915ffc (diff)
add: Request logging (enabled with RUST_LOG=logger=info)
Diffstat (limited to 'src')
-rw-r--r--src/data.rs13
-rw-r--r--src/lib.rs1
-rw-r--r--src/main.rs16
-rw-r--r--src/server.rs8
4 files changed, 26 insertions, 12 deletions
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);