From 311b48924d2e20deb8733a2b94a531f55a83e205 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 23 Aug 2020 21:26:02 +0530 Subject: add logging --- Cargo.lock | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/db.rs | 8 +++++-- src/main.rs | 4 +++- src/service.rs | 14 +++++++++--- 5 files changed, 91 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9759d76..80bfb9c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -71,6 +71,17 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "065374052e7df7ee4047b1160cca5e1467a12351a40b3da123c870ba0b8eda2a" +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi 0.3.9", +] + [[package]] name = "autocfg" version = "0.1.7" @@ -170,6 +181,19 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "env_logger" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" +dependencies = [ + "atty", + "humantime", + "log", + "regex", + "termcolor", +] + [[package]] name = "event-listener" version = "2.4.0" @@ -366,8 +390,10 @@ version = "0.1.0" dependencies = [ "anyhow", "hyper", + "log", "multer", "nanoid", + "pretty_env_logger", "rusqlite", "smol", "url", @@ -409,6 +435,15 @@ version = "1.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9" +[[package]] +name = "humantime" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f" +dependencies = [ + "quick-error", +] + [[package]] name = "hyper" version = "0.13.7" @@ -708,6 +743,16 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "pretty_env_logger" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "926d36b9553851b8b0005f1275891b392ee4d2d833852c417ed025477350fb9d" +dependencies = [ + "env_logger", + "log", +] + [[package]] name = "proc-macro-hack" version = "0.5.18" @@ -729,6 +774,12 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "quick-error" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" + [[package]] name = "quote" version = "1.0.7" @@ -950,6 +1001,15 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "termcolor" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f" +dependencies = [ + "winapi-util", +] + [[package]] name = "thread_local" version = "1.0.1" @@ -1155,6 +1215,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi 0.3.9", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index ca60681..b65b215 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,8 @@ nanoid = "0.3.0" url = "2.1.1" anyhow = "1.0" multer = "1.2" +log = "0.4" +pretty_env_logger = "0.4.0" [dependencies.smol] diff --git a/src/db.rs b/src/db.rs index a5c0f85..4025e12 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,11 +1,14 @@ use anyhow::Result; +use log::{debug, info}; use rusqlite::{Connection, OpenFlags, NO_PARAMS}; +use std::fmt; use std::path::Path; -pub fn init_db>(p: P) -> Result { +pub fn init_db + fmt::Display>(p: P) -> Result { + debug!("Looking for database at `{}`", p); let conn = Connection::open_with_flags( - p, + &p, OpenFlags::SQLITE_OPEN_CREATE | OpenFlags::SQLITE_OPEN_READ_WRITE, )?; conn.execute( @@ -15,5 +18,6 @@ pub fn init_db>(p: P) -> Result { )", NO_PARAMS, )?; + info!("SQLite3 database `{}` initialized", &p); return Ok(conn); } diff --git a/src/main.rs b/src/main.rs index d31abfe..6b48957 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,20 @@ use anyhow::Result; use hyper::service::{make_service_fn, service_fn}; use hyper::Server; +use log::trace; mod db; mod service; use service::shortner_service; fn main() -> Result<()> { + pretty_env_logger::init(); smol::run(async { let addr = ([127, 0, 0, 1], 3000).into(); let service = make_service_fn(|_| async { Ok::<_, hyper::Error>(service_fn(shortner_service)) }); let server = Server::bind(&addr).serve(service); - println!("Listening on http://{}", addr); + trace!("Listening on http://{}", addr); server.await.unwrap(); Ok(()) }) diff --git a/src/service.rs b/src/service.rs index 55a42bf..5883b40 100644 --- a/src/service.rs +++ b/src/service.rs @@ -1,6 +1,7 @@ use anyhow::{Context, Result}; use hyper::header::CONTENT_TYPE; use hyper::{Body, Method, Request, Response, StatusCode}; +use log::{debug, error, info, trace}; use multer::Multipart; use nanoid::nanoid; use rusqlite::{params, Connection}; @@ -11,6 +12,7 @@ use std::collections::HashMap; use crate::db::init_db; fn respond_with_shortlink>(shortlink: S) -> Response { + info!("Successfully generated shortlink"); Response::builder() .status(StatusCode::OK) .header("content-type", "text/html") @@ -56,6 +58,7 @@ async fn process_multipart( let mut m = Multipart::new(body, boundary); if let Some(field) = m.next_field().await? { if field.name() == Some("shorten") { + trace!("Recieved valid multipart request"); let content = field .text() .await @@ -65,9 +68,8 @@ async fn process_multipart( return Ok(respond_with_shortlink(shortlink)); } } - Ok(Response::builder() - .status(StatusCode::OK) - .body(Body::empty())?) + trace!("Unprocessable multipart request!"); + Ok(respond_with_status(StatusCode::UNPROCESSABLE_ENTITY)) } pub async fn shortner_service(req: Request) -> Result> { @@ -91,19 +93,24 @@ pub async fn shortner_service(req: Request) -> Result> { .collect::>(); if let Some(n) = params.get("shorten") { + trace!("POST: {}", &n); let s = shorten(n, &mut conn)?; return Ok(respond_with_shortlink(s)); } else { + error!("Invalid form"); return Ok(respond_with_status(StatusCode::UNPROCESSABLE_ENTITY)); } } + trace!("Attempting to parse multipart request"); return process_multipart(req.into_body(), boundary.unwrap(), &mut conn).await; } &Method::GET => { + trace!("GET: {}", req.uri()); let shortlink = req.uri().path().to_string(); let link = get_link(&shortlink[1..], &mut conn); if let Some(l) = link.unwrap() { + trace!("Found in database, redirecting ..."); Ok(Response::builder() .header("Location", &l) .header("content-type", "text/html") @@ -113,6 +120,7 @@ pub async fn shortner_service(req: Request) -> Result> { &l )))?) } else { + error!("Resource not found"); Ok(respond_with_status(StatusCode::NOT_FOUND)) } } -- cgit v1.2.3