aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock8
-rw-r--r--Cargo.toml2
-rw-r--r--src/cli.rs35
-rw-r--r--src/db.rs8
-rw-r--r--src/main.rs12
5 files changed, 57 insertions, 8 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 80bfb9c..c0016a7 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -390,9 +390,11 @@ version = "0.1.0"
390dependencies = [ 390dependencies = [
391 "anyhow", 391 "anyhow",
392 "hyper", 392 "hyper",
393 "lazy_static",
393 "log", 394 "log",
394 "multer", 395 "multer",
395 "nanoid", 396 "nanoid",
397 "pico-args",
396 "pretty_env_logger", 398 "pretty_env_logger",
397 "rusqlite", 399 "rusqlite",
398 "smol", 400 "smol",
@@ -694,6 +696,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
694checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" 696checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
695 697
696[[package]] 698[[package]]
699name = "pico-args"
700version = "0.3.4"
701source = "registry+https://github.com/rust-lang/crates.io-index"
702checksum = "28b9b4df73455c861d7cbf8be42f01d3b373ed7f02e378d55fa84eafc6f638b1"
703
704[[package]]
697name = "pin-project" 705name = "pin-project"
698version = "0.4.23" 706version = "0.4.23"
699source = "registry+https://github.com/rust-lang/crates.io-index" 707source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index b65b215..0ab5f8f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -15,6 +15,8 @@ anyhow = "1.0"
15multer = "1.2" 15multer = "1.2"
16log = "0.4" 16log = "0.4"
17pretty_env_logger = "0.4.0" 17pretty_env_logger = "0.4.0"
18pico-args = "0.3.4"
19lazy_static = "1.4.0"
18 20
19 21
20[dependencies.smol] 22[dependencies.smol]
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 });