aboutsummaryrefslogtreecommitdiff
path: root/src/service.rs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-08-23 16:56:02 +0100
committerAkshay <[email protected]>2020-08-23 16:56:02 +0100
commit311b48924d2e20deb8733a2b94a531f55a83e205 (patch)
treeb77895990e09ae425e710dfaa37b6ac338306dae /src/service.rs
parente16743598ccdaae9709034a7215c35ccfe2a49fa (diff)
add logging
Diffstat (limited to 'src/service.rs')
-rw-r--r--src/service.rs14
1 files changed, 11 insertions, 3 deletions
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 @@
1use anyhow::{Context, Result}; 1use anyhow::{Context, Result};
2use hyper::header::CONTENT_TYPE; 2use hyper::header::CONTENT_TYPE;
3use hyper::{Body, Method, Request, Response, StatusCode}; 3use hyper::{Body, Method, Request, Response, StatusCode};
4use log::{debug, error, info, trace};
4use multer::Multipart; 5use multer::Multipart;
5use nanoid::nanoid; 6use nanoid::nanoid;
6use rusqlite::{params, Connection}; 7use rusqlite::{params, Connection};
@@ -11,6 +12,7 @@ use std::collections::HashMap;
11use crate::db::init_db; 12use crate::db::init_db;
12 13
13fn respond_with_shortlink<S: AsRef<str>>(shortlink: S) -> Response<Body> { 14fn respond_with_shortlink<S: AsRef<str>>(shortlink: S) -> Response<Body> {
15 info!("Successfully generated shortlink");
14 Response::builder() 16 Response::builder()
15 .status(StatusCode::OK) 17 .status(StatusCode::OK)
16 .header("content-type", "text/html") 18 .header("content-type", "text/html")
@@ -56,6 +58,7 @@ async fn process_multipart(
56 let mut m = Multipart::new(body, boundary); 58 let mut m = Multipart::new(body, boundary);
57 if let Some(field) = m.next_field().await? { 59 if let Some(field) = m.next_field().await? {
58 if field.name() == Some("shorten") { 60 if field.name() == Some("shorten") {
61 trace!("Recieved valid multipart request");
59 let content = field 62 let content = field
60 .text() 63 .text()
61 .await 64 .await
@@ -65,9 +68,8 @@ async fn process_multipart(
65 return Ok(respond_with_shortlink(shortlink)); 68 return Ok(respond_with_shortlink(shortlink));
66 } 69 }
67 } 70 }
68 Ok(Response::builder() 71 trace!("Unprocessable multipart request!");
69 .status(StatusCode::OK) 72 Ok(respond_with_status(StatusCode::UNPROCESSABLE_ENTITY))
70 .body(Body::empty())?)
71} 73}
72 74
73pub async fn shortner_service(req: Request<Body>) -> Result<Response<Body>> { 75pub async fn shortner_service(req: Request<Body>) -> Result<Response<Body>> {
@@ -91,19 +93,24 @@ pub async fn shortner_service(req: Request<Body>) -> Result<Response<Body>> {
91 .collect::<HashMap<String, String>>(); 93 .collect::<HashMap<String, String>>();
92 94
93 if let Some(n) = params.get("shorten") { 95 if let Some(n) = params.get("shorten") {
96 trace!("POST: {}", &n);
94 let s = shorten(n, &mut conn)?; 97 let s = shorten(n, &mut conn)?;
95 return Ok(respond_with_shortlink(s)); 98 return Ok(respond_with_shortlink(s));
96 } else { 99 } else {
100 error!("Invalid form");
97 return Ok(respond_with_status(StatusCode::UNPROCESSABLE_ENTITY)); 101 return Ok(respond_with_status(StatusCode::UNPROCESSABLE_ENTITY));
98 } 102 }
99 } 103 }
100 104
105 trace!("Attempting to parse multipart request");
101 return process_multipart(req.into_body(), boundary.unwrap(), &mut conn).await; 106 return process_multipart(req.into_body(), boundary.unwrap(), &mut conn).await;
102 } 107 }
103 &Method::GET => { 108 &Method::GET => {
109 trace!("GET: {}", req.uri());
104 let shortlink = req.uri().path().to_string(); 110 let shortlink = req.uri().path().to_string();
105 let link = get_link(&shortlink[1..], &mut conn); 111 let link = get_link(&shortlink[1..], &mut conn);
106 if let Some(l) = link.unwrap() { 112 if let Some(l) = link.unwrap() {
113 trace!("Found in database, redirecting ...");
107 Ok(Response::builder() 114 Ok(Response::builder()
108 .header("Location", &l) 115 .header("Location", &l)
109 .header("content-type", "text/html") 116 .header("content-type", "text/html")
@@ -113,6 +120,7 @@ pub async fn shortner_service(req: Request<Body>) -> Result<Response<Body>> {
113 &l 120 &l
114 )))?) 121 )))?)
115 } else { 122 } else {
123 error!("Resource not found");
116 Ok(respond_with_status(StatusCode::NOT_FOUND)) 124 Ok(respond_with_status(StatusCode::NOT_FOUND))
117 } 125 }
118 } 126 }