From 311b48924d2e20deb8733a2b94a531f55a83e205 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 23 Aug 2020 21:26:02 +0530 Subject: add logging --- src/db.rs | 8 ++++++-- src/main.rs | 4 +++- src/service.rs | 14 +++++++++++--- 3 files changed, 20 insertions(+), 6 deletions(-) (limited to 'src') 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