diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/db.rs | 20 | ||||
-rw-r--r-- | src/main.rs | 10 | ||||
-rw-r--r-- | src/service.rs | 8 |
3 files changed, 24 insertions, 14 deletions
@@ -1,16 +1,26 @@ | |||
1 | // extern | ||
1 | use anyhow::Result; | 2 | use anyhow::Result; |
2 | use log::{debug, info}; | 3 | use log::{debug, info}; |
3 | use rusqlite::{Connection, OpenFlags, NO_PARAMS}; | 4 | use rusqlite::{Connection, OpenFlags, NO_PARAMS}; |
4 | 5 | ||
6 | // std | ||
5 | use std::fmt; | 7 | use std::fmt; |
6 | use std::path::Path; | 8 | use std::path::Path; |
7 | 9 | ||
8 | pub fn init_db<P: AsRef<Path> + fmt::Display>(p: P) -> Result<Connection> { | 10 | pub fn open_connection<P>(p: P) -> Result<Connection> |
9 | debug!("Looking for database at `{}`", p); | 11 | where |
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 | |||
21 | pub 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 | ||
1 | use anyhow::Result; | 2 | use anyhow::Result; |
2 | use hyper::service::{make_service_fn, service_fn}; | 3 | use hyper::service::{make_service_fn, service_fn}; |
3 | use hyper::Server; | 4 | use hyper::Server; |
4 | use log::trace; | 5 | use log::trace; |
5 | 6 | ||
7 | // internal | ||
6 | mod db; | 8 | mod db; |
7 | use db::init_db; | 9 | use db::{init_db, open_connection}; |
10 | |||
8 | mod service; | 11 | mod service; |
9 | use service::shortner_service; | 12 | use service::shortner_service; |
10 | 13 | ||
11 | use std::sync::{Arc, Mutex}; | ||
12 | |||
13 | fn main() -> Result<()> { | 14 | fn 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 | ||
1 | use anyhow::{Context, Result}; | 2 | use anyhow::{Context, Result}; |
2 | use hyper::header::CONTENT_TYPE; | 3 | use hyper::header::CONTENT_TYPE; |
3 | use hyper::{Body, Method, Request, Response, StatusCode}; | 4 | use hyper::{Body, Method, Request, Response, StatusCode}; |
@@ -7,8 +8,8 @@ use nanoid::nanoid; | |||
7 | use rusqlite::{params, Connection}; | 8 | use rusqlite::{params, Connection}; |
8 | use url::form_urlencoded; | 9 | use url::form_urlencoded; |
9 | 10 | ||
11 | // std | ||
10 | use std::collections::HashMap; | 12 | use std::collections::HashMap; |
11 | use std::sync::{Arc, Mutex}; | ||
12 | 13 | ||
13 | fn respond_with_shortlink<S: AsRef<str>>(shortlink: S) -> Response<Body> { | 14 | fn 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 | ||
93 | pub async fn shortner_service( | 94 | pub 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 |