aboutsummaryrefslogtreecommitdiff
path: root/src
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
parente16743598ccdaae9709034a7215c35ccfe2a49fa (diff)
add logging
Diffstat (limited to 'src')
-rw-r--r--src/db.rs8
-rw-r--r--src/main.rs4
-rw-r--r--src/service.rs14
3 files changed, 20 insertions, 6 deletions
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 @@
1use anyhow::Result; 1use anyhow::Result;
2use log::{debug, info};
2use rusqlite::{Connection, OpenFlags, NO_PARAMS}; 3use rusqlite::{Connection, OpenFlags, NO_PARAMS};
3 4
5use std::fmt;
4use std::path::Path; 6use std::path::Path;
5 7
6pub fn init_db<P: AsRef<Path>>(p: P) -> Result<Connection> { 8pub fn init_db<P: AsRef<Path> + fmt::Display>(p: P) -> Result<Connection> {
9 debug!("Looking for database at `{}`", p);
7 let conn = Connection::open_with_flags( 10 let conn = Connection::open_with_flags(
8 p, 11 &p,
9 OpenFlags::SQLITE_OPEN_CREATE | OpenFlags::SQLITE_OPEN_READ_WRITE, 12 OpenFlags::SQLITE_OPEN_CREATE | OpenFlags::SQLITE_OPEN_READ_WRITE,
10 )?; 13 )?;
11 conn.execute( 14 conn.execute(
@@ -15,5 +18,6 @@ pub fn init_db<P: AsRef<Path>>(p: P) -> Result<Connection> {
15 )", 18 )",
16 NO_PARAMS, 19 NO_PARAMS,
17 )?; 20 )?;
21 info!("SQLite3 database `{}` initialized", &p);
18 return Ok(conn); 22 return Ok(conn);
19} 23}
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 @@
1use anyhow::Result; 1use anyhow::Result;
2use hyper::service::{make_service_fn, service_fn}; 2use hyper::service::{make_service_fn, service_fn};
3use hyper::Server; 3use hyper::Server;
4use log::trace;
4 5
5mod db; 6mod db;
6mod service; 7mod service;
7use service::shortner_service; 8use service::shortner_service;
8 9
9fn main() -> Result<()> { 10fn main() -> Result<()> {
11 pretty_env_logger::init();
10 smol::run(async { 12 smol::run(async {
11 let addr = ([127, 0, 0, 1], 3000).into(); 13 let addr = ([127, 0, 0, 1], 3000).into();
12 let service = 14 let service =
13 make_service_fn(|_| async { Ok::<_, hyper::Error>(service_fn(shortner_service)) }); 15 make_service_fn(|_| async { Ok::<_, hyper::Error>(service_fn(shortner_service)) });
14 let server = Server::bind(&addr).serve(service); 16 let server = Server::bind(&addr).serve(service);
15 println!("Listening on http://{}", addr); 17 trace!("Listening on http://{}", addr);
16 server.await.unwrap(); 18 server.await.unwrap();
17 Ok(()) 19 Ok(())
18 }) 20 })
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 }