aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/db.rs20
-rw-r--r--src/main.rs10
-rw-r--r--src/service.rs8
3 files changed, 24 insertions, 14 deletions
diff --git a/src/db.rs b/src/db.rs
index 4025e12..299e07c 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -1,16 +1,26 @@
1// extern
1use anyhow::Result; 2use anyhow::Result;
2use log::{debug, info}; 3use log::{debug, info};
3use rusqlite::{Connection, OpenFlags, NO_PARAMS}; 4use rusqlite::{Connection, OpenFlags, NO_PARAMS};
4 5
6// std
5use std::fmt; 7use std::fmt;
6use std::path::Path; 8use std::path::Path;
7 9
8pub fn init_db<P: AsRef<Path> + fmt::Display>(p: P) -> Result<Connection> { 10pub fn open_connection<P>(p: P) -> Result<Connection>
9 debug!("Looking for database at `{}`", p); 11where
10 let conn = Connection::open_with_flags( 12 P: AsRef<Path> + fmt::Display,
13{
14 info!("Opened connection to databse");
15 Ok(Connection::open_with_flags(
11 &p, 16 &p,
12 OpenFlags::SQLITE_OPEN_CREATE | OpenFlags::SQLITE_OPEN_READ_WRITE, 17 OpenFlags::SQLITE_OPEN_CREATE | OpenFlags::SQLITE_OPEN_READ_WRITE,
13 )?; 18 )?)
19}
20
21pub fn init_db<P: AsRef<Path> + fmt::Display>(p: P) -> Result<()> {
22 debug!("Looking for database at `{}`", p);
23 let conn = open_connection(&p)?;
14 conn.execute( 24 conn.execute(
15 "CREATE TABLE IF NOT EXISTS urls ( 25 "CREATE TABLE IF NOT EXISTS urls (
16 link TEXT PRIMARY KEY, 26 link TEXT PRIMARY KEY,
@@ -19,5 +29,5 @@ pub fn init_db<P: AsRef<Path> + fmt::Display>(p: P) -> Result<Connection> {
19 NO_PARAMS, 29 NO_PARAMS,
20 )?; 30 )?;
21 info!("SQLite3 database `{}` initialized", &p); 31 info!("SQLite3 database `{}` initialized", &p);
22 return Ok(conn); 32 Ok(())
23} 33}
diff --git a/src/main.rs b/src/main.rs
index c9819d0..35e686e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,22 +1,24 @@
1// extern
1use anyhow::Result; 2use anyhow::Result;
2use hyper::service::{make_service_fn, service_fn}; 3use hyper::service::{make_service_fn, service_fn};
3use hyper::Server; 4use hyper::Server;
4use log::trace; 5use log::trace;
5 6
7// internal
6mod db; 8mod db;
7use db::init_db; 9use db::{init_db, open_connection};
10
8mod service; 11mod service;
9use service::shortner_service; 12use service::shortner_service;
10 13
11use std::sync::{Arc, Mutex};
12
13fn main() -> Result<()> { 14fn main() -> Result<()> {
14 pretty_env_logger::init(); 15 pretty_env_logger::init();
16 init_db("./urls.db_3")?;
15 smol::run(async { 17 smol::run(async {
16 let addr = ([127, 0, 0, 1], 3000).into(); 18 let addr = ([127, 0, 0, 1], 3000).into();
17 let service = make_service_fn(move |_| async { 19 let service = make_service_fn(move |_| async {
18 Ok::<_, hyper::Error>(service_fn(move |req| { 20 Ok::<_, hyper::Error>(service_fn(move |req| {
19 let db_conn = init_db("./urls.db_3").unwrap(); 21 let db_conn = open_connection("./urls.db_3").unwrap();
20 shortner_service(req, db_conn) 22 shortner_service(req, db_conn)
21 })) 23 }))
22 }); 24 });
diff --git a/src/service.rs b/src/service.rs
index 41aa048..4477ee8 100644
--- a/src/service.rs
+++ b/src/service.rs
@@ -1,3 +1,4 @@
1// extern
1use anyhow::{Context, Result}; 2use anyhow::{Context, Result};
2use hyper::header::CONTENT_TYPE; 3use hyper::header::CONTENT_TYPE;
3use hyper::{Body, Method, Request, Response, StatusCode}; 4use hyper::{Body, Method, Request, Response, StatusCode};
@@ -7,8 +8,8 @@ use nanoid::nanoid;
7use rusqlite::{params, Connection}; 8use rusqlite::{params, Connection};
8use url::form_urlencoded; 9use url::form_urlencoded;
9 10
11// std
10use std::collections::HashMap; 12use std::collections::HashMap;
11use std::sync::{Arc, Mutex};
12 13
13fn respond_with_shortlink<S: AsRef<str>>(shortlink: S) -> Response<Body> { 14fn respond_with_shortlink<S: AsRef<str>>(shortlink: S) -> Response<Body> {
14 info!("Successfully generated shortlink"); 15 info!("Successfully generated shortlink");
@@ -90,10 +91,7 @@ async fn process_form(req: Request<Body>, conn: &mut Connection) -> Result<Respo
90 } 91 }
91} 92}
92 93
93pub async fn shortner_service( 94pub async fn shortner_service(req: Request<Body>, mut conn: Connection) -> Result<Response<Body>> {
94 req: Request<Body>,
95 mut conn: Arc<Mutex<Connection>>,
96) -> Result<Response<Body>> {
97 match req.method() { 95 match req.method() {
98 &Method::POST => { 96 &Method::POST => {
99 let boundary = req 97 let boundary = req