aboutsummaryrefslogtreecommitdiff
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
parente16743598ccdaae9709034a7215c35ccfe2a49fa (diff)
add logging
-rw-r--r--Cargo.lock69
-rw-r--r--Cargo.toml2
-rw-r--r--src/db.rs8
-rw-r--r--src/main.rs4
-rw-r--r--src/service.rs14
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
@@ -72,6 +72,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
72checksum = "065374052e7df7ee4047b1160cca5e1467a12351a40b3da123c870ba0b8eda2a" 72checksum = "065374052e7df7ee4047b1160cca5e1467a12351a40b3da123c870ba0b8eda2a"
73 73
74[[package]] 74[[package]]
75name = "atty"
76version = "0.2.14"
77source = "registry+https://github.com/rust-lang/crates.io-index"
78checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
79dependencies = [
80 "hermit-abi",
81 "libc",
82 "winapi 0.3.9",
83]
84
85[[package]]
75name = "autocfg" 86name = "autocfg"
76version = "0.1.7" 87version = "0.1.7"
77source = "registry+https://github.com/rust-lang/crates.io-index" 88source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -171,6 +182,19 @@ dependencies = [
171] 182]
172 183
173[[package]] 184[[package]]
185name = "env_logger"
186version = "0.7.1"
187source = "registry+https://github.com/rust-lang/crates.io-index"
188checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
189dependencies = [
190 "atty",
191 "humantime",
192 "log",
193 "regex",
194 "termcolor",
195]
196
197[[package]]
174name = "event-listener" 198name = "event-listener"
175version = "2.4.0" 199version = "2.4.0"
176source = "registry+https://github.com/rust-lang/crates.io-index" 200source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -366,8 +390,10 @@ version = "0.1.0"
366dependencies = [ 390dependencies = [
367 "anyhow", 391 "anyhow",
368 "hyper", 392 "hyper",
393 "log",
369 "multer", 394 "multer",
370 "nanoid", 395 "nanoid",
396 "pretty_env_logger",
371 "rusqlite", 397 "rusqlite",
372 "smol", 398 "smol",
373 "url", 399 "url",
@@ -410,6 +436,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
410checksum = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9" 436checksum = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9"
411 437
412[[package]] 438[[package]]
439name = "humantime"
440version = "1.3.0"
441source = "registry+https://github.com/rust-lang/crates.io-index"
442checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f"
443dependencies = [
444 "quick-error",
445]
446
447[[package]]
413name = "hyper" 448name = "hyper"
414version = "0.13.7" 449version = "0.13.7"
415source = "registry+https://github.com/rust-lang/crates.io-index" 450source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -709,6 +744,16 @@ dependencies = [
709] 744]
710 745
711[[package]] 746[[package]]
747name = "pretty_env_logger"
748version = "0.4.0"
749source = "registry+https://github.com/rust-lang/crates.io-index"
750checksum = "926d36b9553851b8b0005f1275891b392ee4d2d833852c417ed025477350fb9d"
751dependencies = [
752 "env_logger",
753 "log",
754]
755
756[[package]]
712name = "proc-macro-hack" 757name = "proc-macro-hack"
713version = "0.5.18" 758version = "0.5.18"
714source = "registry+https://github.com/rust-lang/crates.io-index" 759source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -730,6 +775,12 @@ dependencies = [
730] 775]
731 776
732[[package]] 777[[package]]
778name = "quick-error"
779version = "1.2.3"
780source = "registry+https://github.com/rust-lang/crates.io-index"
781checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
782
783[[package]]
733name = "quote" 784name = "quote"
734version = "1.0.7" 785version = "1.0.7"
735source = "registry+https://github.com/rust-lang/crates.io-index" 786source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -951,6 +1002,15 @@ dependencies = [
951] 1002]
952 1003
953[[package]] 1004[[package]]
1005name = "termcolor"
1006version = "1.1.0"
1007source = "registry+https://github.com/rust-lang/crates.io-index"
1008checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f"
1009dependencies = [
1010 "winapi-util",
1011]
1012
1013[[package]]
954name = "thread_local" 1014name = "thread_local"
955version = "1.0.1" 1015version = "1.0.1"
956source = "registry+https://github.com/rust-lang/crates.io-index" 1016source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1156,6 +1216,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
1156checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 1216checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
1157 1217
1158[[package]] 1218[[package]]
1219name = "winapi-util"
1220version = "0.1.5"
1221source = "registry+https://github.com/rust-lang/crates.io-index"
1222checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
1223dependencies = [
1224 "winapi 0.3.9",
1225]
1226
1227[[package]]
1159name = "winapi-x86_64-pc-windows-gnu" 1228name = "winapi-x86_64-pc-windows-gnu"
1160version = "0.4.0" 1229version = "0.4.0"
1161source = "registry+https://github.com/rust-lang/crates.io-index" 1230source = "registry+https://github.com/rust-lang/crates.io-index"
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"
13url = "2.1.1" 13url = "2.1.1"
14anyhow = "1.0" 14anyhow = "1.0"
15multer = "1.2" 15multer = "1.2"
16log = "0.4"
17pretty_env_logger = "0.4.0"
16 18
17 19
18[dependencies.smol] 20[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 @@
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 }