aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-08-24 09:03:06 +0100
committerAkshay <[email protected]>2020-08-24 09:03:06 +0100
commit7308121fa3efc785bf964b8ab32505ba25513fdc (patch)
treee3d32a7adcbb66c65e20e9b3811e117c602969ba /src
parent0e56a198c62ed9a9c1812cff1ffa618260535a35 (diff)
add cli opts
Diffstat (limited to 'src')
-rw-r--r--src/cli.rs35
-rw-r--r--src/db.rs8
-rw-r--r--src/main.rs12
3 files changed, 47 insertions, 8 deletions
diff --git a/src/cli.rs b/src/cli.rs
new file mode 100644
index 0000000..009397d
--- /dev/null
+++ b/src/cli.rs
@@ -0,0 +1,35 @@
1use anyhow::Result;
2use lazy_static::lazy_static;
3
4use std::default::Default;
5use std::path::PathBuf;
6
7pub struct Config {
8 pub port: u16,
9 pub db_path: PathBuf,
10}
11
12impl Default for Config {
13 fn default() -> Self {
14 Config {
15 port: 3000,
16 db_path: "./urls.db_3".into(),
17 }
18 }
19}
20
21lazy_static! {
22 pub static ref CONFIG: Config = parse_args().unwrap_or(Default::default());
23}
24
25fn parse_args() -> Result<Config> {
26 let mut _a = pico_args::Arguments::from_env();
27 return Ok(Config {
28 port: _a
29 .opt_value_from_fn("--port", str::parse::<u16>)?
30 .unwrap_or(7878),
31 db_path: _a
32 .opt_value_from_str("--database")?
33 .unwrap_or(PathBuf::from("./urls.db_3")),
34 });
35}
diff --git a/src/db.rs b/src/db.rs
index c67e39e..8503b10 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -9,7 +9,7 @@ use std::path::Path;
9 9
10pub fn open_connection<P>(p: P) -> Result<Connection> 10pub fn open_connection<P>(p: P) -> Result<Connection>
11where 11where
12 P: AsRef<Path> + fmt::Display, 12 P: AsRef<Path>,
13{ 13{
14 info!("Opened connection to database"); 14 info!("Opened connection to database");
15 Ok(Connection::open_with_flags( 15 Ok(Connection::open_with_flags(
@@ -18,8 +18,8 @@ where
18 )?) 18 )?)
19} 19}
20 20
21pub fn init_db<P: AsRef<Path> + fmt::Display>(p: P) -> Result<()> { 21pub fn init_db<P: AsRef<Path> + fmt::Debug>(p: P) -> Result<()> {
22 debug!("Looking for database at `{}`", p); 22 debug!("Looking for database at `{:?}`", p);
23 let conn = open_connection(&p)?; 23 let conn = open_connection(&p)?;
24 conn.execute( 24 conn.execute(
25 "CREATE TABLE IF NOT EXISTS urls ( 25 "CREATE TABLE IF NOT EXISTS urls (
@@ -28,6 +28,6 @@ pub fn init_db<P: AsRef<Path> + fmt::Display>(p: P) -> Result<()> {
28 )", 28 )",
29 NO_PARAMS, 29 NO_PARAMS,
30 )?; 30 )?;
31 info!("SQLite3 database `{}` initialized", &p); 31 info!("SQLite3 database `{:?}` initialized", &p);
32 Ok(()) 32 Ok(())
33} 33}
diff --git a/src/main.rs b/src/main.rs
index 35e686e..5cd3492 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -11,14 +11,18 @@ use db::{init_db, open_connection};
11mod service; 11mod service;
12use service::shortner_service; 12use service::shortner_service;
13 13
14mod cli;
15use cli::CONFIG;
16
14fn main() -> Result<()> { 17fn main() -> Result<()> {
15 pretty_env_logger::init(); 18 pretty_env_logger::init();
16 init_db("./urls.db_3")?; 19
20 init_db(&CONFIG.db_path)?;
17 smol::run(async { 21 smol::run(async {
18 let addr = ([127, 0, 0, 1], 3000).into(); 22 let addr = ([127, 0, 0, 1], CONFIG.port).into();
19 let service = make_service_fn(move |_| async { 23 let service = make_service_fn(|_| async {
20 Ok::<_, hyper::Error>(service_fn(move |req| { 24 Ok::<_, hyper::Error>(service_fn(move |req| {
21 let db_conn = open_connection("./urls.db_3").unwrap(); 25 let db_conn = open_connection(&CONFIG.db_path).unwrap();
22 shortner_service(req, db_conn) 26 shortner_service(req, db_conn)
23 })) 27 }))
24 }); 28 });